Antonio E.
Antonio E.

Reputation: 359

How do I fix row count sql bad interaction

I've set this sql query on a heroku database "clip" (postgresql)

select count(table1) as colname1, count(table2) as colname2 from table1, table2;

Each table has 2 rows, and the solution of the count is 4 in each column value.

--------------------
colname1 | colname2
  4      |    4
--------------------

What am I missing either on sql basics or DB specific?

Upvotes: 0

Views: 65

Answers (2)

Raseena Abdul
Raseena Abdul

Reputation: 536

This should give you what you are looking for.

select cnt1, cnt2
from ( select count(1) cnt1 from table1)  a,
     ( select count(1) cnt2 from table2) b;

Upvotes: 2

JGH
JGH

Reputation: 17906

you are making a cross join, so it is counting the row count of table 1 multiplied by the row count of table 2.

Upvotes: 2

Related Questions