常用语句查询

-- # 模糊查询,_ 匹配一个字符,% 匹配多个字符

-- # 下面的意思是:查询name字段第二个字符为s的所有项

-- select * from test where name like '_s%';

# 把字段name 重复的内容去除

-- select DISTINCT name from test;

-- # 多个字段的数学操作,必须是可以计算的类型,如整形,浮点型

-- select *,age+score from test;

-- # 给某个字段空值取某个默认值,取别名

-- SELECT *,(IFNULL(age,0) + IFNULL(score,0)) as total FROM test;

-- # 排序,默认asc升序排序,desc为降序排序,可以设置多个自动

-- SELECT * FROM test ORDER BY age desc, id desc;

-- # 或者下面的写法

-- SELECT * FROM test ORDER BY age, id desc;

-- # 聚合函数,count()不为NULL的所有值,max(),min(),sum(),avg()

-- SELECT count(age),count(score) FROM test;

-- # 分组函数group by类似归类

-- # 下面是把每个部门的人通过group_concat(name)查询出来

-- SELECT party,group_concat(name) FROM Test GROUP BY party

-- # 查询每个部门的薪水和显示他们的名字

-- SELECT sum(salary),party,group_concat(name) FROM test GROUP BY party;

-- # 在使用分组时,select 后面的字段一般都出现在GROUP BY 的后面,

-- # 否则语句查询起来就没有多大意义

-- SELECT name,party FROM test GROUP BY party,name

-- # GROUP BY + 聚合函数

-- SELECT party,group_concat(salary),sum(salary),count(*) FROM test GROUP BY party;

-- # 查询每个部门的名称并且工资大于1500的人数

-- SELECT party,count(*) from test where salary > 1500 GROUP BY party

-- # GROUP BY + having (having 相当于 where ,只能配合group by 中使用

-- # 查询工资总和大于等于5000的部门名称

-- select party,group_concat(salary),sum(salary) as total from test group by party having total > 5000

-- # HAVING 和 where 的区别

-- 1.Having 在group by 分组后进行过滤

-- 2.where 是在分组前对数据进行过滤

-- 3.having 后面可以使用分组函数(统计函数)

-- 4.where 后面不可以使用分组函数

-- 5.where 是对分组前记录的条件,如果某行记录没有满足WHERE子句的条件,

-- 那么这条记录就不会参加分组,而having是对分组后的数据进行过滤

# 查询工资大于2000,工资总和大于4000的部门名称以及工资和, 按工资总和降序排序

-- select party,GROUP_CONCAT(salary),sum(salary) as total from test where salary > 2000 GROUP BY party HAVING total > 4000 ORDER BY total desc

-- # 书写顺序 select from where GROUP BY having order by limit

-- # 执行顺序 from where GROUP BY having select order by limit

-- # limit 从哪一行开始查,总共要查几行

-- # limit 参数1,参数2(参数1是从第几条开始查,参数2是要查询几条)

-- # limit 角标从0开始

-- # 用在分页的情况中:

-- # 如: int curPage = 1 (当前页); int pageSize = 10; (每页10条数据)

-- # 第一页为:0,10; 第二页为:10,10,第二页为:20,10

-- # 写成变量的形式:limit (curPage-1) * pageSize, pageSize

# 数据的完整性:

-- 1.实体完整性:表中的一行(一条记录)代表一个实体; 作用:标识每一行数据不重复,行级约束

-- 1.主键约束:每个表要有一个主键

-- 2.唯一约束:指定列的数据不能重复,可以为空值;格式(create table tablename(字段名1 数据类型 UNIQUE);

-- 3.自动增长列

-- 2.域完整性:单元格的约束

-- 1.数据类型

-- 2.not null

-- 3.default 默认值

-- 3.引用完整性:表与表之间的对应关系:

# 添加约束:名字为score_student,score表的外键sid,参考的是student表中的字段id

-- alter table score add constraint score_student FOREIGN KEY(sid) REFERENCES student(id);

-- alter table score add constraint score_course Foreign key(cid) REFERENCES course(id);

# 多表查询:

# 合并结果集:union 和 union all 多个表查询同时返回结果集,union 能去重复的数据; 要保证列数量,列类型相同

-- create table a(name varchar(20), score int);

-- create table b(name varchar(20), score int);

-- insert into a values('a', 10),('b',20),('c', 30);

-- insert into b values('a', 10),('b',20),('d', 30);

# union and union all 查询

-- select * from a union select * from b;

-- select * from a union all select * from b;

# 笛卡尔现象:

# 假设有2个集合:A = {a,b}, B = {0,1,2}

# 则两个集合的笛卡尔积为:{(a,0),(a,1),(a,2),(b,0),(b,1),(b,2)}

# 可以扩展到多个集合的情况

# 为了不出现笛卡尔现象,得到的数据正确:在查询的时候把主键和外键保持一致

# 原理:逐行扫描,相等的留下,不相等的全不要;

-- select * from student,score where student.id = score.sid;

# 连接查询:内连接(等值连接,自然连接,非等值连接);外连接(左连接,右连接,多表连接)

# 内连接查询(和select * from student,score where student.id = score.sid)的效果一样,还可以加入where条件

# inner 可以省略

-- select * from student inner join score on student.id = score.sid where score > 80;

# 左连接就是把两表满足条件相同的数据查出来,如果左边表当中有不相等的数据,也把左边表当中的数据查出来;

# 同理右连接也是一样;

# outer 可以省略

-- select * from student left OUTER join score on student.id = score.sid

# 多表查询:student, score , course 3个表,查询学生的所有科目的成绩;

# 99连接法:

-- select student.id,student.name,score.score,course.name from student,score,course

-- where student.id = score.sid and score.cid = course.id;

# 内连接查询:

-- select student.id,student.name,score.score,course.name from student

-- INNER JOIN score ON student.id = score.sid

-- INNER JOIN course ON score.cid = course.id

# 自然连接:2个表必须有相同的列名和相同的类型;

-- SELECT * from student NATural Join score;

# 子查询:一个select 语句中包含另外一个完整的select语句,或2个以上的select,那么就是子查询、

# 子查询出现的位置: where后,把select 查询出的结果作为另外一个select的条件值;

# from 后,把查询出的结果当作一个新表;

# 查询与唐僧同一个部门的员工,记得子查询要加一个括号

-- select * from emp where depton = (select depton from emp where ename = '唐僧')

# 查询部门编号为20的所有员工薪水大于16000的员工;

-- select * from (select ename,salary,depton from emp where depton = 20) as temp where temp.salary > 16000;

# 查询工资高于猪八戒的员工

-- select * from emp where salary > (select salary from emp where ename = '猪八戒')

# 查询工作和薪水和猪八戒相同的员工

-- select ename,salary from emp where (job,salary) in (select job,salary from emp where ename ='猪八戒')

# 或者用下面的查询,多表查询:

-- select e.ename,e.salary from emp as e, (select job,salary from emp where ename ='猪八戒') as res where e.job = res.job and e.salary = res.salary;

# 查询有2个员工的上级信息, 下面的in 也可以换成 =

-- select * from emp where empno in (select mgr from emp group by mgr HAVING count(mgr) > 2);

# 自连接

# 求1003员工编号,姓名,经理编号和经理名称姓名

select e1.empno,e1.ename,e2.empno,e2.ename from emp e1, emp e2 where e1.mgr = e2.empno and e1.empno = 1003

猜你喜欢

转载自blog.csdn.net/book_longker/article/details/83153833