繁体   English   中英

在UNION查询中合并条目计数

[英]Combine Entry Count in a UNION Query

如何合并来自UNION查询中所有表的计数。 这就是我所拥有的:

$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];

将整个内容包装在另一个查询中,然后在其中进行求和。

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

或在PHP中循环返回的行,然后自己进行求和。

必须有一个更好的方法来写:/。 联合非常强大,但是您要在单个查询中调用4个选择,并且如果每个页面都运行该选择,则确实会损害性能。

要回答您的问题,类似:

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

您是否尝试将计数放在外面并将其应用于包含所有表联合结果的子查询。

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

或者尝试一下

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

或尝试使用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

更新的答案:

如果您的查询工作正常,请遵循我给出的第一个选项

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

然后您的查询将是这样.....

  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