0

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
1
  • Is there an id in the second table? Commented Nov 17, 2014 at 14:52

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.