问一个select语句。

假设我有三个表

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       
阅读 3k
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;

clipboard.png

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题