• MySQL查询语句


    -- 查询全部内容
    SELECT * FROM sc;
    -- 查询部分列
    SELECT zno,sno,sname FROM student;
    -- 限制条件查询部分列
    SELECT zno,sno,sname,ssex FROM student WHERE ssex = '女';
    -- 限制查询条数
    SELECT zno,sno,sname,ssex FROM student WHERE ssex = '女' LIMIT 6;
    -- 列别名和表别名:可读性增加,简洁性 as可以省略
    SELECT zno as 专业代号,sno as 学号,sname 姓名,ssex 性别 FROM student WHERE ssex = '女' LIMIT 6;
    -- DISTINCT避免重复数据
    SELECT DISTINCT zno FROM student;
    -- 限制查询条件
    SELECT * FROM sc WHERE grade >= 70 and grade <= 80;
    SELECT * FROM sc WHERE grade BETWEEN 70 and 80;
    
    SELECT * FROM sc WHERE grade < 70 or grade > 80;
    SELECT * FROM sc WHERE grade NOT BETWEEN 70 and 80;
    -- 学c0001的学号和成绩
    SELECT sno,grade FROM sc WHERE cno = 'c0001';
    -- 学c0001或c0002的学号和成绩
    SELECT sno,grade FROM sc WHERE cno = 'c0001' or cno = 'c0002';
    SELECT sno,grade FROM sc WHERE cno in( 'c0001','c0002');
    -- 学c0001且c0002的学号和成绩(由in引导的嵌套子查询)
    SELECT sno,grade FROM sc WHERE cno = 'c0001' and sno in(SELECT sno FROM sc WHERE cno = 'c0002');
    -- 查询成绩在集合(65,75,85,95)中的成绩
    SELECT sno,grade FROM sc WHERE grade in(65,75,85,95);
    -- 查询成绩不在集合(65,75,85,95)中的成绩
    SELECT sno,grade FROM sc WHERE grade not in(65,75,85,95);
    
    -- 查询没有分配专业的学生的成绩 NULL比较不能用等号用is 或is not null替代了等号和不等号
    SELECT sno,sname FROM student WHERE zno is null;
    SELECT sno,sname FROM student WHERE zno is not null;
    -- 更新,修改值
    UPDATE student set zno = NULL WHERE sno = '1';
    -- 查询王晓婷的信息
    SELECT * FROM student WHERE sname = '王晓婷';
    -- 模糊查找姓王的同学的信息
    SELECT * FROM student WHERE sname like '王%';
    -- 查询名字中含有
    UPDATE student set sname = '王二晓' WHERE sname = '王千津';
    -- 查询名字中带晓的 not like %(n个字符) _(一个字符)
    SELECT * FROM student WHERE sname like '%晓%';
    -- 查询男女学生人数和总人数
    -- 聚合函数:count(*);sum(列);AUG(列);Max();Min()
    SELECT ssex,COUNT(ssex) FROM student GROUP BY ssex WITH ROLLUP;
    -- GROUP BY 待分类的列名 分类汇总(统计)-- ORDER BY 排序
    -- 查询各个专业的人数 *不忽略null值,对null也计数
    SELECT zno,COUNT(*) FROM student GROUP BY zno;
    -- 对空值不计数
    SELECT zno,COUNT(zno) FROM student GROUP BY zno;
    -- 只显示专业人数超过5的专业
    SELECT zno,COUNT(*) FROM student GROUP BY zno HAVING COUNT(zno)>=5;
    -- 查询学习2门以上课程的学号和门数
    SELECT sno,count(*) FROM sc GROUP BY sno HAVING COUNT(*)>=2;
    -- 查询student中按zno字段排序 默认升序ASC 降序DESC
    SELECT * FROM student ORDER BY zno DESC,sno DESC;
    
  • 相关阅读:
    apache启用gzip压缩方法--转载自http://www.cnblogs.com/linzhenjie/archive/2013/03/05/2943635.html
    yii 主从数据库分离-转载http://www.yiichina.com/doc/guide/2.0/db-dao
    服装尺寸
    php 同步因子的并发处理
    NFC会员管理-转载自http://technews.cn/2014/09/13/nfc-sticker/
    Redis 利用锁机制来防止缓存过期产生的惊群现象-转载自 http://my.oschina.net/u/1156660/blog/360552
    移动端H5页面的设计稿尺寸大小规范-转载自http://www.chinaz.com/design/2015/1103/465670.shtml
    服饰行业淘宝商城店铺首页设计报告-转载自http://bbs.paidai.com/topic/88363
    网页设计的标准尺寸
    hdu2099
  • 原文地址:https://www.cnblogs.com/X-JY/p/10943394.html
Copyright © 2020-2023  润新知