我的MySQL技能非常基础,所以我想得到的是团队1的名称,表格的团队2来自表团队,在一个SELECT语句中(如果可能的话)。我有一个声明,但我只得到团队2中团队1的名称我得到了与团队1相同的名称
小组表
--------------------------------------- - team_id - team_title - team_image - --------------------------------------- - 1 - Brazil - br.png - - 2 - Russia - ru.png - - 3 - Spain - es.png - ---------------------------------------
匹配表
------------------------------------------ - match_id - match_team1 - match_team2 - ------------------------------------------ - 1 - 2 - 3 - - 2 - 1 - 2 - - 3 - 3 - 1 - ------------------------------------------
功能
function get_all_matches($connect) { $sentence = $connect->prepare(" SELECT matches.* , teams.team_title FROM matches , teams WHERE matches.match_team1 = teams.team_id ORDER BY matches.match_id DESC "); $sentence->execute(); return $sentence->fetchAll(); }
答案 0 :(得分:1)
您需要两个联接,每个联接用于您要检索的每个团队名称。
另外,请注意隐式连接(在from
子句中有多个表)已被认为已弃用很长一段时间了,您应该使用现代显式join
子句:< / p>
SELECT m.*,
t1.team_title AS team_1_title,
t2.team_title AS team_2_title
FROM matches m
JOIN teams t1 ON m.match_team1 = t1.team_id
JOIN teams t2 ON m.match_team2 = t2.team_id
ORDER BY m.match_id DESC
答案 1 :(得分:0)
你有没有试过像
这样的东西 Select A.match_team1, t1.team_title,
A.match_team2, t2.team_title
FROM matches A
left outer join teams t2
on A.match_team2 = t2.team_id,
teams T1
WHERE A.match_team1 = t1.team_id
我不熟悉MySql,但是我猜应该支持加入。