- tableA 列:homeid,awayid,date
- tableB 列:id、logo、schoolname
tableB ID 与 tableA 中的 ID 匹配
也许这行不通,但我必须认为它比在 PHP 中循环更好。
我想要特定日期的这样的结果:
hometeamname hometeamlogo awayteamname awayteamlogo
都在一条线上
那可能吗?
执行两次连接以获取主客场数据。
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'
您可以加入tableB
两次,使用两个不同的别名h
和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
然后你可以按日期过滤。
在表 A 上,表 B 的 id 是多值的?
如果是;
select * from tablea
inner join tableb on tableb.id IN(tablea.awayid)
如果不
select * from tablea
inner join tableb on tableb.id = tablea.awayid
我不太确定你想要什么
加入,类似(没有检查它,所以可能有语法错误):
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);
尝试这个:
我没有对此进行测试,但它应该可以工作。
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)