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?
- For MyISAM tables there is even a far better way, see my answer, which is faster.Pentium10– Pentium102010-02-13 09:52:00 +00:00CommentedFeb 13, 2010 at 9:52
6 Answers6
SELECT ( SELECT COUNT(*) FROM user_table) AS tot_user,( SELECT COUNT(*) FROM cat_table) AS tot_cat,( SELECT COUNT(*) FROM course_table) AS tot_course4 Comments
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.
1 Comment
You can certainly us the a Select Agregation statement as Postulated by Ben James, However This will result in a view with as many columns as you have tables. An alternate method may be as follows:
SELECT COUNT(user_table.id) AS TableCount,'user_table' AS TableSource FROM user_tableUNION SELECT COUNT(cat_table.id) AS TableCount,'cat_table' AS TableSource FROM cat_tableUNION SELECT COUNT(course_table.id) AS TableCount, 'course_table' AS TableSource From course_table;The Nice thing about an approch like this is that you can explicitly write the Union statements and generate a view or create a temp table to hold values that are added consecutively from a Proc cals using variables in place of your table names. I tend to go more with the latter, but it really depends on personal preference and application. If you are sure the tables will never change, you want the data in a single row format, and you will not be adding tables. stick with Ben James' solution. Otherwise I'd advise flexibility, you can always hack a cross tab struc.
Comments
select RTRIM(A.FIELD) from SCHEMA.TABLE A where RTRIM(A.FIELD) = ('10544175A') UNION select RTRIM(A.FIELD) from SCHEMA.TABLE A where RTRIM(A.FIELD) = ('10328189B') UNION select RTRIM(A.FIELD) from SCHEMA.TABLE A where RTRIM(A.FIELD) = ('103498732H')2 Comments
SELECT t1.credit, t2.debit FROM (SELECT Sum(c.total_amount) AS credit FROM credit c WHERE c.status = "a") AS t1, (SELECT Sum(d.total_amount) AS debit FROM debit d WHERE d.status = "a") AS t2Comments
I know this is an old stack but i will post this Multi-SQL select case
SELECT bp.bizid, bp.usrid, bp.website, ROUND((SELECT SUM(rating) FROM ratings WHERE bizid=bp.bizid)/(SELECT COUNT(*) FROM ratings WHERE bizid=bp.bizid), 1) AS 'ratings', (SELECT COUNT(*) FROM bzreviews WHERE bizid=bp.bizid) AS 'ttlreviews', bp.phoneno, als.bizname, (SELECT COUNT(*) FROM endorsment WHERE bizid=bp.bizid) AS 'endorses', als.imgname, bp.`location`, bp.`ownership`, (SELECT COUNT(*) FROM follows WHERE bizid=bp.bizid) AS 'followers', bp.categories, bp.openhours, bp.bizdecri FROM bizprofile AS bp INNER JOIN alluser AS als ON bp.usrid=als.userid WHERE als.usertype='Business'Comments
Explore related questions
See similar questions with these tags.





