MySQL三表连接查询

MySQL三表连接查询,第1张

有两种方式: 关键字where 或嵌入在inner 或left 中:

下面定义3个表A,B,C,字段分别为A:a,b;B:b,c;C:c,d

正常where 使用语句如下:

select A.a,B.b,C.c from A

inner join B on A.b=B.b

inner join C on C.c=B.c

where A.a=10 or B.b=10 or C.c=10

下面的SQL 嵌入到inner 中的使用方式:

select A.a,B.b,C.c from A

inner join B on A.b=B.b and B.b=10

inner join C on C.c=B.c and C.c=10

使用UNION联合两个语句即可:

select * from a where cid=1

UNION

select * from b where cid=1

select * from subject a join attachment b on a.tid=b.tid

join message c on b.tid=c.tid

或者

select * from subject a ,attachment b ,message c

where a.tid=b.tid and b.tid=c.tid


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5906703.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-07
下一篇 2023-03-07

发表评论

登录后才能评论

评论列表(0条)

保存