I'm having problem with GROUP BY query in a specific table which has two column primary key.
My table looks like like this:
CREATE TABLE IF NOT EXISTS `stocks` (
`id_city` int(10) NOT NULL,
`id_prod` int(10) NOT NULL,
`sell_price` int(10) DEFAULT '0',
PRIMARY KEY (`id_city`,`id_prod`)
)
Let's say it has such values inserted:
INSERT INTO `stocks` (`id_city`, `id_prod`, `sell_price`)
VALUES ('1', '1', '100'), ('2', '1', '90'), ('3', '1', '10');
After such query:
SELECT id_prod, id_city, MIN(sell_price)
FROM stocks
GROUP BY id_prod
the result row looses 'id_city' key - it takes the first occurance of city_id,
id_prod id_city MIN(sell_price)
1 1 10
How to build a proper query for this case? The result should look like this:
id_prod id_city MIN(sell_price)
1 3 10