2

I want to be able to display a table with a list of people in one column and a string of their orders in another.

People
========
person_id
fname
lname

Orders
========
order_id
person_id
product_id

Products
=========
product_id
productname

I have the following code:

SELECT people.fname, people.lname, products.productname
FROM people
INNER JOIN orders ON people.person_id = orders.person_id
INNER JOIN products ON products.product_id = orders.product_id
ORDER BY lname
...
$rows = $result->num_rows;
while($row = $result->fetch_assoc()) {
    echo "<tr><td>Edit</td>
    <td>".$row["lname"].", ".$row['fname']."</td>
    <td>".$row['productname']."</td></tr>";
    }

This displays:
Smith, Bob | Item 1
Smith, Bob | Item 2
Smith, Bob | Item 3
Roberts, Jill | Item 2
etc..

What I want is for it to display:
Smith, Bob | Item 1, Item 2, Item 3
Roberts, Jill | Item 2

How do I show the name (based on person_id) only once and then a list of their orders in another column?

3
  • 1
    You're probably looking for GROUP_CONCAT on productname in your query. Commented Apr 29, 2015 at 13:48
  • also add your MySql Query Commented Apr 29, 2015 at 13:52
  • I added the SQL. Could you explain the GROUP_CONCAT a little more? I've having trouble finding out how to use it in this situation. (Thank you) Commented Apr 29, 2015 at 14:23

2 Answers 2

1

In MySQL, you have to use the GROUP_CONCAT function. For example something like this:

SELECT CONCAT(LNAME,", ",FNAME), GROUP_CONCAT(PRODUCTNAME)
FROM PEOPLE P, PRODUCTS PR, ORDERS O
WHERE P.PERSON_ID = O.PERSON_ID
AND O.PRODUCT_ID = PR.PRODUCT_ID
GROUP BY (LNAME, FNAME);
Sign up to request clarification or add additional context in comments.

4 Comments

I have tried it, but I'm getting a Parse error. It's rejecting the " ," between lname and fname
I have run this on Mysql it is not giving me an error. Can you tell me wat exact error you are getting.
Ok, I was able to fix the parse error. GROUP_CONCAT is now listing one single row with every product on it. It's not using the loop. In my example, it's giving me: Smith, Bob | Item 1, Item 2, Item 3, Item 2. It's not giving me a row for Jill, and it put her Item 2 attached to Bob's.
With so maneuvering, I was able to get this solution to work. Thanks!
1

Php oriented solution :

You could store your product names in an array, using the person's name as a key :

$rows = $result->num_rows;

$data = array();
while($row = $result->fetch_assoc())
{
     $name = $row["lname"].", ".$row['fname'];
     $data[$name][] = $row['productname'];
}

Then, you'll just have to loop through this array :

foreach($data as $name => $products)
{
     echo "<tr><td>Edit</td><td>".$name."</td>";
     foreach($products as $productname)
     {
         echo "<td>".$productname."</td>";
     }
     echo "<tr>";
}

Comments

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.