数据库----数据查询操作
Posted 夏芷雨涵梦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库----数据查询操作相关的知识,希望对你有一定的参考价值。
数据库查询操作(DQL 数据查询语言)
时间:2019.08.24 作者:夏晓林 内容:DQL数据查询
创建相关的表
CREATE TABLE stu (
sid CHAR(6),
sname VARCHAR(50),
age INT,
gender VARCHAR(50)
);
INSERT INTO stu VALUES('S_1001', 'liuYi', 35, 'male');
INSERT INTO stu VALUES('S_1002', 'chenEr', 15, 'female');
INSERT INTO stu VALUES('S_1003', 'zhangSan', 95, 'male');
INSERT INTO stu VALUES('S_1004', 'liSi', 65, 'female');
INSERT INTO stu VALUES('S_1005', 'wangWu', 55, 'male');
INSERT INTO stu VALUES('S_1006', 'zhaoLiu', 75, 'female');
INSERT INTO stu VALUES('S_1007', 'sunQi', 25, 'male');
INSERT INTO stu VALUES('S_1008', 'zhouBa', 45, 'female');
INSERT INTO stu VALUES('S_1009', 'wuJiu', 85, 'male');
INSERT INTO stu VALUES('S_1010', 'zhengShi', 5, 'female');
INSERT INTO stu VALUES('S_1011', 'xxx', NULL, NULL);
CREATE TABLE emp(
empno INT,
ename VARCHAR(50),
job VARCHAR(50),
mgr INT,
hiredate DATE,
sal DECIMAL(7,2),
comm DECIMAL(7,2),
deptno INT
);
INSERT INTO emp VALUES(7369,'SMITH','CLERK',7902,'1980-12-17',800,NULL,20);
INSERT INTO emp VALUES(7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30);
INSERT INTO emp VALUES(7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30);
INSERT INTO emp VALUES(7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20);
INSERT INTO emp VALUES(7654,'MARTIN','SALESMAN',7698,'1981-09-28',1250,1400,30);
INSERT INTO emp VALUES(7698,'BLAKE','MANAGER',7839,'1981-05-01',2850,NULL,30);
INSERT INTO emp VALUES(7782,'CLARK','MANAGER',7839,'1981-06-09',2450,NULL,10);
INSERT INTO emp VALUES(7788,'SCOTT','ANALYST',7566,'1987-04-19',3000,NULL,20);
INSERT INTO emp VALUES(7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10);
INSERT INTO emp VALUES(7844,'TURNER','SALESMAN',7698,'1981-09-08',1500,0,30);
INSERT INTO emp VALUES(7876,'ADAMS','CLERK',7788,'1987-05-23',1100,NULL,20);
INSERT INTO emp VALUES(7900,'JAMES','CLERK',7698,'1981-12-03',950,NULL,30);
INSERT INTO emp VALUES(7902,'FORD','ANALYST',7566,'1981-12-03',3000,NULL,20);
INSERT INTO emp VALUES(7934,'MILLER','CLERK',7782,'1982-01-23',1300,NULL,10);
CREATE TABLE dept(
deptno INT,
dname VARCHAR(14),
loc VARCHAR(13)
);
INSERT INTO dept VALUES(10, '财务部', 'beijing');
INSERT INTO dept VALUES(20, 'java开发部', 'tianjin');
INSERT INTO dept VALUES(30, '测试部', 'shanghai');
INSERT INTO dept VALUES(40, '销售部', 'shenzheng');
查询操作
#1 简单查询
#1.1 查询所有的列
SELECT * FROM stu;
#1.2 查询指定的列
SELECT sid,sname FROM stu;
#2 条件查询
#2.1 查询性别为女年龄小于50
SELECT * FROM stu WHERE gender='female' AND age<50;
#2.2 查询学号为1001或者姓名为lisi的记录
SELECT * FROM stu WHERE sid='s_1001' OR sname='lisi';
#2.3查询学号为S _ 1001 ,S _ 1002,S _ 1003的记录
SELECT * FROM stu WHERE sid='s_1001' OR sid='s_1002' OR sid='s_1003';
SELECT * FROM stu WHERE sid IN('s_1001','s_1002','s_1003');
#2.4查询学号不是S_1001,S_1002,S_1003的记录
SELECT * FROM stu WHERE sid NOT IN('s_1001','s_1002','s_1003');
#2.5查询年龄为null的记录,空判断用is
SELECT * FROM stu WHERE age IS NOT NULL;
SELECT * FROM stu WHERE NOT age IS NULL;
#2.6查询年龄在20到40之间的学生记录
SELECT * FROM stu WHERE age BETWEEN 20 AND 40;
#2.7查询性别非男的学生记录
SELECT * FROM stu WHERE gender != 'male';
SELECT * FROM stu WHERE gender <> 'male';
#2.8查询姓名不为null的学生记录
SELECT * FROM stu WHERE sname IS NOT NULL;
#3 模糊查询
#3.1查询姓名由3个字符构成的学生记录
SELECT * FROM stu WHERE sname LIKE '___';
#3.2查询姓名由5个字符构成,并且第5个字符为“i”的学生记录
SELECT * FROM stu WHERE sname LIKE '____i';
#3.3查询姓名以“z”开头的学生记录
SELECT * FROM stu WHERE sname LIKE 'z%';
#3.4查询姓名中第2个字符为“i”的学生记录
SELECT * FROM stu WHERE sname LIKE '_i';
#3.5查询姓名中包含“a”字符的学生记录
SELECT * FROM stu WHERE sname LIKE '%a%';
#4 字段控制查询
#4.1去除重复记录,去除重复工资
SELECT DISTINCT sal,comm FROM emp;
#4.2查看员工的月薪和佣金之和
SELECT sal,comm,sal+IFNULL(comm,0) FROM emp;
SELECT ename,job,CONCAT(ename,'的工作是',job) FROM emp;
SELECT ename,job,CONCAT(ename,'的工作是',job),'哈弗大学' AS 'university' FROM emp;
#4.3别名
SELECT sal,comm,sal+IFNULL(comm,0) AS total FROM emp;
#5 排序 升序asc 降序 desc
SELECT * FROM stu ORDER BY age ASC;
#5.1查询所有雇员,按月薪降序排序,如果月薪相同时,按编号升序排序
SELECT * FROM emp ORDER BY sal DESC ,empno ASC;
#6 聚合函数
# 6.1查询emp表中记录数:
SELECT COUNT(*) FROM emp;
SELECT COUNT(empno) FROM emp;
#6.2如果统计某一列个数,不包含null值
SELECT COUNT(comm) FROM emp;
#6.3查询emp表中月薪大于2500的人数:
SELECT COUNT(*) FROM emp WHERE sal>=2500;
#6.4统计月薪与佣金之和大于2500元的人数:
SELECT COUNT(*) FROM emp WHERE sal+IFNULL(comm,0)>=2500;
#6.5查询有佣金的人数,以及有领导的人数:
SELECT COUNT(comm),COUNT(mgr) FROM emp;
#sum和avg
#查询所有雇员月薪和:
SELECT SUM(sal) FROM emp;
#查询所有雇员月薪和,以及所有雇员佣金和:
SELECT SUM(sal),SUM(comm) FROM emp;
#查询所有雇员月薪+佣金和:
SELECT SUM(sal+IFNULL(comm,0)) FROM emp;
#统计所有员工平均工资:
SELECT AVG(sal) FROM emp;
#查询最高工资和最低工资:
SELECT MIN(sal),MAX(sal) FROM emp;
#7 分组
#7.1查询每个部门的部门编号和每个部门的工资和:
SELECT deptno,SUM(sal),COUNT(*) FROM emp GROUP BY deptno;
#7.2查询每个部门的部门编号以及每个部门工资大于1500的人数:
SELECT deptno,COUNT(*) FROM emp WHERE sal>=1500 GROUP BY deptno;
#Having
# 查询工资总和大于9000的部门编号以及工资和:
SELECT deptno,SUM(sal) FROM emp GROUP BY deptno HAVING SUM(sal)>9000;
#limit
#查询前5行记录,起始行从0开始
SELECT * FROM emp LIMIT 0,5;#第一个参数表示从哪个位置开始,后一个参数是查询的数据个数
#分页查询 每页5条记录
SELECT * FROM emp LIMIT 0,5;
SELECT * FROM emp LIMIT 5,5;
SELECT * FROM emp LIMIT 10,5;
总结:
查询语句书写顺序
where 是对原始数据的筛选,group By是把数据进行排序
having 是对分组后的数据筛选 order by 是排序 limit是限制
where group by order by limit 可以单独使用
having 只能用在group by的后面
查询语句执行顺序
from 表 where group by having order by select limit
以上是关于数据库----数据查询操作的主要内容,如果未能解决你的问题,请参考以下文章