select column from tb1,tb2 where 条件;
关键字:[inner]? join on
显示内连接与外连接的不同是新增的关键字,inner join 以及 使用on 替换了where?
select column from tb1 [inner] join tb2 on 条件;
关键字:left [outer]? join on
完全包含左表的数据
select column from tb1 left [outer] join tb2 on 条件;
同理
关键字:right [outer]? join on
完全包含右表的数据。
左右表连接可以相互替换。
select column from tb1 right [outer] join tb2 on 条件;
内连接与外连接的区别就是,内连接只会将两个表的共同的数据展现出来。
关键字:join on
使用场景,就是一个表中的外键也关联着表的主键;
select column from tb asA join tb asB on 条件;
关键字?:union 、 union all
把多次查询的结果合并起来
union 会去重。为了性能,推荐使用union all,不去重。
select column from tb where 条件;
union all
select column from tb where 条件;
嵌套select语句
外部语句可以是增删改查中的任意一个语句。
select * from tb_name where column = (select column from tb);
关键字:in、not in 、any(some)、all
select * from tb where name ="" and age > all(select age from tb);
select * from tb where id in (select id from tb where use_tb_id = "");
关键字:in、not in 、<>=
select * from tb where (age,hobby) = (select age,hobby from tb where name = "");
关键字:in
select * from tb where (age,hobby) in (select age,hobby from tb where name = "" or name = "");