MySQL SELECT from multiple tables, multiple GROUP BY and group_concat?

前端 未结 1 1775
温柔的废话
温柔的废话 2021-01-13 18:37

I have three tables which I want to query in MySQ. As follows:

**Table: Leaderboard**
Name  | Score
------------
James | 1
Steve | 2
Dave  | 5

**Table: Acti         


        
相关标签:
1条回答
  • 2021-01-13 19:29
    SELECT Leaderboard.Name,
      (SELECT Actions.Action
       FROM Actions
       WHERE Actions.Name = Leaderboard.Name
         AND Actions.Action LIKE 'Ate%'
       ORDER BY Time DESC
       LIMIT 1
      ) AS Latest_Action,
      GROUP_CONCAT(Items.Item
                   ORDER BY Items.Time DESC
                   SEPARATOR ', '
                  ) AS Items
    FROM Leaderboard
         LEFT JOIN Items ON Leaderboard.Name = Items.Name
    GROUP BY Leaderboard.Name
    HAVING Latest_Action IS NOT NULL
    ORDER BY Leaderboard.Score DESC
    

    Result verified in SQL Fiddle.

    0 讨论(0)
提交回复
热议问题