H. Ferrence
H. Ferrence

Reputation: 8106

How to group rows and list a certain columns values

I have a Table that has rows of dates & times. I want to produce a list of all the times for each unique date in the table.

Here is the table:

DATE       TIME
2000-01-01 09:00:00
2000-01-01 13:00:00
2000-07-18 09:00:00
2000-07-18 13:00:00
2000-12-31 10:31:00
2000-12-31 14:17:00
2000-12-31 21:42:00

I would the result set to look like this:

2000-01-01, 09:00:00, 13:00:00
2000-07-18, 09:00:00, 13:00:00
2000-12-31, 10:31:00, 14:17:00, 21:42:00

Is it possible?

Amended: Is it also possible to produce this result set in 1 query?

2000-01-01, 2000-07-18, 09:00:00, 13:00:00
2000-12-31, 10:31:00, 14:17:00, 21:42:00

Upvotes: 0

Views: 56

Answers (2)

Grim...
Grim...

Reputation: 16953

You need GROUP_CONCAT: https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

Something like:

SELECT DATE, GROUP_CONCAT(TIME) FROM tablename GROUP BY DATE

Upvotes: 1

Barmar
Barmar

Reputation: 780724

SELECT Date, GROUP_CONCAT(Time) AS Times
FROM YourTable
GROUP BY Date

Upvotes: 1

Related Questions