簡體   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