MySQL将列组合成行

时间:2017-04-28 05:31:14

标签: mysql sql database relational-database

我运行了一个SUM查询来实现以下2列和行:

sum1 | sum2
10     20

我运行的查询如下:

SELECT SUM(minutes) as "sum1", SUM(hoursWorked) AS "sum2"
FROM  entries
JOIN  employees on employeeID = employees.userID 
WHERE YEARWEEK(dateCreated) = YEARWEEK(NOW()) 
ORDER BY employeeID;

我想将值组合成一列,以获得以下内容:

sums
10
20

如何开始这一点有点失落。搜索导致了Concat显然无法正常工作。

任何人都可以帮忙建议一个方法来执行此操作,或者我应该搜索的正确函数吗?

编辑:根据以下建议,我通过执行以下操作解决了这个问题:

SELECT SUM(minutes) as "sum1"
FROM   entries
JOIN   employees on employeeID = employees.userID 
WHERE  YEARWEEK(dateCreated) = YEARWEEK(NOW())
UNION
SELECT SUM(hoursWorked) AS "sum2"
FROM   entries
JOIN   employees on employeeID = employees.userID 
WHERE  YEARWEEK(dateCreated) = YEARWEEK(NOW()) 

2 个答案:

答案 0 :(得分:2)

使用UNION ALL方法:

SELECT sum1 Sums 
FROM your_tablename   
UNION ALL
SELECT sum2 Sums
FROM your_tablename    

答案 1 :(得分:1)

我认为您正在寻找的是UNION操作可能会对您有所帮助。

请查看以下资源:rdp