MySQL三表查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL三表查询相关的知识,希望对你有一定的参考价值。

现在有travelerinfo(orderId、ticketRef)、tickorderinfo(orderId、tickOrderId、status)、pnrauth(tickOrderId、authPCC),三个表七个字段。现在要查B表中status=28,authPCC=ABC的1000条数据。应该怎么写。
实际上只要status=28,authPCC=ABC就行了

select *
from travelerinfo t1 left join tickorderinfo t2 on t1.orderId=t2.orderId
                                 left join pnrauth t3 on t2.tickOrderId=t3.tickOrderId
where t2.status=28 and t3.authPCC='ABC'
order by t1.orderid
limit 1000

参考技术A 做最简单的串联就是了,用假名,这种比较容易理解,用:
select top(1000) * from travelerinfo a,tickorderinfo b,pnrauth c where
a.orderid=b.orderid and b.tickorderid=c.tickorderid

大概是这样,自己研究下再改下

mysql 三表联合查询

A表 id cid title
B表 id cid title
(AB表的结构一样)
C表 cid cname
C表示AB两个的分类表,AB两个都使用的C表的分类,问题来了
查询cid=1的内容?
select * from a where cid=1 和select * from b where cid=1 这两sql可以组合一起吗?

使用UNION联合两个语句即可:
select * from a where cid=1
UNION
select * from b where cid=1
参考技术A select * from a as a, b as b where a.cid=b.cid and a.cid=1
是这样吗?追问

不行的,它会把A表和B表的字段都一起列出来作为一条记录
查询结果是
第1条记录:id cid title id cid title
第2条记录:id cid title id cid title
....

追答

2个Sql组合 就用UNION
select * from a where cid=1
UNION
select * from b where cid=1

以上是关于MySQL三表查询的主要内容,如果未能解决你的问题,请参考以下文章

mysql三表联合查询问题

mysql三表联合查询

急!!求MYSQL三表关联查询方法

mysql三表联查,

MySQL多表查询 三表查询 连接查询的套路

mysql三表查询sql语句