我有 4列的 MySQL 表: completedtasks,duration,date和educationDepartment 。我的第一个目标是在我的特定日期显示我的条目以工作日的名称。我用以下查询完成了这个:
SELECT DAYNAME(date) AS DAY,
completedtasks,
duration,
educationDepartment
FROM programm_completedtask;
现在我想总结一下我所拥有的信息,到星期几。假设我有一个包含以下条目的表格,例如:
+-----------+-----------------------+----------+---------------------+
| Day | completedtasks | duration | educationDepartment |
+-----------+-----------------------+----------+---------------------+
| Monday | Some stuff to do | 6 | A Department |
| Tuesday | Some other stuff to do| 8 | Another Department |
| Wednesday | Some other stuff to do| 6 | Test Department |
| Thursday | Being lazy | 8 | Another Test |
| Friday | A completed task | 8 | Another Test |
| Monday | Some entry | 2 | A Test |
| Wednesday | Test entry | 2 | Another Test |
+-----------+-----------------------+----------+---------------------+
我尝试了几个查询,但那些没有给我我想要的结果。当我使用 GROUP BY 时,它只是给了我表中的条目,如下所示:
+-----------+-----------------------+----------+---------------------+
| Day | completedtasks | duration | educationDepartment |
+-----------+-----------------------+----------+---------------------+
| Monday | Some stuff to do | 6 | A Department |
+-----------+-----------------------+----------+---------------------+
此查询:
SELECT DAYNAME(date) AS Day,
completedtasks,
duration,
educationDepartment
FROM programm_completedtask
WHERE DAYNAME(date)
LIKE "Monday";
给了我以下结果。
+-----------+-----------------------+----------+---------------------+
| Day | completedtasks | duration | educationDepartment |
+-----------+-----------------------+----------+---------------------+
| Monday | Some stuff to do | 6 | A Department |
| Monday | Some entry | 2 | A Test |
+-----------+-----------------------+----------+---------------------+
我的目标是获得结果,如下所示:
+-----------+-----------------------+----------+---------------------+
| Day | completedtasks | duration | educationDepartment |
+-----------+-----------------------+----------+---------------------+
| Monday | Some stuff to do, | 6,2| A Department, A Test|
| | Some entry | | |
+-----------+-----------------------+----------+---------------------+
所以,我想总结一个特定工作日的条目,我希望它在一行中。我希望这在某种程度上是可能的。我非常感谢你的帮助!
答案 0 :(得分:2)
SELECT DAYNAME(date) as `Day`,
GROUP_CONCAT(completedtasks ) as completedTasks,
GROUP_CONCAT(duration ) as duration ,
GROUP_CONCAT(educationDepartment ) as educationDepartment ,
FROM tableName
GROUP BY DAYNAME(date)
无需指定SEPARATOR,因为默认为逗号字符
答案 1 :(得分:1)
这应该有效:
SELECT DAYNAME(date) AS Day,
group_concat(completedtasks separator ',') as completedtasks,
group_concat(duration separator ',') as duration,
group_concat(educationDepartment separator ',') as educationdepartment
FROM programm_completedtask
group by dayname(date);