1

I have sample data in database with multiple different name,date and menu... I need to proceed data with the same name and date, but each data has a different menu...

DATABASE

enter image description here

the problem is that I need to store all the menu from the same name and date as a new variable since I need to call it for table view in web, and the table need to be look like this :

TABLE

enter image description here

I already using array to store the data menu for each same name and date, so the menu come out like this :

enter image description here

I'm really confused at how to continue make approach after this part,

How do I get output --> Candy Cake Cheese , so I can call it in table (?)

I think I can't seem to just use concat_ws in my query, since the values are from the same field of array, or is there another solution so I can get the output table I need ?? Thanks :)

5
  • 1
    Which type of output you needed please explain clearly Commented Nov 6, 2013 at 4:39
  • @NathanSrivi I'm sorry... I need the output in a single variable.. is that possible ? Commented Nov 6, 2013 at 4:46
  • Please, tell which database are you using. It's relevant to the question. :) Commented Nov 6, 2013 at 4:48
  • @PauloFreitas Hmm.. I think already give the sample database table above.. O.o It's from a table in database... Ohh...MySQL database... is that what u mean ? It's MYSQL... sorry for not wrote it down :) Commented Nov 6, 2013 at 4:54
  • 1
    It's generally better to retain the database table that you have separate, so that you can do queries like "what date did Sylvie have Cake?" (which is harder with the view you're proposing); but to use something like GROUP_CONCAT() when retrieving data to make it seem like the multiple records contain one set of data Commented Nov 6, 2013 at 5:02

2 Answers 2

5

You're probably looking for MySQL GROUP_CONCAT() aggregate function:

  SELECT Name, Date, GROUP_CONCAT(Menu) AS Menu
    FROM Table
GROUP BY Name, Date

This will give your data like that:

|--------|------------|-------------------|
| Name   | Date       | Menu              |
|--------|------------|-------------------|
| Sylvie | 2001-01-01 | Candy,Cake,Cheese |
| Sylvie | 2001-02-01 | Milk,Tea          |
|--------|------------|-------------------|

From that you just need to use PHP explode() function to get each menu record as an array.

Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome! I was guessing exactly this when I asked you which database you was using! :)
1

Use serialize or json_encode to store the value into the database and use unserialize or json_decode when you get the value from the database.

1 Comment

I'm looking at it now... thanks for the references.. :) I really need to read more

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.