数据库——单表查询语法:语法顺序与执行顺序

本文详细介绍了数据库单表查询的语法顺序,包括SELECT四则运算和自定义显示、WHERE约束、GROUP BY及HAVING过滤、ORDER BY排序、LIMIT限制查询数量以及正则查询。重点讲解了每个步骤的执行优先级,如WHERE在GROUP BY之前,HAVING在GROUP BY之后,以及ORDER BY和LIMIT的使用方法。

1、单表查询语法优先级,从上往下

执行顺序
写查询语句时按优先级思考!


from 库,表 先找到文件夹

where拿着where指定的约束条件,去文件/表中取出一条条记录

group by 分组条件写到where之后,如果没有group by,则整体作为一组

having 过滤

select 选择显示的字段,!!!逗号分隔,有几个逗号显示几列!

distinct 去重

order by 排序字段

limit n;限制结果的显示条数

语法顺序:

		select distinct 字段1,字段2,字段3 from 库.表 
			where 条件
			group by 分组条件
			having 过滤
			order by 排序字段
			limit n;

2、select 四则运算和自定义显示格式

#通过四则运算查询,
    SELECT name, salary*12 FROM employee;
    SELECT name, salary*12 AS Annual_salary FROM employee;
    SELECT name, salary*12 Annual_salary FROM employee;
#定义显示格式
   CONCAT() 函数用于连接字符串
   
   SELECT CONCAT('姓名: ',name,'  年薪: ', salary*12)  AS Annual_salary 
   FROM employee;
   
   select concat('姓名:',name,'  性别:',sex) as info,concat('年薪:',salary*12) as salary from t1;
   每一条内容都是这样的格式。

   CONCAT_WS() 第一个参数为分隔符
   SELECT CONCAT_WS(':',name,salary*12)  AS Annual_salary 
   FROM employee;

as可以省略,起别名:

mysql> select count(sid) '学生总数' from student;
+--------------+
| 学生总数     |
+--------------+
|           19 |
+--------------+

一个逗号是一列,看下面对比:

mysql> select concat('姓名:',sname,'  ',      '性别:',gender)from student;
+-----------------------------------------------------+
| concat('姓名:',sname,'  ',      '性别:',gender)     |
+-----------------------------------------------------+
| 姓名:乔丹  性别:女                                  |
| 姓名:艾弗森  性别:女                                |
| 姓名:科比  性别:女                                  |
| 姓名:奥尼尔  性别:男                                |
| 姓名:姚明  性别:男                                  |
| 姓名:麦迪  性别:男                                  |
| 姓名:斯科拉  性别:男                                |
| 姓名:詹姆斯  性别:男                                |
| 姓名:韦德  性别:女                                  |
| 姓名:费舍尔  性别:男                                |
| 姓名:保罗  性别:男                                  |
| 姓名:邓肯  性别:男                                  |
| 姓名:吉诺比利  性别:女                              |
| 姓名:罗斯  性别:女                                  |
| 姓名:霍华德  性别:女                                |
| 姓名:梅西  性别:男                                  |
| 姓名:刘翔  性别:男                                  |
| 姓名:张三  性别:男                                  |
| 姓名:张四  性别:女                                  |
+-----------------------------------------------------+
mysql> select concat('姓名',sname),concat('性别',gender) from student;
+------------------------+-------------------------+
| concat('姓名',sname)   | concat('性别',gender)   |
+------------------------+-------------------------+
| 姓名乔丹               | 性别女                  |
| 姓名艾弗森             | 性别女                  |
| 姓名科比               | 性别女                  |
| 姓名奥尼尔             | 性别男                  |
| 姓名姚明               | 性别男                  |
| 姓名麦迪               | 性别男                  |
| 姓名斯科拉             | 性别男                  |
| 姓名詹姆斯             | 性别男                  |
| 姓名韦德               | 性别女                  |
| 姓名费舍尔             | 性别男                  |
| 姓名保罗               | 性别男                  |
| 姓名邓肯               | 性别男                  |
| 姓名吉诺比利           | 性别女                  |
| 姓名罗斯               | 性别女                  |
| 姓名霍华德             | 性别女                  |
| 姓名梅西               | 性别男                  |
| 姓名刘翔               | 性别男                  |
| 姓名张三               | 性别男                  |
| 姓名张四               | 性别女                  |
+------------------------+-------------------------+

3、WHERE约束

  • 比较运算符:><>= <= !=
  • between and 在 之间
  • in (10,20,30,40)或的关系
  • like ‘e%’,可以是%或_,%表示任意多字符,_表示一个字符
  • 逻辑运算符:在多个条件直接可以使用逻辑运算符 and or not

示例

select name,age from employee where post = 'teacher';
select name,age from employee where post='teacher' and age > 30; 
select name,age,salary from employee where post='teacher' and salary between 9000 and 10000;
select * from employee where post_comment is not null;
select name,age,salary from employee where post='teacher' and salary in (10000,9000,30000);
select name,age,salary from employee where post='teacher' and salary not in (10000,9000,30000);
select name,salary*12 from employee where post='teacher' and name like 'jin%';

注意:

关键字IS NULL(判断某个字段是否为NULL,不能用等号,需要用IS)
    SELECT name,post_comment FROM employee 
        WHERE post_comment IS NULL;

    SELECT name,post_comment FROM employee 
        WHERE post_comment IS NOT NULL;

    SELECT name,post_comment FROM employee 
        WHERE post_comment=''; 注意''是空字符串,不是null
#:关键字LIKE模糊查询
    通配符’%’
    SELECT * FROM employee 
            WHERE name LIKE 'eg%';

    通配符’_’
    SELECT * FROM employee 
            WHERE name LIKE 'al__';

4、GROUP BY

分组指的是:将所有记录按照某个相同字段进行归类,比如针对员工信息表的职位分组,或者按照性别进行分组等。
目的:以组为单位进行统计,结合聚合函数用。

  • 分组发生在where之后,即分组是基于where之后得到的记录而进行的
  • 分组完毕后,比如group by post,只能查看post字段,如果想查看组内信息,需要借助于聚合函数。
  • 如果显示了其他信息,需要设置严格分组模式:
    set global sql_mode="ONLY_FULL_GROUP_BY";
    重登,再查看严格模式只能查看分组字段,select*group by就会报错
    不设置严格模式,select*group by可以显示每组第一条记录的全部信息,但是没意义。

示例:
部门薪水表:每个部门一组看最大,最小,平均薪水等。

select post,max(salary) as emp_count from employee group by post;
select post,min(salary) as emp_count from employee group by post;
select post,avg(salary) as emp_count from employee group by post;
select post,sum(age) as emp_count from employee group by post;

group_concat

按照岗位分组,并查看组内成员名

select post,group_concat(name) from employee group by post;

一个组内所有员工名列举出来,组别,后面是紧跟着每一组所有的姓名,总共行数是组类目数。

聚合函数

聚合函数聚合的是组的内容,若是按优先级顺序执行到select,没有group by,则默认一组,select可以用聚合函数。

max 最大值
min 最小值
avg 平均值
sum 求和
count 计数
    SELECT MAX(salary) FROM employee;
    SELECT MIN(salary) FROM employee;

5、HAVING过滤

HAVING与WHERE不一样的地方在于

  • 执行优先级从高到低:where > group by > having

  • having与聚合函数一起用

  • Where 发生在分组group by之前,分组之前不能用聚合函数。因而Where中可以有任意字段,但是绝对不能使用聚合函数。

  • Having发生在分组group by之后,因而Having中可以使用分组的字段,无法直接取到其他字段,使用聚合函数

正常:
select * from emp where salary > 100000; 

报错,没有分组:
select * from emp having salary > 100000; 

#报错,分组后无法直接取到salary字段
select post,group_concat(name) from emp group by post having salary > 10000;

示例:
查询各岗位平均薪资大于8000的岗位名、平均工资

select post,avg(salary) from employee group by post having avg(salary) > 8000;

6、ORDER BY

排序,不指定就按id升序排序。

  • asc 升序,默认
  • desc 降序
select * from employee order by age asc,id desc;
 #先按照age升序排,如果age相同则按照id降序排

7、限制查询的记录数:LIMIT

  • limit 3 限制显示3条
  • LIMIT 0,5; #从第0开始,即先查询出第一条,然后包含这一条在内往后查5条
取出工资最高的岗位所有信息

select * from employee order by salary desc limit 1;
 分页显示,每页5条
select * from employee limit 0,5;
select * from employee limit 5,5;
select * from employee limit 10,5;
select * from employee limit 15,5;

8、正则查询

regexp 后跟正则表达式字符串

以名字开头的
不用正则:
select * from employee where name like 'jin%';

用正则:
以jin开头以g或n结尾
select * from employee where name regexp '^jin';
select * from employee where name regexp '^jin.*[g|n]$';
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值