gpt4 book ai didi

mysql - SQL : Group a Union

转载 作者:行者123 更新时间:2023-11-29 06:24:27 24 4
gpt4 key购买 nike

我有以下查询,它总是给我两行。我想使用 tbluser.userid 将它们分组为一行,是否可以分组联合?

SELECT
SUM(tblfooditem.calories)
FROM tblfooditem
INNER JOIN tbluser ON tblfooditem.userid = tbluser.userid
WHERE tblfooditem.userid=?userid AND tblfooditem.date=?date
GROUP BY tbluser.userid
UNION
SELECT
SUM(tbladdedmealitem.calories)
FROM tbladdedmeal
INNER JOIN tbluser ON tbladdedmeal.userid = tbluser.userid
WHERE tbladdedmeal.userid=?userid AND tbladdedmeal.date=?date
GROUP BY tbluser.userid;

最佳答案

这是可能的。

select sum(s) from (
SELECT
SUM(tblfooditem.calories) S,tbl.userid
FROM tblfooditem
INNER JOIN tbluser ON tblfooditem.userid = tbluser.userid
WHERE tblfooditem.userid=?userid AND tblfooditem.date=?date
GROUP BY tbluser.userid
UNION
SELECT
SUM(tbladdedmealitem.calories) S,tbl.userid
FROM tbladdedmeal
INNER JOIN tbluser ON tbladdedmeal.userid = tbluser.userid
WHERE tbladdedmeal.userid=?userid AND tbladdedmeal.date=?date
GROUP BY tbluser.userid
) Q
group by Q.userid

关于mysql - SQL : Group a Union,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/763206/

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