Concatenate what is the same and separate what is different with MYSQL / PHP

1

Well, I think it might be a bit confusing maybe, but I need to do the following: In an example table I'm inserting multiple names in the name column, in which many of these names are the same, I'd like to join all the equal names in one query, and those that are different in another. For example:

nome sobrenome
joão Silva
joão Pereira
joão Santos
Luca João
Luca Jose

In this case, query 1 would result in: João Silva, João Pereira, João Santos and query 2 would be only Luca João and Luca Jose. I'm trying to do this with PHP, a SELECT WHERE joon would solve the problem in that case, however this would have to be done automatically, if it had other names it would do the same thing.

    
asked by anonymous 10.11.2017 / 14:10

1 answer

3

I understood that there are two columns, one for name and one for last name. Am I right?
If so, you can do this:

  • Make a SELECT DISTINCT of the column name;
  • Trace the result of this SELECT in a loop and for each record, create a new query with the following SQL:
    SELECT NAME FROM TABLE WHERE NAME = (Name that is coming from the loop)
  • 10.11.2017 / 14:20