简体   繁体   English

在UNION查询中合并条目计数

[英]Combine Entry Count in a UNION Query

How do i combine the counts from all the tables being used in a UNION query. 如何合并来自UNION查询中所有表的计数。 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. 或在PHP中循环返回的行,然后自己进行求和。

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. 联合非常强大,但是您要在单个查询中调用4个选择,并且如果每个页面都运行该选择,则确实会损害性能。

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.. 或尝试使用FULL OUTER JOIN代替工会,它会给您相同的结果。

 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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