我有以下3个表格:
CREATE TABLE IF NOT EXISTS `item` (
`id` int(11) NOT NULL auto_increment,
`subcategory` mediumint(9) NOT NULL,
`name` varchar(64) NOT NULL,
`pagetitle` varchar(128) NOT NULL,
`metadescription` varchar(256) NOT NULL,
`properties` varchar(256) NOT NULL,
`specification` text NOT NULL,
`summary` text NOT NULL,
`homepage` varchar(256) NOT NULL default 'none',
PRIMARY KEY (`id`)
);
CREATE TABLE IF NOT EXISTS `link` (
`id` mediumint(9) NOT NULL auto_increment,
`item` mediumint(9) NOT NULL,
`merchant` mediumint(9) NOT NULL,
`rawlink` varchar(256) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE IF NOT EXISTS `prices` (
`id` mediumint(9) NOT NULL auto_increment,
`link` mediumint(9) NOT NULL,
`timestamp` date NOT NULL,
`price` float NOT NULL,
PRIMARY KEY (`id`)
);
这些表格包含价格比较网站的数据。
每个项目都有多个链接(指向不同的商店) link.item = item.id 。
每个链接都有多个价格(每天跟踪一个) prices.link = link.id 。
问题
我想创建一个返回的MySQL查询:
到目前为止,我已经想出了这个:
SELECT item.name AS iname, item.id AS iid,
((SELECT MIN(price) FROM prices, link WHERE prices.timestamp = CURDATE() AND link.id = prices.link AND link.item = iid) -
(SELECT MIN(price) FROM prices, link WHERE prices.timestamp = CURDATE() - INTERVAL 1 DAY AND link.id = prices.link AND link.item = iid))
AS changeprice
FROM prices, link, item
WHERE link.id = prices.link AND link.item = item.id
GROUP BY link.item
ORDER BY changeprice DESC
LIMIT 0,3
技术上有效,但效率低下(服务器超时)。
我知道它有效,因为我通过添加额外的WHERE子句来限制项目以提高速度:
item.id BETWEEN '20' AND '30'
我目前有55个项目,这证明太多了,所以我需要一个更快/更简单的解决方案。
我找到了this alternative solution,但它似乎无法正常工作(我收到错误)。
提前致谢!
示例数据
INSERT INTO `item` (`id`, `subcategory`, `name`, `pagetitle`, `metadescription`, `properties`, `specification`, `summary`, `homepage`) VALUES
(1, 5, 'Item 1', 'TEST', 'TEST', '0', 'TEST', 'TEST', 'TEST'),
(2, 5, 'Item 2', 'TEST', 'TEST', '0', 'TEST', 'TEST', 'TEST'),
(3, 5, 'Item 3', 'TEST', 'TEST', '0', 'TEST', 'TEST', 'TEST');
INSERT INTO `link` (`id`, `item`, `merchant`, `rawlink`) VALUES
(1, 1, 1, 'TEST'),
(2, 1, 2, 'TEST'),
(3, 1, 3, 'TEST'),
(4, 2, 1, 'TEST'),
(5, 2, 2, 'TEST'),
(6, 2, 3, 'TEST'),
(7, 3, 1, 'TEST'),
(8, 3, 2, 'TEST'),
(9, 3, 3, 'TEST');
INSERT INTO `prices` (`id`, `link`, `timestamp`, `price`) VALUES
(1, 1, '2013-02-07', 110.00),
(2, 1, '2013-02-06', 75.00),
(3, 2, '2013-02-07', 105.00),
(4, 2, '2013-02-06', 100.00),
(5, 3, '2013-02-07', 105.00),
(6, 3, '2013-02-06', 100.00),
(7, 4, '2013-02-07', 50.00),
(8, 4, '2013-02-06', 50.00),
(9, 5, '2013-02-07', 50.00),
(10, 5, '2013-02-06', 50.00),
(11, 6, '2013-02-07', 50.00),
(12, 6, '2013-02-06', 50.00),
(13, 7, '2013-02-07', 10.00),
(14, 7, '2013-02-06', 4.00),
(15, 8, '2013-02-07', 8.00),
(16, 8, '2013-02-06', 6.00),
(17, 9, '2013-02-07', 10.00),
(18, 9, '2013-02-06', 10.00);
预期输出
Item 1 - 30
Item 3 - 4
Item 2 - 0
澄清:第1项= 30因为:
(今天第1项最低价) - (昨天第1项最低价)
=(链接2& 3今天是105.00) - (链接1昨天是75.00)
=(105.00) - (75.00)= 30
(链接1,链接2和链接3都属于第1项。我今天需要最低价格(在这3个链接中) - 昨天的最低价格(在这3个链接中))。
网站示例
我比较iPad的价格。 (item1 = iPad)。
我链接到3家销售iPad的商店。 (link1 =亚马逊,link2 = eBay,link3 = Argos)。
每个链接都有价格历史记录。 (亚马逊今天是110.00,但昨天是75.00。今天eBay是105.00,但昨天是100.00.Argos今天是105.00,但昨天是100.00。)
因此,最低价格如下:
今天= eBay和Arogs(105.00) 昨天=亚马逊(75.00)
iPad的价格涨幅为30.00。
答案 0 :(得分:0)
试试这个
select item.name AS iname, item.id AS iid ,changeprice from(
select t1.link, min(pricem) - min(pricemm) as changeprice from (
select id , link, price pricemm from prices
where prices.timestamp = CURDATE() - INTERVAL 1 DAY
group by price)t1
inner join(
select id , link, price pricem from prices
where prices.timestamp = CURDATE()
group by price )t2
on t1.link = t2.link
)t3
INNER JOIN link
ON t3.link = link.id
inner join item
ON link.item = item.id
group by link.item
ORDER BY changeprice DESC
LIMIT 3 ;
编辑:
你的创建表单是错误的,不要在列和表名称周围使用单引号,使用反引号 - > '''
或使用那些:
CREATE TABLE IF NOT EXISTS `item` (
id int(11) NOT NULL auto_increment,
subcategory mediumint(9) NOT NULL,
name varchar(64) NOT NULL,
pagetitle varchar(128) NOT NULL,
metadescription varchar(256) NOT NULL,
properties varchar(256) NOT NULL,
specification text NOT NULL,
summary text NOT NULL,
homepage varchar(256) NOT NULL default 'none',
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS link (
id mediumint(9) NOT NULL auto_increment,
item mediumint(9) NOT NULL,
merchant mediumint(9) NOT NULL,
rawlink varchar(256) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS prices (
id mediumint(9) NOT NULL auto_increment,
link mediumint(9) NOT NULL,
timestamp date NOT NULL,
price float NOT NULL,
PRIMARY KEY (id)
);
修改强>
有3个条目,第一个不是0,它是-36,04
尝试用
替换最后一组 group by link.item
通过
group by t3.link
DEMO here 请注意,有3种不同的价格。链接。
<强> EDIT2 强> 它的长期查询,但它提供了你想要的东西。
select iname , iid , min(changeprice) changeprice from (
select t1.iname , t1.iid , pricem - pricemm as changeprice from
(select item.name AS iname, item.id AS iid ,min(price) pricem from prices
INNER JOIN link
ON prices.link = link.id
inner join item
ON link.item = item.id
where prices.timestamp = CURDATE()
group by item.name
ORDER BY price DESC
)t1,
( select item.name AS iname, item.id AS iid ,min(price) pricemm from prices
INNER JOIN link
ON prices.link = link.id
inner join item
ON link.item = item.id
where prices.timestamp = CURDATE() - INTERVAL 1 DAY
group by item.name
ORDER BY price DESC
)t2
having changeprice >=0
ORDER BY changeprice DESC
)h
group by iname
order by changeprice desc LIMIT 3