如何在查询中添加新列以显示少于当前日期的数据?

时间:2019-02-21 11:45:09

标签: mysql sql

下面显示的我当前的查询显示了用户详细信息及其注册的事件,但是在他们参加活动后,我想要另一列显示WHERE Start_Date

SELECT users.username, users.user_firstname, users.user_lastname, users.user_role,
       GROUP_CONCAT( Events.Event_Name ) AS Event_Names, COUNT(Events.Event_Name) AS Event_Count
FROM users LEFT JOIN 
     user_events
     ON users.id = user_events.id LEFT JOIN
     Events
     ON Events.Event_ID = user_events.Event_ID
GROUP BY users.username, users.user_firstname, users.user_lastname, users.user_role

理想情况:

| username| user_firstname | user_lastname | user_role | Event_Name |PreEve                               
| M       | jo             | mccann        | employee  | baking,run | fight

1 个答案:

答案 0 :(得分:0)

这是您想要的吗?

SELECT u.username, u.user_firstname, u.user_lastname, u.user_role,
       GROUP_CONCAT(CASE WHEN e.Start_Date < CURDATE() THEN e.Event_Name END) AS Previous_Event_Names, 
       GROUP_CONCAT(CASE WHEN e.Start_Date >= CURDATE() THEN e.Event_Name END) AS Future_Event_Names, 
       COUNT(e.Event_Name) AS Event_Count
FROM users u LEFT JOIN 
     user_events ue
     ON u.id = ue.id LEFT JOIN
     Events e
     ON e.Event_ID = ue.Event_ID
GROUP BY u.username, u.user_firstname, u.user_lastname, u.user_role;

GROUP_CONCAT()忽略了NULL值。