0
Good evening, I may have this answer somewhere but I spent all day researching, using various examples and nothing.
I have 2 tables, one of products and another with the models being that 1 product can have N models, each model has its price, so the same product can have N different values depending on the model.
With this, I need to search the 2 tables picking only 1 product and 1 model, this model being the one with the lowest price. First I did it:
SELECT `produto`.`titulo`, `modelo`.`sku`, `modelo`.`preco_de`, `modelo`.`preco_por` FROM `produto`
INNER JOIN `modelo` ON `modelo`.`codigo_produto` = `produto`.`codigo`
ORDER BY `modelo`.`preco_por` ASC LIMIT 0, 20
Here he took the product and all models, for example, a refrigerator has 2 models, 110v and 220v, then listed the refrigerator 2 times, one with the price of 110v and the other with the price of 220v, with that I changed the code to:
SELECT `produto`.`titulo`, `modelo`.`sku`, `modelo`.`preco_de`, `modelo`.`preco_por` FROM `produto`
INNER JOIN `modelo` ON `modelo`.`codigo_produto` = `produto`.`codigo`
GROUP BY `produto`.`codigo`
ORDER BY `modelo`.`preco_por` ASC LIMIT 0, 20
Blz, now I have 1 record per product, but now ORDER BY has been ignored because GROUP BY always takes the first record.
I need to do this to always list the product with your model that has the lowest price but I don’t know what else to do, I’m almost giving up and searching only the product and then doing 20 searches to get the lowest price of each one, for me this is a defeat and I didn’t want to do, but I’ve already missed a whole day of the project that’s already pretty tight.
Edited 01:
Using the MIN(modelo
.preco_por
) AS preco_por
indicated by the friend below, he really picks the lowest price but keeps getting the record that comes first in the bank.
Edited 02: I’m using Mariadb 10.5.8 which is the newest version in the Docker repository.
Take a look at the three questions listed in the top box, I believe the answers that are in them will show you the way.
– bfavaretto