简体   繁体   English

查询以返回(分组的)行,其中不存在具有column = condition的行

[英]Query to return (grouped) rows where no row with column=condition exists

Let's say I have a table with two columns, a client_id and a boolean. 假设我有一个包含两列的表,一个client_id和一个布尔值。

There are multiple data entries for a given client_id, each of which may or may not have the boolean value set to true. 给定的client_id有多个数据条目,每个条目的布尔值都可以设置为true,也可以不设置为true。

I need a query that will return only the client_ids that have NO ROWS with the boolean set to true, and I need it grouped by client_id. 我需要一个查询,该查询将仅返回没有ROWS且将布尔值设置为true的client_id,并且需要按client_id进行分组。

I'm sure this is simple, it just escapes me right now. 我敢肯定,这很简单,现在就让我摆脱了。

Use the HAVING clause to filter the groups: 使用HAVING子句来过滤组:

SELECT client_id FROM my_table GROUP BY client_id HAVING SUM(boolean) = 0

Note that SUM(boolean) works in MySQL because it does not have true boolean types; 注意, SUM(boolean)在MySQL中有效,因为它没有真正的布尔类型。 in other RDBMS you would either have to use a different aggregate function or else test the boolean and return a result that can be used in SUM() : 在其他RDBMS中,您要么必须使用其他聚合函数,要么测试布尔值并返回可在SUM()使用的结果:

... HAVING SUM(CASE WHEN boolean THEN 1 END) = 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM