繁体   English   中英

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

[英]SQL help, can't get the desired output with joins

我有这个查询:

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解析。

这是我得到的输出:

在此处输入图片说明 突出显示的行中的ID是正确的,但它不是第一行,我需要将其作为第一行,因为我想将搜索限制为1个结果,所以我不希望行中填充空值。

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

将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

从联接中删除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部分负责将所需的行与同一个团队的所有其他行连接起来。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM