0

I have a single table with a layout similar to this:

code | model | title          | colour
-----|-------|----------------|-------
1001 | 1001  | Product 1 Name | Blue
2001 | 2001  | Product 2 Name | Red
3001 | 3001  | Product 3 Name | Blue
3001 | 3002  | Product 3 Name | Red
3001 | 3003  | Product 3 Name | Green
4001 | 4001  | Product 4 Name | Blue

I want to create an HTML via PHP table of results with this output:

code | model            | title          | colour
-----|------------------|----------------|-----------------
1001 | 1001             | Product 1 Name | Blue
2001 | 2001             | Product 2 Name | Red
3001 | 3001, 3002, 3003 | Product 3 Name | Blue, Red, Green
4001 | 4001             | Product 4 Name | Blue

I have done all the mysqli database connection bits and built a query to throw the results out into a table, that's the easy bit, but the concatenation and grouping - I can't figure out how it's done.

I've tried GROUP BY and GROUP_CONCAT functions but it's not giving me anything I can seem to work with.

4

1 回答 1

2

使用GROUP_CONCAT()

select code, 
       group_concat(model) as model, 
       title, 
       group_concat(colour) as colour
from your_table
group by code, title
于 2013-08-23T08:36:48.383 回答