在我的where子句中需要有关多个条件的帮助

时间:2019-02-26 19:58:47

标签: sql postgresql

我对我的数据集有一个简单的查询:

SELECT * FROM efleet_copy
WHERE EXTRACT(MONTH from service_date) = 02
AND status = 'Approved' AND mta_id NOT LIKE '_________'
ORDER BY mta_id;

我需要排除满足以下条件的交易:

provider = MTA CURB APP which is mta_id = 1
provider = MTA CURB APP Streethail which is mta_id = 1
provider = MTA CONCIERGE which is mta_id = 2

这是示例数据:

enter image description here

我已经在我的where子句中尝试了这些查询,但是没有用:

AND provider IN ('MTA CURB APP', 'MTA CURB APP Streethail') AND mta_id <> '1'
AND provider IN ('MTA CONCIERGE') AND mta_id <> '2'

AND (provider = 'MTA CURB APP' AND mta_id <> '1') OR (provider = 'MTA CURB APP Streethail' AND mta_id <> '1') OR (provider = 'MTA CONCIERGE' AND mta_id <> '2')

1 个答案:

答案 0 :(得分:1)

这是您想要的吗?

WHERE EXTRACT(MONTH from service_date) = 2 AND
      status = 'Approved' AND
      mta_id NOT LIKE '_________' AND
      (provider, mta_id) NOT IN ( ('MTA CURB APP', 1),
                                  ('MTA CURB APP Streethail', 1)
                                  ('MTA CONCIERGE', 2)
                                );
ORDER BY mta_id;
相关问题