Concatenating and grouping results from a mysql query

Scott Brown

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.

juergen d

Use GROUP_CONCAT()

select code, 
       group_concat(model) as model, 
       title, 
       group_concat(colour) as colour
from your_table
group by code, title

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

No results from SQL Query

분류에서Dev

Trying to filter results from a query using CASE

분류에서Dev

Importing query results from another database

분류에서Dev

Cross referencing results from query's

분류에서Dev

Select results from one query to another

분류에서Dev

Create an id array from mysql results

분류에서Dev

MySQL Random Select Query with limit returning different number of results (undesired)

분류에서Dev

MySQL ignore query results with column that appears more than once

분류에서Dev

What is an efficient way to exclude all results from a sql query?

분류에서Dev

MySQL SQL Query to Delete a String From Records

분류에서Dev

Search query from multiple fields MySQL

분류에서Dev

MySQL query, sum data from table

분류에서Dev

MySQL Best select query from two table

분류에서Dev

Modify and rename JSON elements from MySQL query

분류에서Dev

MySQL query error in selecting data from database

분류에서Dev

Select data from logged in user with a mySQL query

분류에서Dev

Removing \n from result of mysql query

분류에서Dev

MySql multiple select from two tables and join their results to a third table

분류에서Dev

PHP/MySql HTML Table Grouping

분류에서Dev

MySQL grouping and getting newest rows

분류에서Dev

Access query based on query results

분류에서Dev

mysql filtering results with subquery results

분류에서Dev

ORDER BY results of a query (nested?)

분류에서Dev

Using the results of one query as a subarray in the results of another

분류에서Dev

Concatenating files from a directory using cat gives "No such file or directory" error

분류에서Dev

NodeJS: Getting the values from the result of a multiple statement MySQL query

분류에서Dev

How to make a mysql query from c# and save it in a variable?

분류에서Dev

How to query from Mysql DATETIME column based on day name

분류에서Dev

How to query from Mysql DATETIME column based on day name

Related 관련 기사

  1. 1

    No results from SQL Query

  2. 2

    Trying to filter results from a query using CASE

  3. 3

    Importing query results from another database

  4. 4

    Cross referencing results from query's

  5. 5

    Select results from one query to another

  6. 6

    Create an id array from mysql results

  7. 7

    MySQL Random Select Query with limit returning different number of results (undesired)

  8. 8

    MySQL ignore query results with column that appears more than once

  9. 9

    What is an efficient way to exclude all results from a sql query?

  10. 10

    MySQL SQL Query to Delete a String From Records

  11. 11

    Search query from multiple fields MySQL

  12. 12

    MySQL query, sum data from table

  13. 13

    MySQL Best select query from two table

  14. 14

    Modify and rename JSON elements from MySQL query

  15. 15

    MySQL query error in selecting data from database

  16. 16

    Select data from logged in user with a mySQL query

  17. 17

    Removing \n from result of mysql query

  18. 18

    MySql multiple select from two tables and join their results to a third table

  19. 19

    PHP/MySql HTML Table Grouping

  20. 20

    MySQL grouping and getting newest rows

  21. 21

    Access query based on query results

  22. 22

    mysql filtering results with subquery results

  23. 23

    ORDER BY results of a query (nested?)

  24. 24

    Using the results of one query as a subarray in the results of another

  25. 25

    Concatenating files from a directory using cat gives "No such file or directory" error

  26. 26

    NodeJS: Getting the values from the result of a multiple statement MySQL query

  27. 27

    How to make a mysql query from c# and save it in a variable?

  28. 28

    How to query from Mysql DATETIME column based on day name

  29. 29

    How to query from Mysql DATETIME column based on day name

뜨겁다태그

보관