我有一个包含主行的表,我想要所有这些行。然后有2个表可能没有匹配的行,但如果这些表没有匹配的行,我仍然希望主表中的行。
此查询有效,但如果所有3个表中都没有行,则不会返回任何内容。我在php中使用mysql。
SELECT PS_Info.* , PS_Guides.Guide, PS_User.UserID FROM PS_Info
JOIN PS_Guides ON PS_Info.ID = PS_Guides.InfoID
JOIN PS_User ON PS_Info.ID = PS_User.InfoID
WHERE PS_Info.GameID = :ID AND PS_User.UserID = :UserID
我正在尝试显示PS_Info中的列表。然后我想隐藏其中一些项目,如果用户将它们设置为隐藏(PS_User = UserID,InfoID)。如果PS_User中没有任何内容,则他们没有将此项设置为隐藏,因此我想显示它。 PS_Guides包含有关列表中每个项目的额外信息。并非所有项目都包含额外信息,因此如果此表中没有行,则我不显示它。
答案 0 :(得分:2)
您正在寻找' left join'。
SELECT PS_Info.* , PS_Guides.Guide, PS_User.UserID FROM PS_Info
LEFT JOIN PS_Guides ON PS_Info.ID = PS_Guides.InfoID
LEFT JOIN PS_User ON PS_Info.ID = PS_User.InfoID
WHERE PS_Info.GameID = :ID AND PS_User.UserID = :UserID
答案 1 :(得分:2)
SELECT PS_Info.* , PS_Guides.Guide, PS_User.UserID FROM PS_Info
left JOIN PS_Guides ON PS_Info.ID = PS_Guides.InfoID //use left join
left JOIN PS_User ON PS_Info.ID = PS_User.InfoID //use left join
WHERE PS_Info.GameID = :ID AND PS_User.UserID = :UserID