Justin
Justin

Reputation: 45390

MySQL get count() from separate tables

I am trying to get a simple count of rows in three different MySQL tables with a single query. Here is what I have:

SELECT count(s.`id`) AS c1,
       count(g.`id`) AS c2,
       count(r.`id`) AS c3
  FROM table1 s,
       table2 g,
       table3 r

This does not work though, I think its multiplying all three totals instead. Thanks for the help.

Upvotes: 2

Views: 101

Answers (1)

fthiella
fthiella

Reputation: 49089

If tables have no relationship an cannot be joined, try this:

SELECT
  (select count(id) from table1) as c1,
  (select count(id) from table2) as c2,
  (select count(id) from table3) as c3

Upvotes: 4

Related Questions