Join for mySQL的问题 - 我可以做我想做的事吗?

时间:2013-08-16 19:34:30

标签: mysql join

  • tableA栏:homeid,awayid,date
  • tableB列:id,logo,schoolname

tableB ID与tableA

中的ID匹配

也许这不起作用,但我必须认为它比在PHP中循环更好。

我想要特定日期的结果:

hometeamname hometeamlogo awayteamname awayteamlogo

全部在一行

这可能吗?

5 个答案:

答案 0 :(得分:1)

进行两次联接以获取主页和离开数据。

select b1.schoolname hometeamname, b1.logo hometeamlogo,
       b2.schoolname awayteamname, b2.logo awayteamlogo
from tableA a
join tableB b1 on b1.id = a.homeid
join tableB b2 on b2.id = a.awayid
where a.date = 'yyyy-mm-dd'

答案 1 :(得分:0)

您可以使用两个不同的别名tableBh加入a两次:

SELECT
  h.schoolname hometeamname,
  h.logo hometeamlogo,
  a.schoolname awayteamname,
  a.logo awayteamlogo
FROM
  tableA inner join tableB h on tableA.homeid=h.id
  inner join tableB a on tableA.awayid=a.id

然后您可以按日期过滤。

答案 2 :(得分:0)

在表A上,tableB id是多值的?

如果是;

select * from tablea
inner join tableb on tableb.id IN(tablea.awayid)

如果没有

select * from tablea
inner join tableb on tableb.id = tablea.awayid

我不太确定你想要什么

答案 3 :(得分:0)

加入,类似(没有检查它可能有语法错误):

select H.schoolname as hometeamname, H.logo as hometeamlogo, 
       W.schoolanme as awayteamname, W.logo as awayteamlogo 
       from tableA as A join tableB as H on (tableA.homeid = H.id) 
       join table B as W on (tableA.homeid = W.id);

答案 4 :(得分:0)

试试这个:

我没有对此进行过测试,但它应该有效。

SELECT home.schoolname, home.logo, away.schoolname, away.logo
FROM tableB home, tableB away
WHERE (home.id, away.id) IN (SELECT homeid, awayid FROM tableA)