码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2017-11-25 18:32:22      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:microsoft   font   内连接   sql数据库   bsp   soft   保留   类型   表关联   

一,关键词:

  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;

  

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

标签:microsoft   font   内连接   sql数据库   bsp   soft   保留   类型   表关联   

原文地址:http://www.cnblogs.com/hi-feng/p/7895823.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!