一简单数据记录查询
1 select empto,ename,job from t_employee;
2select * from t_employee;
3避免重复数据查询 distinct
select distinct job from t_employee;
4四则运算数据查询
select ename,sal*12 as yearsalary from t_employee;
5 利用函数concat()来连接字符串
select concat(ename,'员工的年薪为:',sal*12) yearsalary from t_employee;
二条件数据记录查询
1select ename from t_employee where job ='clerk' && sal>12000;
2select ename from t_employee where job ='clerk' and sal>12000;
3select ename from t_employee where sal between 10000 and 20000;
4select ename from t_employee where comm is not null;
5select ename from t_employee where not comm is null;
6select ename from t_employee where empno=7878 or empno=9878 or empno=8878;
7select ename from t_employee where empno in (7878 ,9878 ,8878);
8select ename from t_employee where empno not in (7878 ,9878 ,8878);
9select ename from t_employee where not empno in (7878 ,9878 ,8878);
10 like 匹配部分值 '%' :通配符,匹配任意长度的字符串 '_':通配符,匹配单个字符
select ename from t_employee where ename like 'A%';
select ename from t_employee where not ename like '_A%';
三排序数据记录查询 group by 默认asc升序
1select * from t_employee order by sal ASC,
hiredate DESC
四限制数据记录查询数量 limit(分页系统)
1select * from t_employee where comm is null hiredate limit 2;
2select * from t_employee where comm is null order By hiredate limit 0,5;
3 select * from t_employee where comm is null order By hiredate limit 5,5; #从第六条开始显示,共显示5条记录
五统计函数和分组数据记录查询
count() \ avg() \ sum() \ max() \ min() 忽略null
1select count(comm) number from t_employee where not comm=0;
2select avg(comm) average from t_employee where not comm=0;
3select sum(sal) sumvalue from t_employee;
4select max(sal) maxvalue,min(sal) minvalue from t_employee;
5 分组查询,字段上要有重复的值才有意义
显示每个分组中的字段 group_concat()
1select deptno,group_concat(ename) enames from t_employee group by deptno;
| deptno | enames |
| 10 | miller,king,clerk |
| 20 | ford,adams,scott,jones |
| 30 | blake,martin,ward,allen,james |
2select deptno,hiredate from t_employee group by deptno,hiredate;
| deptno | hiredate |
| 10 | 1981-09-01 |
| 20 | 1983-03-05 |
| 20 | 1995-07-04 |
| 30 | 1987-03-25 |
3select deptno,hiredate,group_concat(ename) ename,count(ename)
from t_employee
group by deptno,hiredate;
4分组数据查询--实现having字句限定分组查询
对分组进行条件限制,决不能通过关键字where来实现,该关键字用来实现条件限制数据记录。having来是实现条件限制分组数据记录
select deptno,avg(sal) average,group_concat(ename) enames,count(ename) number
from t_employee
group by deptno
having avg(sal)>12000;
本文详细介绍了MySQL单表数据查询的多种方法,包括简单数据记录查询、条件数据记录查询、排序数据记录查询、限制数据记录查询数量以及统计函数和分组数据记录查询,还给出了相应的SQL语句示例。

被折叠的 条评论
为什么被折叠?



