简体   繁体   中英

Combine Entry Count in a UNION Query

How do i combine the counts from all the tables being used in a UNION query. This is what i have:

$query = "SELECT COUNT(*) as num
from table_one LEFT JOIN table_constant on table_one.c_id 
= table_constant.c_id 
where table_constant.user_id = '$uid'
UNION
SELECT COUNT(*) as num
from table_two LEFT JOIN table_constant on table_two.c_id 
= table_constant.c_id 
where table_two.added_by = '$uid'
UNION
SELECT COUNT(*) as num
from table_three LEFT JOIN table_constant ON table_three.c_id 
= table_constant.c_id
where table_constant.user_id = '$uid'
UNION
SELECT COUNT(*) as num
from table_four LEFT JOIN table_constant ON table_four.c_id 
= table_constant.c_id
where table_constant.user_id = '$uid'
ORDER BY date_time_added DESC";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

Wrap the whole thing in yet another query and do the summation there.

SELECT sum(num)
FROM ( ... union queries here ...) as subquery;

Or loop over the returned rows in PHP and do the summation yourself.

There must be a better way to write that :/. A union is very powerful, but you are calling 4 selects in a single query, and if that is run every page, it will really hurt performance.

To answer you question, something like:

SELECT
    SUM (mnTbl.num) as sumNum
FROM
    (
        SELECT
            COUNT(*) as num
        FROM
                table_one
            LEFT JOIN
                table_constant
            ON
                table_one.c_id = table_constant.c_id 
        WHERE
            table_constant.user_id = '$uid'
    UNION
        SELECT
            COUNT(*) as num
        FROM
                table_two
            LEFT JOIN
                table_constant
            ON
                table_two.c_id = table_constant.c_id 
        WHERE
            table_two.added_by = '$uid'
    UNION
        SELECT
            COUNT(*) as num
        FROM
                table_three
            LEFT JOIN
                table_constant
            ON
                table_three.c_id = table_constant.c_id
        WHERE
            table_constant.user_id = '$uid'
    UNION
        SELECT
            COUNT(*) as num
        FROM
                table_four
            LEFT JOIN
                table_constant
            ON
                table_four.c_id  = table_constant.c_id
        WHERE
            table_constant.user_id = '$uid'
    ) as mnTbl
ORDER BY
    date_time_added DESC

Did you try to put count outside and apply it on sub query containing all tables union result.

 SELECT COUNT(*) FROM (SELECT ...) as abc

Or try this out

Select mytable .userid, sum(mytable .subcount) as totalcount from
(
select userid, count(*) as subcount from table1 group by userid
union all
select userid, count(*) as subcount from table2 group by userid
)
as mytable 
group by mytable .userid

or try using FULL OUTER JOIN instead of union it will give you the same result..

 SELECT Count(UserID), UserId
 FROM MyTable1
 GROUP BY MyTable1.UserID
 UNION
 SELECT Count(UserID), UserId
 FROM MyTable2
 FULL OUTER JOIN MyTable2 ON (MyTable1.UserId=MyTable2.UserId)
 GROUP BY MyTable2.UserID

Updated answer:

IF Your query is working fine follow my first option that i gave means

 select count(*) from (your query) as pagecount..

Then your query would be like this.....

  select count(*) from
  (
  SELECT COUNT(*) as num
   from table_one LEFT JOIN table_constant on table_one.c_id 
  = table_constant.c_id 
  where table_constant.user_id = '$uid'
 UNION
 SELECT COUNT(*) as num
from table_two LEFT JOIN table_constant on table_two.c_id 
 = table_constant.c_id 
 where table_two.added_by = '$uid'
UNION
SELECT COUNT(*) as num
from table_three LEFT JOIN table_constant ON table_three.c_id 
= table_constant.c_id
where table_constant.user_id = '$uid'
UNION
SELECT COUNT(*) as num
from table_four LEFT JOIN table_constant ON table_four.c_id 
= table_constant.c_id
where table_constant.user_id = '$uid'
ORDER BY date_time_added DESC") as pagecount

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM