我有两个SQL查询来计算表tContentNode
中的行,具体取决于类型2或3
查询1:
SELECT count(*) _countA FROM TCONTENTNODE WHERE type = '2'
AND parentid ='02b3abc2-4983-485a-ab09-1a8cb328b9b5';
查询2:
SELECT count(*) _countB FROM TCONTENTNODE WHERE type = '3'
AND parentid ='02b3abc2-4983-485a-ab09-1a8cb328b9b5';
现在我想只使用一个查询获取_countA
和_countB
的值。如何使用一个查询获取计数。有没有办法做到这一点。我正在使用SQLite数据库。
修改
我想要_countA
和_countB
的值分开,而不是两者都在一起(不使用IN
)。
答案 0 :(得分:2)
我使用以下查询解决了问题: -
SELECT
COUNT(case TCONTENTNODE.type when '2' then 1 end) as _countA,
COUNT(case TCONTENTNODE.type when '3' then 1 end) as _countB
FROM TCONTENTNODE
WHERE TCONTENTNODE.parentid ="02b3abc2-4983-485a-ab09-1a8cb328b9b5";
答案 1 :(得分:1)
不要让SQLite确认,但
select count(*), type FROM TCONTENTNODE WHERE (type = '2' or type = '3' )
AND parentid ='02b3abc2-4983-485a-ab09-1a8cb328b9b5' group by type
只是为了澄清这将返回
count type
23 2
23888 3
答案 2 :(得分:0)
尝试使用group by
SELECT type,count(*) as cnt
FROM TCONTENTNODE
WHERE parentid ='02b3abc2-4983-485a-ab09-1a8cb328b9b5';
group by type
有两个变量:
SELECT
COUNT(case TCONTENTNODE.type when '2' then 1 end) as _countA,
COUNT(case TCONTENTNODE.type when '3' then 1 end) as _countB
FROM TCONTENTNODE
WHERE TCONTENTNODE.parentid ='02b3abc2-4983-485a-ab09-1a8cb328b9b5';
答案 3 :(得分:0)
使用IN KeyWord
SELECT count(*)_ countb FROM TCONTENTNODE WHERE(输入IN(3,2)AND parentid ='02b3abc2-4983-485a-ab09-1a8cb328b9b5');