在一个查询中使用两个选择 [英] Using two select in one query

查看:125
本文介绍了在一个查询中使用两个选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个用于查询表tContentNode中的行的SQL查询,具体取决于类型2或3.
查询1:

I have two SQL queries for counting the rows from the table tContentNode, depending on the type 2 or 3.
Query 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数据库.

Now I want to get the values of _countA and _countB using only one query. How can I get the counts using one query. Is there any way to do that. I am using SQLite Database.

我想要分别_countA_countB的值,而不是两者都在一起(不使用IN).

I want values of _countA and _countB separately, not both together(Not using IN).

推荐答案

尝试使用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';

这篇关于在一个查询中使用两个选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆