我想只输出2行 - 标题和总和。标题来自orbiting_group_types表,而SUM在sql查询期间计算。这是我的查询输出所有orbiting_group_types连接总和列:
SELECT
*
FROM
orbiting_group_types ogt
LEFT JOIN (
SELECT
SUM (val),
orbiting_group_type_id
FROM
report_orbiting_vals subrov
WHERE
subrov.orbiting_group_type_id = 4
GROUP BY
orbiting_group_type_id
) rov ON ogt. ID = rov.orbiting_group_type_id
我想只输出标题和总和列。我应该如何修改我的查询?
答案 0 :(得分:2)
试试这个:
SELECT
ogt.title,
rov.summ
FROM
orbiting_group_types ogt
LEFT JOIN (
SELECT
SUM (val) AS summ,
orbiting_group_type_id
FROM
report_orbiting_vals subrov
WHERE
subrov.orbiting_group_type_id = 4
GROUP BY
orbiting_group_type_id
) rov ON ogt. ID = rov.orbiting_group_type_id
答案 1 :(得分:1)
假设您当前的查询有效,请更改您的选择。我还在子查询中命名了您的计算字段
SELECT
ogt.Title
,rov.SumValue
FROM
orbiting_group_types ogt
LEFT JOIN (
SELECT
SUM (val) SumValue,
orbiting_group_type_id
FROM
report_orbiting_vals subrov
WHERE
subrov.orbiting_group_type_id = 4
GROUP BY
orbiting_group_type_id
) rov ON ogt.ID = rov.orbiting_group_type_id