我想帮助一个mysql查询:
max(id)
t_type = 'buy'
quotes2010
表格中获取最新(按日期)的价格(基于transactions.symbol
)transactions.high
列quotes2010.price
transactions.high
列。 。 。 。在一个查询中。
我要做的最基本的事情是更新表格,如果第二个表格中的符号价格更高。以下是数据库结构的基本版本:
CREATE TABLE IF NOT EXISTS `transactions` (
`id` int(10) unsigned NOT NULL auto_increment,
`symbol` char(8) NOT NULL,
`high` double(8,2) NOT NULL,
`t_type` enum('buy','sell') NOT NULL,
`t_date` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `position` (`position`,`id`),
KEY `t_date` (`t_date`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
INSERT INTO `transactions` VALUES(1, 'AO1', 1.35, '2010-12-12 00:08:57');
CREATE TABLE IF NOT EXISTS `quotes2010` (
`symbol` char(8) NOT NULL,
`price` double(8,2) NOT NULL,
`date` datetime NOT NULL,
PRIMARY KEY (`symbol`,`date`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `quotes2010` VALUES('A01', 1.40, '2010-12-19 10:03:05');
答案 0 :(得分:0)
您的架构和数据集有限,但我认为这可以满足您的需求。
UPDATE
quotes2010 INNER JOIN
(
SELECT symbol, MAX(date) AS max_date
FROM quotes2010
GROUP BY symbol
) quote_by_maxdate ON quote_by_maxdate.symbol=quotes2010.symbol AND quote_by_maxdate.max_date=quotes2010.date
INNER JOIN transactions ON quotes2010.symbol = transactions.symbol
INNER JOIN
(
SELECT symbol, MAX(id) AS max_id
FROM transactions
WHERE t_type='buy'
GROUP BY symbol
) transactions_by_max_id ON transactions.id=transactions_by_max_id.max_id
SET quotes2010.price = IF(quotes2010.price > transactions.high,quotes2010.price-0.01,quotes2010.price),
transactions.high = IF(quotes2010.price > transactions.high,quotes2010.price,transactions.high)