Hello, let's say there is such a table in mysql enter image description here

How to use PHP to bring it to the browser in this form

enter image description here

I know how to sum up the numerical values, but I can’t catch up with a few lines into one. Tell me please

  • one
    select group_concat(name SEPARATOR ' '), sum(val1), sum(val2) from table1 - teran
  • Thank you what you need!) - Alex Stassov
  • designed the answer - teran

1 answer 1

To concatenate strings when grouping, mysql has a function GROUP_CONCAT()

The command syntax is as follows:

 GROUP_CONCAT([DISTINCT] expr [,expr ...] [ORDER BY {unsigned_integer | col_name | expr} [ASC | DESC] [,col_name ...]] [SEPARATOR str_val]) 

That is, to get a list of names without repeating alphabetically separated by a space (comma by default), you should write a query of the form:

  SELECT GROUP_CONCAT(DISTINCT name ORDER BY name ASC SEPARATOR ' ') 

By default, the maximum length of the string to be combined is 1024 and is set by the variable group_concat_max_len