我有3张桌子: 'art'包含产品列表('art_id','art_nom'代表名称) 'mag'是商店列表('mag_id') 'pri'包含特定商店的产品价格。一个产品有几个价格,甚至一个商店的几个价格,取决于日期('pri_pri'是价格,'pri_dat'是价格注册的日期,并且是日期时间格式)。
我希望产品列表中包含3个最近的价格(对于所选的3家商店)。我得到了这个要求:
SELECT art.art_id, art_nom,
pri0.pri_pri AS pri_pri0,
pri1.pri_pri AS pri_pri1,
pri2.pri_pri AS pri_pri2
FROM (((art
LEFT JOIN (SELECT pri_pri, art_id, mag_id AS nbr_pri0 FROM pri WHERE mag_id = '7081' GROUP BY art_id) AS pri0 ON art.art_id = pri0.art_id)
LEFT JOIN (SELECT pri_pri, art_id, mag_id AS nbr_pri1 FROM pri WHERE mag_id = '14432' GROUP BY art_id) AS pri1 ON art.art_id = pri1.art_id)
LEFT JOIN (SELECT pri_pri, art_id, mag_id AS nbr_pri2 FROM pri WHERE mag_id = '14515' GROUP BY art_id) AS pri2 ON art.art_id = pri2.art_id)
我收到了我想要的清单,但我没有最近的价格。 因此,如果有人可以帮助我添加一个获得最新价格的条件,那将非常感激。
我真的不确定这种方式是否非常有效。所以,如果您对简化它有一些建议......
谢谢!
注意:我简化了我的请求,因此更容易阅读。如果您想要完整的请求,5个价格和价格数量的'ORDER BY',这里是:
SELECT art.art_id, cat_id1, cat_id2, cat_id3, art_ean, art_nom, art_unt, art_qtt,
mrq_nom, mrq_img,
(IFNULL(nbr_pri0, 0) + IFNULL(nbr_pri1, 0) + IFNULL(nbr_pri2, 0) + IFNULL(nbr_pri3, 0) + IFNULL(nbr_pri4, 0)) AS nbr_pri,
nbr_pri0, pri0.pri_pri AS pri_pri0,
nbr_pri1, pri1.pri_pri AS pri_pri1,
nbr_pri2, pri2.pri_pri AS pri_pri2,
nbr_pri3, pri3.pri_pri AS pri_pri3,
nbr_pri4, pri4.pri_pri AS pri_pri4
FROM mrq, (((((art
LEFT JOIN (SELECT pri_pri, art_id, mag_id, COUNT(pri_id) AS nbr_pri0 FROM pri WHERE mag_id = '7081' GROUP BY art_id) AS pri0 ON art.art_id = pri0.art_id)
LEFT JOIN (SELECT pri_pri, art_id, mag_id, COUNT(pri_id) AS nbr_pri1 FROM pri WHERE mag_id = '14432' GROUP BY art_id) AS pri1 ON art.art_id = pri1.art_id)
LEFT JOIN (SELECT pri_pri, art_id, mag_id, COUNT(pri_id) AS nbr_pri2 FROM pri WHERE mag_id = '14515' GROUP BY art_id) AS pri2 ON art.art_id = pri2.art_id)
LEFT JOIN (SELECT pri_pri, art_id, mag_id, COUNT(pri_id) AS nbr_pri3 FROM pri WHERE mag_id = '12458' GROUP BY art_id) AS pri3 ON art.art_id = pri3.art_id)
LEFT JOIN (SELECT pri_pri, art_id, mag_id, COUNT(pri_id) AS nbr_pri4 FROM pri WHERE mag_id = '8136' GROUP BY art_id) AS pri4 ON art.art_id = pri4.art_id)
WHERE mrq.mrq_id = art.mrq_id
ORDER BY nbr_pri DESC
LIMIT 0,50
编辑:这是Nitu和Strawberry提出的表格的脚本(我希望如此,我不确定“DDL”和“sqlfiddle”是什么意思):
CREATE TABLE IF NOT EXISTS `art` (
`art_id` int(11) NOT NULL AUTO_INCREMENT,
`art_nom` char(40) NOT NULL,
PRIMARY KEY (`art_id`)
);
INSERT INTO `art` (`art_id`, `art_nom`) VALUES
(1, 'Coca-Cola classic'),
(2, 'Coca-Cola vanille'),
(3, 'Coca-Cola Cherry'),
(4, 'Coca-Cola Light lemon');
CREATE TABLE IF NOT EXISTS `mag` (
`mag_id` int(6) NOT NULL AUTO_INCREMENT,
`mag_vil` char(100) NOT NULL,
`mag_nom` char(100) NOT NULL,
PRIMARY KEY (`mag_id`)
);
INSERT INTO `mag` (`mag_id`, `mag_vil`, `mag_nom`) VALUES
(2, '01100 Oyonnax', 'Petit Casino Oyonnax'),
(3, '75001 Paris', 'Petit Casino Paris'),
(4, '69001 Lyon', 'Petit Casino Lyon');
CREATE TABLE IF NOT EXISTS `pri` (
`pri_id` int(11) NOT NULL AUTO_INCREMENT,
`art_id` int(11) NOT NULL,
`mag_id` int(6) NOT NULL,
`pri_pri` float NOT NULL,
`pri_dat` datetime NOT NULL,
PRIMARY KEY (`pri_id`)
);
INSERT INTO `pri` (`pri_id`, `art_id`, `mag_id`, `pri_pri`, `pri_dat`) VALUES
(1, 1, 14515, 2.61, '2014-08-12 17:28:48'),
(2, 1, 12458, 1.74, '2014-04-01 17:52:00'),
(3, 1, 12458, 2.39, '2014-03-15 17:52:00'),
(4, 3, 12458, 1.93, '2014-07-31 20:00:00'),
(5, 3, 8136, 1.21, '2014-08-08 14:02:00');
编辑编辑:对于这个数据,我想收到产品art_id = 1的价格1.74(pri_id = 2),因为日期比价格2.39(pri_id = 3)更新
答案 0 :(得分:0)
您可以将子查询更改为
SELECT pri_date, MAX(pri_date) AS maxdate, pri_pri, art_id, mag_id, COUNT(pri_id) AS nbr_pri0
FROM pri
WHERE mag_id = '7081'
GROUP BY art_id
HAVING maxdate=pri_date
这将为您提供最近的约会。