只是为了扩展我以前的问题-我的数据库中有两个表,我想提取某些信息。下表:
(player): player_id (primary), playerName (match): match_id (primary), playerID1, playerID2, playerID3, scorer etc..
您向我提供了以下代码来获取播放器的名称:
SELECT p.Name FROM `match` m INNER JOIN `player` p ON p.player_id IN (m.playerID1, m.playerID2, m.playerID3) //etc
效果很好,谢谢-我只想进行两项调整:
尝试类似以下的操作(假设字段名称)。它将显示玩家的姓名,比赛的matchId以及他们是否在该比赛中得分:)。
SELECT p.Name as 'Player Name', m.matchId as 'Match Id', CASE s.scorerId WHEN ISNULL(S.SCORERID,0) THEN 'YES' ELSE 'NO' END as 'Scored?', s.name as 'Scorer Name' --as requested from comment FROM match m INNER JOIN player p ON p.player_id IN (m.playerID1, m.playerID2, m.playerID3) LEFT JOIN scorer s ON s.matchId = m.matchId AND s.player_id IN (m.playerID1, m.playerID2, m.playerID3)
看到这个SQLFiddle HERE