SQL帮助,无法通过连接获得所需的输出

时间:2014-06-05 08:47:56

标签: php mysql

我有这个问题:

SELECT 
    t1.team_flag AS flag_1, 
    t2.team_flag AS flag_2, 
    t1.team_name AS team_name_1, 
    t2.team_name AS team_name_2, 
    t1.team_id AS team_id_1, 
    t2.team_id AS team_id_2, 
    games.game_id, 
    games.game_team_1, 
    games.game_team_2, 
    games.game_time, 
    games.game_day, 
    games.game_location, 
    games.game_group, 
    games.game_active, 
    location_id_to_names.location_id, 
    location_id_to_names.location_name 
FROM games 
    LEFT JOIN teams t1 ON t1.team_id = game_team_1 
    LEFT JOIN teams t2 ON t2.team_id = game_team_2 
    LEFT JOIN location_id_to_names ON games.game_location = location_id_to_names.location_id
ORDER BY games.game_day

返回所有团队名称正确的游戏,但是,我现在只想选择一个具有特定ID的游戏,这不起作用:

SELECT 
    t1.team_flag AS flag_1, 
    t2.team_flag AS flag_2, 
    t1.team_name AS team_name_1, 
    t2.team_name AS team_name_2, 
    t1.team_id AS team_id_1, 
    t2.team_id AS team_id_2, 
    games.game_id, 
    games.game_team_1, 
    games.game_team_2, 
    games.game_time, 
    games.game_day, 
    games.game_location, 
    games.game_group, 
    games.game_active, 
    location_id_to_names.location_id, 
    location_id_to_names.location_name 
FROM games 
    LEFT JOIN teams t1 ON t1.team_id = game_team_1 AND games.game_id = :game_id 
    LEFT JOIN teams t2 ON t2.team_id = game_team_2 
    LEFT JOIN location_id_to_names ON games.game_location = location_id_to_names.location_id 
ORDER BY games.game_day

(:game_id)由PDO解析。

这是我得到的结果:

enter image description here 突出显示的行中的ID是正确的,但它不是第一行,我需要将其作为第一行,因为我想将搜索限制为1个结果,所以我不想要一行填充空值。

完成这项工作的最佳方法是什么?

2 个答案:

答案 0 :(得分:0)

将games.game_id =:game_id移至WHERE语句

SELECT 
    t1.team_flag AS flag_1, 
    t2.team_flag AS flag_2, 
    t1.team_name AS team_name_1, 
    t2.team_name AS team_name_2, 
    t1.team_id AS team_id_1, 
    t2.team_id AS team_id_2, 
    games.game_id, 
    games.game_team_1, 
    games.game_team_2, 
    games.game_time, 
    games.game_day, 
    games.game_location, 
    games.game_group, 
    games.game_active, 
    location_id_to_names.location_id, 
    location_id_to_names.location_name 
FROM games 
    LEFT JOIN teams t1 ON t1.team_id = game_team_1
    LEFT JOIN teams t2 ON t2.team_id = game_team_2 
    LEFT JOIN location_id_to_names ON games.game_location = location_id_to_names.location_id 
WHERE
    games.game_id = :game_id 
ORDER BY games.game_day

答案 1 :(得分:0)

从您的加入中移除LEFT并留下一个简单的JOIN

SELECT 
    t1.team_flag AS flag_1, 
    t2.team_flag AS flag_2, 
    t1.team_name AS team_name_1, 
    t2.team_name AS team_name_2, 
    t1.team_id AS team_id_1, 
    t2.team_id AS team_id_2, 
    games.game_id, 
    games.game_team_1, 
    games.game_team_2, 
    games.game_time, 
    games.game_day, 
    games.game_location, 
    games.game_group, 
    games.game_active, 
    location_id_to_names.location_id, 
    location_id_to_names.location_name 
FROM games 
    LEFT JOIN teams t1 ON t1.team_id = game_team_1 AND games.game_id = :game_id 
    LEFT JOIN teams t2 ON t2.team_id = game_team_2 
    LEFT JOIN location_id_to_names ON games.game_location = location_id_to_names.location_id 
ORDER BY games.game_day

LEFT部分负责将您想要的行加入到同一团队的所有其他行中。

相关问题