SQL查询以找到所有具有标志的逻辑表达式的UUID
我有一个带有UUID和一个标志列的SQLite数据库(在其他列中),我想返回所有满足标志逻辑表达式的UUID。
UUID | flag | ...
1 | "a" |
1 | "b" |
1 | "a" |
2 | "b" |
2 | "c" |
3 | "a" |
例如,我想返回所有在所有行上都具有标志(“ a”和“ b”或“ c”)的UUID。在上表中,只有uuid = 1满足该约束。
这是一个类似的问题 - 没有脱节---因此,那里的解决方案在这里不起作用。
编辑:@forpas 拥有sum
解决方案是我想要的,但是我最终通过在看到它看到的用户定义的聚合函数来解决问题。
I have a SQLite database with ~30 million rows with UUIDs and a flag column (among other columns) and I want to return all UUIDs that satisfy a logical expression of the flags.
UUID | flag | ...
1 | "a" |
1 | "b" |
1 | "a" |
2 | "b" |
2 | "c" |
3 | "a" |
For example I want to return all UUIDs that have flag ("a" AND ("b" or "c")) over all rows. In the above table only UUID=1 satisfies that constraint.
This is a similar question but it only asks about the case of having all 4 flags set --- there is no disjunction --- so the solutions there don't work here.
edit: @forpas HAVING SUM
solution is what I was looking for but I ending up solving the problem by creating a user defined aggregate function before I saw it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
具有
子句中的条件的聚合:您可以添加任意多的条件。
请参阅 demo 。
You can use aggregation with the conditions in the
HAVING
clause:You can add as many conditions as you want.
See the demo.
使用
min()和max()
函数考虑以下方法:当
min()
与字符串一起使用时,字符串是按字母顺序排列的,并且第一个返回。在您的情况下,具有a
值的标志将是最小值,并且具有c
值的标志将是最大值。 HASTER子句将确保组内有一个a
值,并且该值不等于组的最大标志值(在这种情况下为b或c
)。请参阅在这里的演示。
Consider the following approach using
min() and max()
functions:When
min()
is used with strings, the strings are ordered alphabetically A-Z and the first one is returned. In your case flags witha
value will be the minimum and flags withc
value will be the maximum. The having clause will ensure that there's ana
value within the group and that value is not equal to the maximum flag value of the group (in this caseb or c
).See a demo from here.
您可以使用
存在
,如果还有其他uUID的行,并且标志可以更简单地写
you can use
EXISTS
if there are other rows with the same UUID, and the flagor you can simpler write