Reputation: 57
Trying to count the number of canceled trips after filtering out the banned users (Client+Driver), however it seems like my where clause is not working
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
Upvotes: 0
Views: 46
Reputation: 1842
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');
Upvotes: 0
Reputation: 651
You need a further parentheses around the first OR term in your WHERE. The OR term is causing logical errors:
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
Upvotes: 2