Multiple select statements in Single query

后端 未结 6 726
青春惊慌失措
青春惊慌失措 2020-11-28 02:58

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(         


        
相关标签:
6条回答
  • 2020-11-28 03:10

    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.

    0 讨论(0)
  • 2020-11-28 03:12
    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')
    
    0 讨论(0)
  • 2020-11-28 03:15
    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
    
    0 讨论(0)
  • 2020-11-28 03:17

    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'
    
    0 讨论(0)
  • 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_table
    UNION SELECT COUNT(cat_table.id) AS TableCount,'cat_table' AS TableSource FROM cat_table
    UNION 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.

    0 讨论(0)
  • 2020-11-28 03:23
    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 
    
    0 讨论(0)
提交回复
热议问题