SQL查询有效但数据是否已损坏?

时间:2014-02-27 04:32:55

标签: mysql sql

我有一个查询在仅搜索1个游戏WHERE gameid = 36的数据时工作正常,但是一旦我尝试加入多个游戏gameid = 36 AND gameid = 37,查询运行正常,但返回所有数据的所有0

SELECT 
 CONCAT_WS(  '. ', SUBSTR( p.playerfname, 1, 1 ) , p.playerlname ) name, 
 COALESCE( goalEvents.goals, 0 ) goals, 
 COALESCE( a1.assists, 0 ) + COALESCE( a2.assists, 0 ) assists, 
 COALESCE( goalEvents.goals, 0 ) + COALESCE( a1.assists, 0 ) + COALESCE( a2.assists, 0 ) points
FROM players p
 LEFT JOIN (SELECT scorer, COUNT( scorer ) goals FROM goalEvents WHERE gameid = 36 AND gameid = 37GROUP BY scorer) goalEvents ON p.playerid = goalEvents.scorer
 LEFT JOIN (SELECT assist1, COUNT( assist1 ) assists FROM goalEvents WHERE gameid = 36 AND gameid = 37 GROUP BY assist1) a1 ON p.playerid = a1.assist1
 LEFT JOIN (SELECT assist2, COUNT( assist2 ) assists FROM goalEvents WHERE gameid = 36 AND gameid = 37 GROUP BY assist2) a2 ON p.playerid = a2.assist2
WHERE p.playerteam = 26 ORDER BY points DESC, goals DESC

在调用多个游戏线时必须出现错误

FROM goalEvents WHERE gameid = 36 AND gameid = 37 

我在多场比赛中搜索错了吗?我基本上试图通过我桌子的最后4个游戏来搜索

3 个答案:

答案 0 :(得分:1)

尝试:

FROM goalEvents WHERE gameid = 36 OR gameid = 37

您需要使用OR代替AND,gameid永远不能是36 AND 37。您想要找到两个不同的记录,一个记录gameid=36而另一个记录与gameid=37。如果你使用AND,他们都会失败。

如果您有2个以上,您还可以尝试gameid IN (36, 37, ...)这是OROROR等的简写

答案 1 :(得分:1)

你可以使用IN条款我认为这样可以解决问题

喜欢试试这个

FROM goalEvents WHERE gameid in(36,37)
祝你好运。

答案 2 :(得分:1)

试试这个:

FROM goalEvents WHERE gameid in (36, 37);

我猜gameid是独一无二的,所以你不能使用and,因为它不可能在一行中为36和37。因此,请尝试使用in