我已经加入了两个表并获得了一个结果集,该结果集对于相同的id或者说类型有不同的行,但它们具有不同的列值。
EG。
leave_type | max Days | jan | feb | mar | apr
Personal | 12 | 0.00 | 0.00 | 2.00 | 0.00
Personal | 12 | 1.00 | 0.00 | 0.00 | 0.00
Sick | 5 | 0.00 | 0.00 | 1.00 | 0.00
我希望结果如下:
leave_type | max Days | jan | feb | mar | apr
Personal | 12 | 1.00 | 0.00 | 2.00 | 0.00
Sick | 5 | 0.00 | 0.00 | 1.00 | 0.00
如何在mysql中完成?
非常感谢任何帮助。感谢。
答案 0 :(得分:4)
您可以使用GROUP BY
和汇总功能。
SELECT
`leave_type`,
MAX(`days`) AS `max_days`,
MAX(`jan`) AS `jan`,
MAX(`feb`) AS `feb`,
MAX(`mar`) AS `mar`,
MAX(`apr`) AS `apr`
FROM
`table`
GROUP BY
`leave_type`
答案 1 :(得分:2)
您可以使用聚合查询:
select leave_type, maxDays,
sum(jan) as jan, sum(feb) as feb, sum(mar) as mar, sum(apr) as apr
from t
group by leave_type, maxDays ;
答案 2 :(得分:2)
尝试按假类型汇总并获取月份列的总和:
SELECT
leave_type,
MAX(`max Days`) AS max_days,
SUM(jan) AS jan,
SUM(feb) AS feb,
SUM(mar) AS mar,
SUM(apr) AS apr
FROM yourTable
GROUP BY leave_type;
目前尚不清楚给定的假期类型的最大天数是否总是相同。无论如何,我随意保留最大值。