0
I have three tables. Usuários, Anúncios and Fotos.
To query what I’m doing is to catch the id of the Ad, the Título, to Quantidade of 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 the group by give me this mistake:
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 will be displayed query, the result is repeated only because of the amount of photos. Then, 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.
To query is 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`;
Already if I take the group by, works "correctly", however, as I have the ad, 1 and 2 with a photo each and the ad 3 with two photos, returns me 4 values, instead of returning 3 values since the intention is to group all the results of the photos by ad.
As a rule, everything you have within select has to be in group by
– Don't Panic
Everson, and what would group by then look like? I added the
fotosanuncios.id_anuncioin select and still giving the same error.– user60485
group by anuncios.id_anuncio, anuncios.id_user, anuncios.titulo, anuncios.visualizacoes, fotosanuncio.foto, will stop generating error, but may not bring the records you need.– Don't Panic
I tested here and returns as if I didn’t have the group by. Returns all the data then repeating the ad that has more than one photo.
– user60485
As is the table fotosanuncio, it possesses some id? put its structure
– Caique Romero