avatar
问个SQL的问题# Database - 数据库
m*e
1
比如我有个table,只有两个column A和B
现在我想做如下判断:
1. 是否A>10的entry的数量大于X
2. 是否B<20的entry的数量大于Y
要求用一个SELECT query判断1和2 至少有一个成立。
如果光是1,很容易:
SELECT COUNT(1) > X FROM tbl WHERE A > 10;
如果光是2,也很容易:
SELECT COUNT(1) > Y FROM tbl WHERE B < 20;
可是两个combine起来,怎么写?注意要一个query,最后返回一行,0 or 1。
avatar
a*y
2
要是SQL SERVER的话,
SELECT CASE WHEN (p1>X or p2>Y) THEN 1 ELSE 0 END
from (select count(*) as p1 FROM tbl WHERE A > 10) AS d1,
(select count(*) as p2 FROM tbl WHERE B < 20) AS d2;
avatar
a*y
3
要是SQL SERVER的话,
SELECT CASE WHEN (p1>X or p2>Y) THEN 1 ELSE 0 END
from (select count(*) as p1 FROM tbl WHERE A > 10) AS d1,
(select count(*) as p2 FROM tbl WHERE B < 20) AS d2;

【在 a*******y 的大作中提到】
: 要是SQL SERVER的话,
: SELECT CASE WHEN (p1>X or p2>Y) THEN 1 ELSE 0 END
: from (select count(*) as p1 FROM tbl WHERE A > 10) AS d1,
: (select count(*) as p2 FROM tbl WHERE B < 20) AS d2;

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。