zoukankan      html  css  js  c++  java
  • 第八节 查找命令select

      1 -- 查询所有字段
      2     SELECT * FROM t_student;
      3 
      4 -- 查询指定字段
      5     SELECT name,id FROM t_student;
      6 
      7 --使用as给字段起别名
      8     SELECT name AS 姓名, age AS年龄 FROM t_student;
      9     SELECT t_student .name, t_student .age FROM t_student;
     10     SELECT s.name, s.age FROM t_student AS s;
     11 -- SELECT t_student .name, t_student .age FROM t_student AS s; 错误语句
     12 
     13 -- 去重
     14     SELECT DISTINCT gender FROM t_student;
     15 
     16 -- 条件查询
     17     -- 普通条件查询
     18         SELECT * FROM t_studentWHERE age>18;
     19     -- 比较运算符查询
     20         SELECT * FROM  t_student WHERE not (age>18 and age <30);
     21         SELECT * FROM  t_student WHERE not age>18 and age <30;
     22     -- 模糊查询
     23         -- like,% 替换一个或者多个,_替换一个
     24             -- 查询姓名中以‘小开头的’名字
     25             SELECT * FROM t_student WHERE name LIKE '小%';
     26             -- 查询姓名中有‘小的’所有名字
     27             SELECT * FROM t_student WHERE name LIKE '%小%';
     28             -- 查询两个字的名字
     29             SELECT * FROM t_student WHERE name LIKE '__';
     30         -- rlike 正则
     31             -- 以周开头的名字
     32             SELECT * FROM t_student WHERE name RLIKE '^周.*';
     33             -- 以周开头以伦结尾的名字
     34             SELECT * FROM t_student WHERE name RLIKE '^周.*伦$';
     35 
     36 -- 范围查询
     37     -- in
     38         -- 查询年纪为18、34的姓名
     39         SELECT * FROM t_student WHERE age IN (18,34);
     40     -- not in
     41         -- 查询年龄不为18、34的姓名
     42         SELECT * FROM t_student WHERE age NOT IN (18,64);
     43     -- between...and...
     44         -- 查询年龄在18到34岁的姓名
     45         SELECT * FROM t_student WHERE age BETWEEN 18 AND 34;
     46     -- not between...and...
     47         -- 查询年龄不在18和34之间的姓名
     48         SELECT * FROM t_student WHERE age NOT BETWEEN 18 AND 34;
     49         ---- not between...and...是一种固定语句,不是between...and...取反的意思。因此NOT (BETWEEN 18 AND 34)是错误的
     50         SELECT * FROM t_student WHERE NOT age BETWEEN 18 AND 34;
     51         -- 这才是取反操作
     52 
     53 -- 空判断
     54     -- 判空 IS NULL
     55     SELECT * FROM t_student WHERE height IS NULL;
     56     -- 判非空 IS NOT NULL
     57     SELECT * FROM t_student WHERE height IS NOT NULL;
     58 
     59 -- 排序
     60     --ORDER BY 字段
     61         -- ASC从小到大排列,升序
     62             -- 查询年龄18-34之间的男性,并年龄升序排列
     63             SELECT * FROM t_student WHERE age BETWEEN 18 AND 34 AND gender=1 ORDER BY age ASC;
     64         -- DESC从大到小排列,降序
     65             -- 查询年龄在18-34之间的女性,身高降序
     66             SELECT * FROM t_student WHERE age BETWEEN 18 AND 34 AND gender=2 ORDER BY height DESC;
     67         -- ORDER BY 多个字段
     68             SELECT * FROM t_student WHERE age BETWEEN 18 AND 34 AND gender=2 ORDER BY height ASC, id DESC;
     69 
     70 -- 聚合函数
     71     -- 总数,count
     72         -- 查询男性有多少人,女性有多少人
     73         SELECT COUNT(name) FROM t_student WHERE gender=1;
     74         SELECT COUNT(*) AS 女性人数, avg(age) AS 平均年龄 FROM t_student WHERE gender=1;
     75     -- 最大值 max
     76         --查询最大的年龄
     77         SELECT MAX(age) FROM t_student ;
     78     -- 最小值 min
     79         -- 查询女性的最高身高
     80         SELECT MAX(height) FROM t_student WHERE gender=2;
     81     -- 平均值 avg
     82         -- 计算平均年龄
     83         SELECT AVG(age) FROM t_student ;
     84     -- 四舍五入 round(123.23, 1)保留一位小数
     85         -- 计算所有人的平均年龄,保留两位小数
     86         SELECT ROUND(AVG(age), 2) FROM t_student ;
     87 
     88 -- 分组
     89     -- gruop by
     90         -- 按照性别分组,查询所有性别
     91         SELECT gender FROM t_student GROUP BY gender;
     92         -- 计算每种性别中的人数
     93         SELECT gender,COUNT(NAME) FROM t_student GROUP BY gender;
     94         -- 计算男性的人数
     95         SELECT gender,COUNT(NAME) FROM t_student WHERE gender=1 GROUP BY gender;
     96     -- group_concat(...)
     97         --查询同种性别中的姓名
     98         SELECT gender,group_concat(name) FROM t_student WHERE gender=1 GROUP BY gender;
     99         SELECT gender,group_concat(name, id, age) FROM t_student WHERE gender=1 GROUP BY gender;
    100         SELECT gender,group_concat(name,' ',id, '', age) FROM t_student WHERE gender=1 GROUP BY gender;
    101     -- having
    102         -- 查询平均年龄超过30岁的性别、姓名
    103         SELECT gender, group_concat(NAME), AVG(age) FROM t_student GROUP BY gender having AVG(age)>30;
    104         -- where是对原表进行过滤,having是对分组后每个组的数据进行判断
    105         -- 查询每种性别中的人数多于两个的信息
    106         SELECT gender, group_concat(NAME) FROM t_student GROUP BY gender having COUNT(NAME)>2;
    107 
    108 -- 分页
    109     -- limit start,count
    110         -- start表示从第几个开始取(0是第一个),count是取几个
    111         SELECT * FROM t_student LIMIT 1, 5;
    112     -- 限制查询出来的个数
    113     SELECT * FROM t_student WHERE gender=1 LIMIT 2;
    114     -- 查询前5个数据
    115     SELECT * FROM t_student LIMIT 0, 5;
    116     -- 查询 ID 6-10(包含)的数据
    117     SELECT * FROM t_student LIMIT 5,5;
    118     -- 每页显示2个,第1个页面
    119     SELECT * FROM t_student LIMIT 0, 2;
    120     -- 每页显示2个,第2个页面
    121     SELECT * FROM t_student LIMIT 2, 2;
    122     -- 每页显示2个,第3个页面
    123     SELECT * FROM t_student LIMIT 4, 2;
    124     -- 每页显示2个,第4个页面
    125     SELECT * FROM t_student LIMIT 6, 2;
    126     -- 每页显示2个,显示第6页信息,按照年龄从小到大排序
    127     SELECT * FROM t_student LIMIT 10,2 ORDER BY age;
    128     -- LIMIT 只能放在最后
    129 -- 连接查询
    130     -- inner join ...on 显示两者共有,即交集
    131      SELECT S.*,C.NAME FROM t_student AS S INNER JOIN CLASSES AS C ON S.cls_id = C.id;
    132     -- left join ...on 谁在左边以谁为主,如果左边在右边没有对应的值,就显示null
    133      SELECT * FROM t_student AS S LEFT JOIN CLASSES AS C ON S.cls_id=C.id;
    134     -- right join ...on 谁在右边以谁为主,如果右边在左边没有对应的值,就显示null
    135     SELECT * FROM t_student AS S RIGHT JOIN CLASSES AS C ON S.cls_id=C.id;
    136     -- 查询没有对应班级的学生
    137     SELECT * FROM t_student AS S left JOIN CLASSES AS C ON S.cls_id=C.id having c.id is null;
    138      -- 此处having改成where也行,但不建议,原则上原表过滤用where,查询结果过滤用having
    139  
    140 -- 自关联
    141 
    142  -- 子查询(自关联的表也可以用子查询得到相同的结果)
    143      select * from areas where pid = (select aid from areas where atitle = '河北省')
  • 相关阅读:
    导入模块
    Windows x86-64下python的三个版本
    ubuntu 16.04 添加网卡
    重启rsyncd
    docker时区
    git回滚
    impdp and docker install oracleXE
    Oracle 把一个用户所有表的读权限授予另一个用户
    zabbix web监控
    WebStorm license server
  • 原文地址:https://www.cnblogs.com/kogmaw/p/12405788.html
Copyright © 2011-2022 走看看