Query mysql to take two-line data and merge into one

Asked

Viewed 1,417 times

2

Hello, I am unable to execute a query in mysql. I have a table that has the following fields:

numero | data       | descricao | norma
 001   | 2017-02-28 | Lapis     | NBR
 001   | 2017-02-28 | Lapis     | ISO

As in the example above, there are records that have the same number, date and description, but the norm is different, so the query returns more than one line as a result.

I’ve tried using Distinct, group by but it didn’t work.

How can I mount this query to return me in a single line: the number, the date, the description that are equal and join in the same row the two standards?

1 answer

5


I think what you’re looking for is group_concat.

You can make a select of the fields you want to display, with a group_concat in the field you want to join, and grouping by the fields that are equal.

Sort of like this:

SELECT `numero`, `data`, `descricao`,  group_concat(`norma`) as norma
FROM suatabela
GROUP BY `numero`, `data`, `descricao`
  • 1

    It worked right, thank you very much!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.