I have a few shops which have products. I want to to write a query where I put product_id
and I get max(id)` offer from this shop. And very important thing - all products must be in each shop. If I want two products and the shop has only one, it isn't correct.
My query what is wrong now:
select max(o.offer_id) as max_offer_id, o.product_id,
shop_id, prod.name
from offer as o
join product as prod on prod.product_id = o.product_id
where prod.product_id in ( 1,2 )
group by o.product_id, shop_id , prod.name
order by shop_ida
I would like to get a result like this:
max_offer_id product_id shop_id name
10 1 1 PROD1
9 2 1 PROD2
11 1 2 PROD1
12 2 2 PROD2
but I get this:
max_offer_id product_id shop_id name
10 1 1 PROD1
9 2 1 PROD2
11 1 2 PROD1
12 2 2 PROD2
1 1 3 PROD1 <-- wrong
4 1 5 PROD1 <-- wrong
6 2 6 PROD2 <-- wrong
tables and some data: https://www.db-fiddle.com/f/vyMmeUqmgYHmMirMFzwx2Y/5