SQL加入" AND" +"或"声明

时间:2016-02-10 07:17:19

标签: php sql join

我遇到了这个SQL语句的问题。我试图通过ID' 25'将结果限制在玩家身上。和' 87'查询没有错误(我通过此查询得到每个PID)这是语句。

SELECT Rosters.PID, SUM( Rosters.Goals ) Goals, SUM( Rosters.Assists ) Assists, SUM( Rosters.PIM ) PIM, Rosters.TID, Players.pid, Players.firstname, Players.lastname, 
       SUM((Rosters.Goals + Rosters.Assists)) AS Points, Teams.tid, Teams.teamname
       FROM Rosters
       INNER JOIN Players
       ON Rosters.PID = Players.pid
       INNER JOIN Teams 
       ON Rosters.TID = Teams.tid
       WHERE ((Rosters.TID BETWEEN 9 AND 16) AND (Players.pid = 25 OR 87))
       GROUP BY 
       Players.pid
       ORDER BY Points DESC

我几乎可以肯定这是我缺乏SQL理解的原因,但是W3C学校并没有提供更复杂的解决方案,而且我查找的Stack线程已经非常本地化了。

Here is the current output. It should just be the players with Id's 25 and 87

4 个答案:

答案 0 :(得分:3)

Players.pid附近的条件有误。

应该是

(Players.pid = 25 OR Players.pid = 87)

逻辑OR运算符组合了两个逻辑条件。 Players.pid = 25是有效条件,它评估为布尔truefalse。但是87肯定不会计算为布尔值。

答案 1 :(得分:2)

只需将代码的一部分从Players.pid = 25 OR 87更改为Players.pid = 25 OR Players.pid = 87

答案 2 :(得分:1)

或者您可以使用IN构造将Players.pid与可能值列表进行匹配(请注意,不再需要外括号):

QXmlStreamReader

答案 3 :(得分:1)

另外如果您认为Players.pid会增加以检查条件,您可以使用IN操作并像这样使用

AND Players.pid IN (25,87) // You can add here more id to check

代替您的代码

AND (Players.pid = 25 OR 87)