Hello, I would like the help of the collaborators. I have a table "catalog" related to another table 'images'. Each record in "catalog" has n pictures. I need in a SELECT with INNER JOIN images, only 3 images are returned for each row of the catalog table.
I've been thinking about solving the issue with something like
SELECT c. cat_id
as id, c. cat_nome_fantasia
the company, i.img_name the image FROM catalogo
c
LEFT JOIN (SELECT * FROM images WHERE fk_catalogo_id = c.cat_id LIMIT 3) i ON c.cat_id = i.fk_catalogo_id
However the cat_id column in the subquery (WHERE fk_catalogo_id = c.cat_id ) is not recognized. Do you know if there is any way to change that? Type make the Global column.
Then I tried
SELECT ( @valor_id: = c. cat_id
) the id, c. cat_nome_fantasia
the company, i.img_name the image FROM catalogo
c
LEFT JOIN (SELECT * FROM images WHERE fk_catalogo_id = @valor_id LIMIT 3) i ON c.cat_id = i.fk_catalogo_id
But @valor_id seems not to have been set because it did not return records.
Finally I tried to set the variable.
SET @valor_id: = 1;
SELECT ( @valor_id: = c. cat_id
) the id, c. cat_nome_fantasia
the company, i.img_name the image FROM catalogo
c
LEFT JOIN (SELECT * FROM images WHERE fk_catalogo_id = @valor_id LIMIT 3) i ON c.cat_id = i.fk_catalogo_id
In this case it almost worked , searched for the images with LIMIT.
Does anyone have any idea how to solve it? Thanks