我被困在一个与MySQL相关的问题中。采取的查询
select * from orders where customer_id in (256, 255, 236);在各种网站上,在IN操作符的解释下,他们提到(例如https://www.w3schools.com/sql/sql_in.asp) IN在多个或多个条件下起着类似捷径的作用。
在MySQL中是否有适用于多个条件的操作符?
上面的查询将返回与customer_id 256、255和236相关的所有行。但是,如果我只想返回这三个customer_ids共有的行,则尝试执行以下查询。
select * from orders where customer_id = 256 and customer_id = 255 and customer_id = 236;但我知道这行不通。
因此,我想知道我是否可以使用快捷方式处理这个倍数和条件(而不使用group by和can )?
发布于 2019-10-02 12:16:22
您可以使用聚合:
select o.order_id
from orders o
where o.customer_id in (256, 255, 236)
group by o.order_id
having count(distinct o.customer_id) = 3; -- "3" = all customers这需要聚合(或join或窗口函数),因为您希望比较不同行上的数据。在这种情况下,一个简单的WHERE子句是不够的。
https://stackoverflow.com/questions/58201327
复制相似问题