我正在尝试编写如下查询
if (select count(*) from Users where fkId=5000 and status='r') =
(select count(*) from Users where fkId=5000) then ..
只需一个查询。
这意味着,如果所有具有 fkId=5000 的行也具有status=r
,然后做点什么。
fkId=5000
可以有任意数量的行,并且这些行的任何部分都可以有 status=r
, status=k
, status=l
, status=a
等等 我对所有具有 fkId=5000
的行的情况感兴趣还有 status=r
(而不是任何其他状态)。
我现在的做法是
how many rows with id=5000 and status = 'r'?
how many rows with id=5000?
are those numbers equal? then ..
我正在尝试弄清楚如何仅使用 1 个查询而不是 2 个来重写此查询。关键字 ALL似乎无法编写这样的查询( <> ALL
相当于 NOT IN
)。我试了几个GROUP BY
公式,但无法得到正确的结果。
最佳答案
最有效的方法是:
if not exists (select 1
from users
where fkid = 5000 and (status <> 'r' or status is null)
)
这将在第一个不匹配的行停止查询。
关于MySQL - 判断列_all_是否具有相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17001256/