SQL join, union, union all, 完整的select语句

本文详细解析了SQL中各种连接方式(如笛卡尔积、左连接、右连接等)的特点及应用场景,并介绍了关联子查询的使用方法。此外,还对比了union与union all的区别,帮助读者更好地理解和掌握SQL查询技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://www.cnblogs.com/lijingran/p/9001302.html

                               where        on

笛                          ok               no

corss join            ok               ok     执行计划一样

inner join             ok                ok    执行计划一样

left     join            no                 ok

right  join             no                 ok

full     join             no                 ok

 

1.笛卡尔积

select A.clue_id as A_clue_id,  A.cooperate_days A_days, B.clue_id as B_clue_id, B.cooperate_days B_days from

clue_pri_sea A, clue_source B where A.clue_id in (3328, 3584) and A.clue_id=B.clue_id;

 

2.左连接  left  join 

 

select * from clue_pri_sea  left join clue_source on clue_pri_sea.clue_id=clue_source.clue_id  where clue_pri_sea.clue_id=1\G

 

 

3.右连接  right  join

select * from clue_pri_sea  right join clue_source on clue_pri_sea.clue_id=clue_source.clue_id  where clue_pri_sea.clue_id=1\G

 

 

4. union  和 union all

 

union操作符用于合并两个或者多个select语句的结果集。

要求:union的两边的表,必须拥有相同数量的列,顺序也要一样,列数据类型也要一样。

另外,结果集的列名和第一个select语句列名是一样的。

union命令会将相同的行合并成一个; union all 不做合并。

 

 

 

 

5.完整的select语句

 

select [ALL | distinct] field1, field2 as alias from xxxTable where   group by  having order by

 

 

关联子查询

SELECT * from film WHERE film_id in (SELECT film_id FROM film_actor WHERE actor_id=1);

 

 

MySQL resolves all joins using a nested-loop join method. This means that MySQL reads a row from the first table, and then finds a matching row in the second table, the third table, and so on. When all tables are processed, MySQL outputs the selected columns and backtracks through the table list until a table is found for which there are more matching rows. The next row is read from this table and the process continues with the next table.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值