数据库——连接查询(子查询)

来源:互联网 发布:带数字的域名 编辑:程序博客网 时间:2024/05/31 11:03

准备4张表:


# 子查询
-- any的用法
select num2 from t2 where num2 > any(select num1 from t1);

运行结果:

-- exists的使用(返回True,外层sql语句执行;返回False,外层sql不执行)
select * from score where exists (select name from student where s_id=20);#返回False

运行结果:

select * from score where exists (select name from student where s_id=101);#返回True

运行结果:

详解exists:

-- in的使用
-- select * from t1;# t1表
-- select * from t2;# t2表
-- select num1 from t1 where num1 in(select num2 from t2);

运行结果:


-- not in的使用(与in的用法相反)
-- select num1 from t1 where num1 not in(select num2 from t2);

运行结果:

-- union all的使用(使用union all包含重复的行;union从查询结果集中去除重复的行;)
select s_id,name from student where s_id>101 union all select s_id,name from student where s_id>103;

运行结果:


-- union的使用
select s_id,name from student where s_id>101 union select s_id,name from student where s_id>103;

运行结果:


select s_id,name from student where s_id>101 or s_id>103;

运行结果:

对比union和union-all的区别:



阅读全文
0 0
原创粉丝点击