weto
weto

Reputation: 69

SELECT count(*) IN SELECT

How can SELECT count(*) IN SELECT

I have select:

SELECT t1.idTab1 
FROM table1 t1, (SELECT count(*) FROM table2 t2 WHERE t2.idTab1 = t1.idTab1) 
WHERE t1.idTab1 <= 3

My sample data is:

Table1:

idTab1
1
2
3

Table2:

Tab2CountIdTab1
10
200
30

And in result I want to have:

idTab1 Tab2CountIdTab1 
1      10
2      200
3      30

Upvotes: 0

Views: 174

Answers (1)

user4261009
user4261009

Reputation:

You may want to use a subquery like this:

SELECT t1.idTab1,
       (SELECT count(*) 
          FROM table2 t2 
         WHERE t2.idTab1 = t1.idTab1) as Tab2CountIdTab1
  FROM table1 t1
 WHERE t1.idTab1 <= 3;

Upvotes: 3

Related Questions