gpt4 book ai didi

php - 在 UNION 查询中组合条目计数

转载 作者:太空宇宙 更新时间:2023-11-03 11:14:04 24 4
gpt4 key购买 nike

我如何合并 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 中遍历返回的行并自己进行求和。

关于php - 在 UNION 查询中组合条目计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6482104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com