简体   繁体   中英

Get datewise result as grouped from MySql Database

I would like to select data from a Mysql table as grouped data. I have a table like below in MySql Database.

+-----------------------+
|   date         name   |
+-----------------------+
| 12/02/2015   computer |
| 12/02/2015   mouse    |
| 12/02/2015   Book     |
| 13/02/2015   Paper    |
| 13/02/2015   Pen      |
| 13/02/2015   Pencil   |
+-----------------------+

I would like to display data like below.

+---------------+
| date          |
+---------------+
| 12/02/2015    |
+---------------+
| name          |
+---------------+
| computer      |
| mouse         |
| Book          |
+---------------+
| date          |
+---------------+
| 13/02/2015    |
+---------------+
| name          |
+---------------+
| Paper         |
| Pen           |
| Pencil        |
+---------------+

I need the Query. Thanks

Update

I would not like to use group_concat() .I think there are other ways to do that. There are some limitation of group_concat() like it can concate certain amount of text and I am facing that type of problem.

I think here are some solutions MySQL Query with Group By

Thanks

Use GROUP BY conditional SQL function to perform this task. It will group the results of your query by date, so you will have an order of names sorted by date :

SELECT T.date, T.name
FROM   TABLE T
GROUPE BY T.date, T.name -- T.name is not necessary

Which will be display as below :

DATE       NAME
-------------------
12/02/2015 Computer
12/02/2015 Mouse
12/02/2015 Book
13/02/2015 Paper
13/02/2015 Pen
13/02/2015 Pencil
select date, group_concat(name) as names
from table
group by date

the result will be

12/02/2015 compute,mouse,book

13/02/2015 paper,pen,pencil

then you can loop the result and explode the names value into array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM