mysql - 问一个select语句。
PHPz
PHPz 2017-04-17 13:48:24
0
1
429

假设我有三个表

ba(id,tt,day);
bb(id,aid,tt,day);
bc(id,bid,tt,day);

aidbid 分别对应的是baid,与bbid

table: ba
[id=1,tt=aaa,day=2015]
[id=2,tt=ccc,day=2015]
[id=3,tt=ddd,day=2015]

table: bb
[id=1,aid=3,tt=b-a,day=2016]
[id=2,aid=2,tt=b-b,day=2016]
[id=3,aid=1,tt=b-c,day=2016]

table: bc
[id=1,bid=1,tt=c-1,day=1]
[id=2,bid=1,tt=c-2,day=1]
[id=3,bid=3,tt=c-3,day=1]

假设我已 bb.id=1 为条件查询数据。
用一条select语句查出如下数据要怎么写。

1. ba.tt as at
2. bb.id as bid, bb.tt as bt
3. bc.id as cid, bc.tt as ct, bc.day as cday

也就是要查出除了 table.bb 的id=1的中的内容还要查出与之关联的ba、bc中的内容。
也就 ba[3],bb[1],bc[1],bc[2]中的内容。

我是这样写的,可是好像不行……

SELECT 
    ba.tt as at,
    bb.id as bid, bb.tt as bt,
    bc.id as cid, bc.tt as ct, bc.day as cday
FROM
    bb
    INNER JION 
        bb.aid=ba.id
    AND
        bc.bid=bb.id
    WHERE
        bb.id=1       
PHPz
PHPz

学习是最好的投资!

Antworte allen(1)
刘奇
SELECT
    ba.tt as at,
    bb.id as bid, bb.tt as bt,
    bc.id as cid, bc.tt as ct, bc.day as cday
FROM
    bb
INNER JOIN ba ON bb.aid=ba.id
INNER JOIN bc ON bc.bid=bb.id
WHERE bb.id=1;

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!