I have three tables. Usuários
, Anúncios
and Fotos
.
The query I'm doing is to get the id
of the ad, the Título
, Quantidade
views, and the first photo of this ad from the photo table. Since a single ad can have multiple photos. So far so good, but when I put group by gives me this error:
1055 - Expression # 5 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'vivachapeco.fotosanuncio.foto' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode = only_full_group_by
I need to get a single photo because on the page where this query will appear, the result is repeated only because of the number of photos. So the ad with id
1 will be repeated 5 times because it has 5 photos, when in fact, I would like to have only one photo of it.
The query looks like this:
SELECT 'anuncios'.'id_anuncio', 'anuncios'.'id_user', 'anuncios'.'titulo',
'anuncios'.'visualizacoes', 'fotosanuncio'.'foto'
FROM 'anuncios'
INNER JOIN 'users'
ON 'users'.'id_user' = 'anuncios'.'id_user'
INNER JOIN 'fotosanuncio'
ON 'fotosanuncio'.'id_anuncio' = 'anuncios'.'id_anuncio'
WHERE 'users'.'id_user' = 3
GROUP BY 'fotosanuncio'.'id_anuncio';
If I shoot the group by , it works "correctly", but as I have the ad, 1 and 2 with a photo each and ad 3 with two photos, I get 4 values , instead of returning 3 values since the intention is to group all the results of the photos by advertisement.