MySQL单表查询
Posted 蜗牛也是妞
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL单表查询相关的知识,希望对你有一定的参考价值。
1、单表查询
单表语法: select distinct 字段1,字段2,... from 表名 where 约束条件 group by 分组条件 having 过滤条件 order by 排序字段 limit n; #简单查询 select * from emp; select id,name from emp; # 去除重复 select distinct post from emp; # 四则运算 select name,salary*12 as annual_salary from emp; select name,salary*12 annual_salary from emp; # 定义显示格式 concat() 函数用于连接字符串 select concat(\'姓名: \',name,\' 年薪: \', salary*12) as annual_salary from employee; concat_ws() 第一个参数为分隔符 select concat_ws(\':\',name,salary*12) as annual_salary from employee; 结合CASE语句: select ( case when name = \'egon\' then name when name = \'alex\' then concat(name,\'_BIGSB\') else concat(name,\'_SB\') end ) as new_name from emp;
2、where
where字句中可以使用:
Ø比较运算符:> < >= <= <> !=between 80 and 100 值在10到20之间
Øin(80,90,100) 值是10或20或30
Ølike \'egon%\'
pattern可以是%或_,
%表示任意多字符
_表示一个字符
逻辑运算符:在多个条件直接可以使用逻辑运算符 and or not
#1:单条件查询 SELECT name FROM employee WHERE post=\'sale\'; #2:多条件查询 SELECT name,salary FROM employee WHERE post=\'teacher\' AND salary>10000; #3:关键字BETWEEN AND SELECT name,salary FROM employee WHERE salary BETWEEN 10000 AND 20000; SELECT name,salary FROM employee WHERE salary NOT BETWEEN 10000 AND 20000; #4:关键字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 ps: 执行 update employee set post_comment=\'\' where id=2; 再用上条查看,就会有结果了 #5:关键字IN集合查询 SELECT name,salary FROM employee WHERE salary=3000 OR salary=3500 OR salary=4000 OR salary=9000 ; SELECT name,salary FROM employee WHERE salary IN (3000,3500,4000,9000) ; SELECT name,salary FROM employee WHERE salary NOT IN (3000,3500,4000,9000) ; #6:关键字LIKE模糊查询 通配符’%’ SELECT * FROM employee WHERE name LIKE \'eg%\'; 通配符’_’ SELECT * FROM employee WHERE name LIKE \'al__\';
3、goup by
单独使用GROUP BY关键字分组 SELECT post FROM employee GROUP BY post; 注意:我们按照post字段分组,那么select查询的字段只能是post,想要获取组内的其他相关信息,需要借助函数 GROUP BY关键字和GROUP_CONCAT()函数一起使用 SELECT post,GROUP_CONCAT(name) FROM employee GROUP BY post;#按照岗位分组,并查看组内成员名 SELECT post,GROUP_CONCAT(name) as emp_members FROM employee GROUP BY post; GROUP BY与聚合函数一起使用 select post,count(id) as count from employee group by post;#按照岗位分组,并查看每个组有多少人
#强调:聚合函数聚合的是组的内容,若是没有分组,则默认一组 示例: SELECT COUNT(*) FROM employee; SELECT COUNT(*) FROM employee WHERE depart_id=1; SELECT MAX(salary) FROM employee; SELECT MIN(salary) FROM employee; SELECT AVG(salary) FROM employee; SELECT SUM(salary) FROM employee; SELECT SUM(salary) FROM employee WHERE depart_id=3;
4、having
#执行优先级从高到低:where > group by > having #1. Where 发生在分组group by之前,因而Where中可以有任意字段,但是绝对不能使用聚合函数。 #2. Having发生在分组group by之后,因而Having中可以使用分组的字段,无法直接取到其他字段,可以使用聚合函数 select post,avg(salary) from emp group by post having avg(salary) > 20000;
5、order by排序
select * from emp order by age asc; # 默认升序,从小到大 select * from emp order by age desc; #从大到小 #按多列排序:先按照age排序,如果年纪相同,则按照id排序 select * from emp order by age asc,id desc; select post,avg(salary) from emp group by post order by avg(salary);
6、limit n
#默认初始位置为0 SELECT * FROM employee ORDER BY salary DESC LIMIT 3; #从第0开始,即先查询出第一条,然后包含这一条在内往后查5条 SELECT * FROM employee ORDER BY salary DESC LIMIT 0,5; #从第5开始,即先查询出第6条,然后包含这一条在内往后查5条 SELECT * FROM employee ORDER BY salary DESC LIMIT 5,5;
7、正则
SELECT * FROM employee WHERE name REGEXP \'^ale\'; SELECT * FROM employee WHERE name REGEXP \'on$\'; SELECT * FROM employee WHERE name REGEXP \'m{2}\'; 小结:对字符串匹配的方式 WHERE name = \'egon\'; WHERE name LIKE \'yua%\'; WHERE name REGEXP \'on$\'; #查看所有员工中名字是jin开头,n或者g结果的员工信息 select * from employee where name regexp \'^jin.*[gn]$\';
以上是关于MySQL单表查询的主要内容,如果未能解决你的问题,请参考以下文章