我在mySQL数据库中有两个表:
价格:
Date Security Price
2017-06-06 A 3.1
2017-06-06 B 4.1
2017-06-06 C 5.1
2017-06-05 A 3
2017-06-05 B 4
2017-06-05 C 5
... more prices
工种:
Date Security Description
2017-06-06 A adesc
2017-06-06 B bdesc
2017-06-06 C cdesc
... more trades
我想创建一个视图,根据今天的日期和之前的日期,在当前Trades
表中添加2个列。例如,如果今天的日期是2017-06-06
,那么我正在寻找的输出将是:
工种:
Date Security Description Price(Today) Price(Yesterday)
2017-06-06 A adesc 3.1 3
2017-06-06 B bdesc 4.1 4
2017-06-06 C cdesc 5.1 5
... more trades
我知道我可以根据日期进行左连接以获取其中一列(即SELECT * FROM Trades LEFT JOIN Prices ON Prices.Date=DATE(NOW())
,但是是否可以扩展语法以允许添加多列,如图所示示例
非常感谢任何帮助!
答案 0 :(得分:3)
是的,试一试:
LEFT JOIN Prices AS TodaysPrices ON Prices.Date=CURDATE()
LEFT JOIN Prices AS YesterdaysPrices ON Prices.Date=SUBDATE(CURDATE(), 1)