how to select only 1 record of each id

2

After doing an inner join between the user (1) and image (N) tables, the following table is returned (the id refers to the user id and the file_name to the image file):

| id | filename

| 12 | img1.png

| 12 | img2.png

| 13 | img3.png

| 13 | img4.png

How to retrieve only 1 image from each id? (without using the where = id, because at the time of the query, I do not have the id to do the comparison.)

    
asked by anonymous 06.11.2015 / 18:24

2 answers

2

I would do it as follows:

SELECT u.*, i.imagem as imagem_usuario FROM usuario u 
LEFT JOIN imagem i ON(i.id_usuario=u.id) GROUP BY u.id_usuario ORDER BY u.id_usuario ASC

In this way, we can also sort by id, to see if it takes the first record or the last record.

    
06.11.2015 / 18:37
1

Use a Group By Id

Something like

Select * from Tabela
Group By Id
    
06.11.2015 / 18:28