在UNION查询中组合条目计数

时间:2011-06-26 04:16:37

标签: php mysql count union

如何组合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];

3 个答案:

答案 0 :(得分:2)

将整个事物包装在另一个查询中并在那里进行求和。

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

或者在PHP中循环返回的行并自己进行求和。

答案 1 :(得分:1)

必须有更好的方法来写:/。联合是非常强大的,但是你在一个查询中调用了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

答案 2 :(得分:1)

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

 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而不是union,它会给你相同的结果..

 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