结果未过滤

时间:2019-10-08 23:33:33

标签: sql sql-server

在过滤掉被禁止的用户(Client + Driver)后尝试计算已取消的行程数,但是似乎我的where子句不起作用

Trips表 Trips Table

用户表

Users Table

SELECT COUNT(*)
FROM Trips
WHERE Status = 'cancelled_by_driver' OR Status = 'cancelled_by_client'
AND Client_Id NOT IN (SELECT Users_Id
                      FROM Users
                      WHERE Users.Banned = 'Yes')
AND Driver_Id NOT IN (SELECT Users_Id
                      FROM Users
                      WHERE Users.Banned = 'Yes')
GROUP BY Request_at

2 个答案:

答案 0 :(得分:2)

您需要在WHERE中的第一个OR词附近加上一个括号。 OR项引起逻辑错误:

SELECT COUNT(*)
FROM Trips
WHERE (Status = 'cancelled_by_driver' OR Status = 'cancelled_by_client')
AND Client_Id NOT IN (SELECT Users_Id
                      FROM Users
                      WHERE Users.Banned = 'Yes')
AND Driver_Id NOT IN (SELECT Users_Id
                      FROM Users
                      WHERE Users.Banned = 'Yes')
GROUP BY Request_at

答案 1 :(得分:0)

select count(*)
from trips, users
where trips.Status in ('cancelled_by_driver','cancelled_by_client')
and (trips.Client_Id = user.id and user.banned != 'Yes')
and (trips.Driver_Id = user.id and user.banned != 'Yes');