How to join result of two tables 1 - n

1

I have the following problem.

1 client table that has relation to 1 table photos (respective client photos)

relationship 1 client for "n" photos;

  

In short:

     

Client has id, name.

     

photo has client_id, content (bytes).

I can not make a query that fetches all clients, but I want only one photo of each client

I need to select return this from each client:

RESULT THAT I EXPECT:

cliente.id, 
cliente.nome, 
foto.conteudo //uma foto para cada cliente.

WHY?   I'm making a list of clients that shows only the photo and the name on the home page.

    
asked by anonymous 12.01.2015 / 22:31

1 answer

3

You can group the result by client:

SELECT
    cliente.id, cliente.nome, foto.conteudo
FROM cliente
    LEFT JOIN foto /* use INNER JOIN se todo cliente tiver 1+ foto */
    ON foto.cliente_id = cliente.id
GROUP BY cliente.id
    
12.01.2015 / 22:41