【问题标题】:Result not filtered结果未过滤
【发布时间】:2019-10-08 23:33:33
【问题描述】:

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

行程表

用户表

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

【问题讨论】:

  • 请将您的数据添加为文本而不是图像。

标签: sql sql-server


【解决方案1】:

您需要在 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

【讨论】:

    【解决方案2】:
    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');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      • 2016-01-11
      • 2012-07-08
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多