0

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

4
  • 3
    Have you tried anything yourself? What did you manage? Commented Jun 8, 2015 at 10:54
  • A similar question came up recently. See this: stackoverflow.com/questions/30699602/… Commented Jun 8, 2015 at 10:58
  • Write your query we will edit it. Commented Jun 8, 2015 at 10:59
  • You should handle the display in the application (php) code. Your format is not really a "database" format. For instance, you are missing different types in one column. Commented Jun 8, 2015 at 11:40

2 Answers 2

0

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
Sign up to request clarification or add additional context in comments.

1 Comment

this query will not return all the values, the mouse,book,pen and pencil will not be returned. you have to concat the names into one value
0
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

1 Comment

I looking for a solution without group_concat(). Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.