300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > SQL查询语句多表联查 三表关联查询

SQL查询语句多表联查 三表关联查询

时间:2023-06-05 14:20:34

相关推荐

SQL查询语句多表联查 三表关联查询

1.左连接(三表关联)

三表只查询左表有数据的值,右表没有的数据会显示出NULL值

语法 left---join---on

示例:

select s.*,c.*,d.* from 表名 s left join 表名 c on c.id = s.sid

left join 表名 d on d.id = s.sid

select s.*,c.*,d.* from 表名 s left join 表名 c on c.id = s.sidleft join 表名 d on d.id = s.sid

2.右连接(三表关联)

三表只查询右表有的数据值,左表没有的数据会显示出BULL值

语法:rigth---join---on

示例:select s.*,c.*,o.* from student s right join sc c on

c.cid = s.sid right join course o on o.oid = s.sid

select s.*,c.*,o.* from student s right join sc c onc.cid = s.sid right join course o on o.oid = s.sid

3.内连接(三表关联)

查询三表共有的例的值,有一例为NULL值时其他表的例也不会查询出来

语法:inner---join---on

示例:select s.*,c.*,o.* from student s innerjoin sc c on

c.cid = s.sid inner join course o on o.oid = s.sid

select s.*,c.*,o.* from student s innerjoin sc c onc.cid = s.sid inner join course o on o.oid = s.sid

4.全连接(三表关联)

语法:union

示例:select s.*,c.*,o.* from student s right join sc c on

c.cid = s.sid right join course o on o.oid = s.sid UNION select s.*,c.*,o.* from student s innerjoin sc c on

c.cid = s.sid inner join course o on o.oid = s.sid

select s.*,c.*,o.* from student s right join sc c onc.cid = s.sid right join course o on o.oid = s.sid UNION select s.*,c.*,o.* from student s innerjoin sc c onc.cid = s.sid inner join course o on o.oid = s.sid

5.连接模糊查询(左连接示例)

SELECT s.*,c.*,o.* FROM student s LEFT JOIN sc c ON s.sid = c.cidLEFT JOIN course o ON o.oid = s.sid WHERE s.sdept LIKE "%c%"

新手菜鸟一枚,如有错误的地方还请大神指教~~

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。