mysql数据库关联有什么用_mysql数据库关联查询【lert join】常见使用

本文介绍了MySQL数据库中不同类型的关联查询,包括JOIN、LEFT JOIN、RIGHT JOIN、INNER JOIN以及如何获取交集、去交集等。通过实例展示了各种连接方式的使用,帮助理解数据库关联查询的基本概念。
摘要由CSDN通过智能技术生成

一,关键词:

1) left(连接类型)   join  on(条件)

二,常见连接:

1)join(获取的是一个笛卡尔积)、

select * from t_table1  join t_table2 ;

2)左连接 (两表关联,在匹配条件之后,左表保留全部,也就是包含没有匹配到的字段)

select * from t_table1  left join t_table2 on t_table1.id = t_table2 .id;

3)右连接(两表关联,在匹配条件之后,右表保留全部,包含没有匹配到的字段)

select * from t_table1  right join t_table2 on t_table1.id = t_table2 .id;

4)内连接 (获取交集 )

select * from t_table1  inner join t_table2  on t_table1.id = t_table1  .id;

5)只查左表内容(两表关联,查询左表独有的数据)

select * from t_table1  left join t_table2  on t_table1.id = t_table2  .id where t_table2.id is null;

6)只查右表内容(两表关联,查询右表独有的数据)

select * from t_table1  left join t_table2  on t_table1.id = t_table2  .id where t_table1.id is null;

7)全连接

select * from t_table1  left join t_table2  on t_table1 .id = t_table2.id

union

select * from t_table1  right join t_table2  on t_table1 .id = t_table2.id;

8)去交集

select * from t_table1  left join t_table2  on t_table1.id = t_table2  .id where t_table2  .id is null

union

select * from t_table1  right join t_table2  on t_table1.id = t_table2  .id where t_table1  .id is null;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值