134

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?

askedNov 21, 2009 at 11:03
boss's user avatar
1
  • For MyISAM tables there is even a far better way, see my answer, which is faster.CommentedFeb 13, 2010 at 9:52

6 Answers6

303
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
DanB's user avatar
DanB
2,1121 gold badge14 silver badges25 bronze badges
answeredNov 21, 2009 at 11:46
sathish's user avatar
Sign up to request clarification or add additional context in comments.

4 Comments

For MyISAM tables there is even a far better way, see my answer.
"Operand should contain 1 column(s)" - only if your merged tables differ in columns count. They should match. 1 column per table in this example.
this only works if you're returning a single output from each sub-query
@Prachi what is the solution for returning multiple columns?
26

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.

answeredFeb 13, 2010 at 9:37
Pentium10's user avatar

1 Comment

20

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.

answeredNov 21, 2009 at 11:37
Miguel Castaneda's user avatar

Comments

11
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')
sloth's user avatar
sloth
101k21 gold badges183 silver badges224 bronze badges
answeredAug 19, 2011 at 21:10
net.tunneler's user avatar

2 Comments

What question is answered by this?
This is a combination of Miguel Castaneda's UNION() solution and Pentium10's INFORMATION_SCHEMA solution. Please cite answers you use.
7
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 t2
answeredJun 18, 2019 at 12:41
AngularJMK's user avatar

Comments

2

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'
answeredOct 5, 2018 at 13:53
Niclausel's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.