Selecting fields that are repeated in the MySql database

0

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

I want to select all the records in this table.

However, in the field assunto it has several values that repeat, and I would like my selection to take only 1 of those that repeat.

Example:

In the table:

(1, assunto1, mensagem1, 2)

(2, assunto1, mensagem2, 3)

(3, assunto2, mensagem3, 4)

(4, assunto3, mendagem4, 4)


SELECT * FROM tabela WHERE...

I would like it to look like this:

subject1, subject2, subject3,

    
asked by anonymous 18.09.2014 / 22:04

3 answers

2

If I understand the question:

SELECT * FROM tabela GROUP BY assunto

Change assunto to 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 @fernando, also remember DISTINCT

SELECT DISTINCT(assunto) FROM tabela WHERE ...
    
18.09.2014 / 22:08
2

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

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

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

I would get: SELECT * FROM table WHERE ... GROUP BY subject;

    
18.09.2014 / 22:08
0

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

SELECT * FROM tabela WHERE (condição) GROUP_BY assunto;
    
18.09.2014 / 22:08