我有这个问题:
SELECT
s.last_spread, s.sd, s.mean, s.id
,c.id_ticker, c.coef
,t.ticker
,/*p.last,*/ p.price
FROM (SELECT * FROM spreads WHERE spreads.id_check=1 LIMIT 60,896) as s
INNER JOIN coef as c
ON c.id_spread = s.id
INNER JOIN tickers AS t
ON t.id = c.id_ticker
LEFT JOIN (SELECT prices.id_ticker, MAX(prices.date) as last, prices.price FROM prices GROUP BY prices.id_ticker) AS p
ON p.id_ticker = t.id
ORDER BY s.id, c.id
最后一次JOIN没有返回正确的值....我需要获得代码的最后一个价格(所以每个代码的最后一行)。
目前它返回第一个价格,如何更改整个查询以获取每个ticker.id的最后价格? (p.id_ticker = t.id
)
谢谢!
答案 0 :(得分:3)
通过此子查询添加订单:
SELECT prices.id_ticker, MAX(prices.date) as last, prices.price
FROM prices GROUP BY prices.id_ticker
order by prices.id_ticker desc
答案 1 :(得分:2)
使用A having子句会很优雅:
LEFT JOIN (SELECT prices.id_ticker, prices.date as last, prices.price FROM prices GROUP BY prices.id_ticker HAVING prices.date = MAX(prices.date) )
编辑但 NOT 工作......(请参阅下面的评论)
答案 2 :(得分:1)
我会说使用ORDER BY
和LIMIT
只能获得一个价格(最后价格)。