Concatenate what is equal and separate what is different with MYSQL/PHP

Asked

Viewed 151 times

1

Well, I think it’s gonna get a little messy maybe, but I need to do this: In an example table I am inserting several names in the name column, in which many of these names are equal, I would like to join all the same 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 just Luca João and Luca Jose. I’m trying to do this with PHP, a SELECT WHERE john would solve the problem in this case, but this would have to be done automatically if he had other names he did the same thing.

  • Put the code you have on.

  • I don’t have any code at the moment, that’s just an idea. I am seeing the possibilities to try to organize this in a viable way when creating DB, in Sso I would just like to receive an example if what I am trying to do is possible...

1 answer

3


I understood that there are two columns, one for name and one for surname. I am right?
If so, it is possible to do so:

  1. Make a SELECT DISTINCT of the column name;
  2. Scroll through 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 loop)
  • I didn’t know there was such a thing in MYSQL, thank you very much!

  • Just clarifying that the loop should be made in the PHP with the Resultset of the first query with the DISTINCT.

Browser other questions tagged

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