joecrocker
08-22-2009, 05:09 AM
I'm a newbie and have been struggling on this MYSQL query for days...
I would like to display the min price for each store_id (group) and it's associated fields. This query works fine when item_id = 70 but not for any other item_ids. Only one row of data (one store) is displayed instead of two stores.
Any help would be greatly appreciated!!! Thanks!!!!
GOOD QUERY when item_id = 70:
SELECT min1.* FROM toy_items AS min1 LEFT OUTER JOIN toy_items AS min2 ON min2.store_id = min1.store_id AND min2.price < min1.price WHERE min2.price IS null AND min1.item_id = 70 ORDER BY min1.price
CORRECT RESUTLS:
id item_id store_id description price quantity
363 70 14 Toys for tots 1 0.55 21
366 70 15 Toys for tots 41 0.75 52
WRONG QUERY when item_id = 78:
SELECT min1.* FROM toy_items AS min1 LEFT OUTER JOIN toy_items AS min2 ON min2.store_id = min1.store_id AND min2.price < min1.price WHERE min2.price IS null AND min1.item_id = 78 ORDER BY min1.price
WRONG RESUTLS (store 14 result should also be displayed):
id item_id store_id description price quantity
368 78 15 Toys for tots 44 0.75 26
Here's the table and data:
CREATE TABLE IF NOT EXISTS `toy_items` (
`id` int(4) unsigned NOT NULL AUTO_INCREMENT,
`item_id` int(4) unsigned NOT NULL DEFAULT '0',
`store_id` int(4) unsigned NOT NULL DEFAULT '0',
`description` VARCHAR(255) NOT NULL DEFAULT '',
`price` DECIMAL(15,2),
`quantity` int(4) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
INDEX `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO toy_items (id, item_id, store_id, price, description, quantity) VALUES
('363','70', '14', '0.550', 'Toys for tots 1', '21'),
('364','84', '14', '1.440', 'Toys for tots 11', '22'),
('365','78', '14', '0.690', 'Toys for tots 31', '23'),
('366','70', '15', '0.750', 'Toys for tots 41', '52'),
('367','84', '15', '0.750', 'Toys for tots 16', '25'),
('368','78', '15', '0.750', 'Toys for tots 44', '26'),
('369','70', '15', '0.950', 'Toys for tots 3', '28'),
('370','84', '15', '0.950', 'Toys for tots 11', '1'),
('371','78', '15', '0.950', 'Toys for tots 98', '4');
I would like to display the min price for each store_id (group) and it's associated fields. This query works fine when item_id = 70 but not for any other item_ids. Only one row of data (one store) is displayed instead of two stores.
Any help would be greatly appreciated!!! Thanks!!!!
GOOD QUERY when item_id = 70:
SELECT min1.* FROM toy_items AS min1 LEFT OUTER JOIN toy_items AS min2 ON min2.store_id = min1.store_id AND min2.price < min1.price WHERE min2.price IS null AND min1.item_id = 70 ORDER BY min1.price
CORRECT RESUTLS:
id item_id store_id description price quantity
363 70 14 Toys for tots 1 0.55 21
366 70 15 Toys for tots 41 0.75 52
WRONG QUERY when item_id = 78:
SELECT min1.* FROM toy_items AS min1 LEFT OUTER JOIN toy_items AS min2 ON min2.store_id = min1.store_id AND min2.price < min1.price WHERE min2.price IS null AND min1.item_id = 78 ORDER BY min1.price
WRONG RESUTLS (store 14 result should also be displayed):
id item_id store_id description price quantity
368 78 15 Toys for tots 44 0.75 26
Here's the table and data:
CREATE TABLE IF NOT EXISTS `toy_items` (
`id` int(4) unsigned NOT NULL AUTO_INCREMENT,
`item_id` int(4) unsigned NOT NULL DEFAULT '0',
`store_id` int(4) unsigned NOT NULL DEFAULT '0',
`description` VARCHAR(255) NOT NULL DEFAULT '',
`price` DECIMAL(15,2),
`quantity` int(4) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
INDEX `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO toy_items (id, item_id, store_id, price, description, quantity) VALUES
('363','70', '14', '0.550', 'Toys for tots 1', '21'),
('364','84', '14', '1.440', 'Toys for tots 11', '22'),
('365','78', '14', '0.690', 'Toys for tots 31', '23'),
('366','70', '15', '0.750', 'Toys for tots 41', '52'),
('367','84', '15', '0.750', 'Toys for tots 16', '25'),
('368','78', '15', '0.750', 'Toys for tots 44', '26'),
('369','70', '15', '0.950', 'Toys for tots 3', '28'),
('370','84', '15', '0.950', 'Toys for tots 11', '1'),
('371','78', '15', '0.950', 'Toys for tots 98', '4');