Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using two select in one query

Tags:

sql

select

sqlite

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';

Query 2:

SELECT count(*) _countB FROM TCONTENTNODE WHERE type = '3' 
     AND parentid ='02b3abc2-4983-485a-ab09-1a8cb328b9b5';

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.

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

like image 640
Jainendra Avatar asked Oct 15 '25 19:10

Jainendra


2 Answers

I solved the problem using the following query:-

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";
like image 184
Jainendra Avatar answered Oct 18 '25 13:10

Jainendra


Dont have SQLite to confirm but

select count(*), type FROM TCONTENTNODE WHERE (type = '2' or type = '3' )
     AND parentid ='02b3abc2-4983-485a-ab09-1a8cb328b9b5' group by type

Just to clarify this will return

count type

23     2
23888  3
like image 29
BugFinder Avatar answered Oct 18 '25 13:10

BugFinder