SQL练习题

2019-01-19  本文已影响0人  luckee
t_book
t_booktype
t_price
SELECT * FROM t_book WHERE bookTypeId IN (SELECT id FROM t_booktype);

SELECT * FROM t_book WHERE price >=(SELECT price FROM t_price WHERE priceLevel=1);

#只有当后面有查询结果,也就是返回true的时候才会执行前面的查询
SELECT * FROM t_book WHERE EXISTS (SELECT * FROM t_booktype);

#大于等于任意一个即可,即大于等于最小值就可以
SELECT * FROM t_book WHERE price >= ANY(SELECT price FROM t_price);

#大于等于所有的,即大于等于最大值才可以
SELECT * FROM t_book WHERE price >= ALL(SELECT price FROM t_price);

别名

project_name(项目名称) progress_name(进度) date(日期)
项目1 申请 2019-1-1
项目2 申请 2019-1-5
项目1 预审 2019-1-10

使用SQL语句将其变成:

项目名称 申请 预审
项目1 2019-1-1 2019-1-10
项目2 2019-1-5
select s1.project_name as 项目名称, s1.date as 申请, s2.date as 预审 from
(
(select project_name, date from project_progress where progress_name = '申请') s1 
left join 
(select project_name, date from project_progress where progress_name = '预审') s2
on s1.project_name = s2.project_name
);

or

select s1.项目名称, 申请,  预审 from
(
(select project_name as 项目名称, date as 申请 from project_progress where progress_name = '申请') s1 
left join 
(select project_name as 项目名称, date as 预审 from project_progress where progress_name = '预审') s2
on s1.项目名称 = s2.项目名称
);
上一篇 下一篇

猜你喜欢

热点阅读