Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple select statements in Single query

Tags:

mysql

I am generating a report in php (mysql),

ex:

`select count(id) as tot_user from user_table  select count(id) as tot_cat from cat_table  select count(id) as tot_course from course_table` 

Like this I have 12 tables.

Can i make it in single query. If i did? Process gets slow?

like image 424
boss Avatar asked Nov 21 '09 11:11

boss


2 Answers

SELECT  (     SELECT COUNT(*)     FROM   user_table ) AS tot_user, (     SELECT COUNT(*)     FROM   cat_table ) AS tot_cat, (     SELECT COUNT(*)     FROM   course_table ) AS tot_course 
like image 198
sathish Avatar answered Oct 06 '22 00:10

sathish


If you use MyISAM tables, the fastest way is querying directly the stats:

select table_name, table_rows       from information_schema.tables  where       table_schema='databasename' and       table_name in ('user_table','cat_table','course_table') 

If you have InnoDB you have to query with count() as the reported value in information_schema.tables is wrong.

like image 20
Pentium10 Avatar answered Oct 05 '22 23:10

Pentium10