Selection of fields that repeat in the Mysql database

Asked

Viewed 1,199 times

0

I have a table in the database, with columns id, assunto, mensagem, id_replicante

I want to select all records from this table.

But in the field assunto has several repeating values, and I would like my selection to take only 1 of these repeating.

Example:

On the table:

(1, assunto1, mensagem1, 2)

(2, assunto1, mensagem2, 3)

(3, assunto2, mensagem3, 4)

(4, assunto3, mendagem4, 4)


SELECT * FROM tabela WHERE...

I’d like to keep it that way:

subject 1, subject 2, event3,

  • Regardless of being a beginner in PHP, it would be good to express yourself better, because you can understand in a diversity of ways your question. Read on [Ask], and try to be clearer. You can [Edit] the answer, and add more details to increase the chance of an answer that solves your problem.

  • Probably with GROUP BY you solve the problem.

3 answers

3


If I understand the question:

SELECT * FROM tabela GROUP BY assunto

Exchange the assunto by the desired field. Then you can add the details you need, such as WHERE, ORDER BY, etc, but use the right order:

SELECT * FROM tabela WHERE ... GROUP BY assunto

As mentioned by @Ernando, also remember DISTINCT

SELECT DISTINCT(assunto) FROM tabela WHERE ...

3

You can add a GROUP BY (to group by subjects).

SELECT * FROM table WHERE.... GROUP BY [column Subject].

Let’s say the table structure is: id, subject, message

Ficaria: SELECT * FROM table WHERE ... GROUP BY subject;

  • In addition to group by you can also use DISTINCT SELECT DISTINCT(subject) FROM table;

1

I think you can use SQL GROUP_BY for this. Example:

SELECT * FROM tabela WHERE (condição) GROUP_BY assunto;

Browser other questions tagged

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