5.单表查询--select

3.1 user(),database();now();
select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
select database();
查看当前库
select now(); 
查看当前时间

select distinct 字段 from 表;
select emp_name,salary*12 from employee;
 #字段salary 参与了四则运算
select emp_name from 
select concat('一个字符串','字符串2',)
3.2 select 字段名 from 表名
select * from 表名;
select 字段名 from 表名;
select 字段名,字段名,字段名 from 表名;
3.3 distinct 去重
select distinct 字段 from 表;
 对查出来的字段进行去重
select distinct 字段,字段 from 表;
联合去重
select emp_name,salary*12 from 表;
字段 salary 参与了四则运算
3.4 concat 拼接
select concat(字段,'字符串2',字段) from 表
select concat(emp_name,':',salary)as info from employee;#as 新的名字
select concat(emp_name,':',salary) info from employee;# 和上面as的一样
select concat_ws('分隔符',字符串,字段1,字段2) info from employee;
select concat_ws('|','信息',emp_name,salary) info from employee;
3.5 concat语句
select(
    case
    when emp_name = 'alex' then
        concat(emp_name,'haha')
    when emp_name = 'jingliyang' then
        emp_name
    else
        concat(emp_name,'sb')
    end
    ) as new_name
from employee;
3.6 group by(分组,去重)
select * from 表 group by 字段名
    分组 group by
    根据某个重复率比较高的字段进行的
    这个字段有多少种可能就分成多少个组
        根据性别分组 : 男的一组 女的一组
        根据部门分组 : 销售一组 教学一组 ...
    去重
    一旦分组了就不能对具体某一条数据进行操作了
        永远都是考虑这组xxx
    group_concat : 只用来做最终的显示,不能作为中间结果操作其他数据
    select post,group_concat(emp_name) from employe group by post;
    拿到分组后的部门人的名字
    
练习
    求各部门薪资大于1w的人的个数
    select * from employee where salary >10000;
    select post,count(id) from employee where salary >10000 group by post;

有一个需要注意:

select post,min(salary) emp_name from employe group by post;(在min(salary)后面不加,直接就会重命名)
mysql> select post,min(salary),emp_name from employe group by post;
mysql> select post,max(salary),emp_name from employe group by post;
mysql> select post,avg(salary),emp_name from employe group by post;
+-----------------------------------------+-------------+----------+
| post                                    | min(salary) | emp_name |
+-----------------------------------------+-------------+----------+
| operation                               |    10000.13 | 张野     |
| sale                                    |     1000.37 | 歪歪     |
| teacher                                 |     2100.00 |fdfd    |
  外交大使              |                   7300.33      | egon     |
+-----------------------------------------+-------------+----------+
4 rows in set (0.01 sec)
在这里 emp_name 不会去显示正确的名字,只会按照下面的分组时的第一个人的名字去显示.
mysql> select * from employe group by post;
+----+----------+--------+-----+------------+-----------------------------------------+-
| id | emp_name | sex    | age | hire_date  | post                                    |
+----+----------+--------+-----+------------+-----------------------------------------+-
| 14 | 张野     | male   |  28 | 2016-03-11 | operation                               |
|  9 | 歪歪     | female |  48 | 2015-03-11 | sale                                    |
|  2 | alex     | male   |  78 | 2015-03-02 | teacher                                 |
|  1 | egon     | male   |  18 | 2017-03-01 | 老男孩驻沙河办事处外交大使              |
+----+----------+--------+-----+------------+-----------------------------------------+-
3.7 having过滤条件
    就是一个对组进行筛选的条件
    要部门人数大于3个人的部门 count(id)>3
    select post from employee group by post having count(id)>3;


    having的问题 不建议你用
        select id,emp_name,age from employee having age>20; # 查询的字段,必须前面有age
        select id,emp_name from employee group by age having age>20;
3.8 聚合函数
    99.99%的情况都是和分组一起用的
    如果没有和分组一起用,默认一整张表是一组
    count(id)  / count(*) 计数 :每个组对应几条数据
    max 求最大值: 这个组中某字段的最大值
    min 求最大值: 这个组中某字段的最小值
    avg 求平均值
    sum 求和值

    select min(hire_date) from employee
        求employee中所有人里最早入职的时间
    select min(hire_date) from employee group by post
        求每个部门中入职最早的时间
3.9 order by(排序)
order by 字段
    order by 字段 (默认从小到大 asc一样)
    order by 字段 asc 
    order by 字段 desc 从大到小
    
select * from 表名 order by 字段1,字段2 desc
    order by 字段1,字段2
    order by 字段 asc,字段2 desc
    order by 字段 desc,字段2 asc
    order by 字段 desc,字段2 desc
select * from book order by price asc;
select * from book order by price ;
select * from book order by price desc;
3.10 limit(显示分页)
    1.显示分页
        limit m,n
            表示从m+1开始,取n条
            limit 0,6 表示从1开始取6条
            limit 6,6 表示从7开始取6条
            limit 12,6 表示从13开始取6条
            limit 18,6 表示从19开始取6条
    2.取前n名
        limit n   m默认为0
        跟order by一起用
    limit n offset m :等同于 从m+1开始,取n条
    select * from book order by price limit 10;
3.11 单表mysql语句关键字执行的顺序
  1. from 表(先找到表)

  2. where 先从这张表中产查询的行

  3. group 分组

  4. having 对组过滤

  5. select 想要的列

  6. order by 排序

  7. limit 取一个区间

    select 想要的列 from 表
    where 先从这张表中查询的行
    group by 分组
    having 对组过滤
    order by 排序
    limit 取一个区间
    # 比如说 select name as n from 表 where n='alex' 这个就不可以,因为where先执行,还没有n呢

猜你喜欢

转载自www.cnblogs.com/pythonblogs/p/12105692.html