2011-06-26 45 views
0

如何組合來自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]; 

回答

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,而不是工會,它會給你同樣的結果..

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 

更新答案:

如果您的查詢工作正常,請按照我的第一個選項,即gav E表示

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 
+0

嘿。我需要在上面貼出的$ total_pages變量內的計數。我不知道該怎麼做。 – KPO

+0

我已經更新了我的答案,請檢查一下。如果您在查詢中符合工會規則,請執行此操作。從(你的查詢)中選擇count(*)作爲mycount。它爲我工作希望現在你將擺脫你的問題.. – Syeda

+0

一件事Syeda。我如何把它放入一個變量? – KPO

2

將所有東西包裹在另一個查詢中,並在那裏進行求和。

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

或循環遍歷PHP中的返回行,並自己進行求和。

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 
+0

OK嘗試你現在。 – KPO

+0

希望它的工作,不能真正測試sql語句與一些示例數據 – Ben

+0

我爲mnTbl.num做了什麼?表名? – KPO