SELECT
k.*,
y.exchange,
(
SELECT
close AS close_20,
close_adj AS close_adj_20
FROM stocks k
WHERE
t = (
SELECT z.t
FROM usa_stocks_daily_bar z
WHERE
t < k.t
ORDER BY z.t DESC
LIMIT 20
)
AND id = k.id
LIMIT 1
)
FROM stocks k
JOIN symbols y
ON k.id = y.id
WHERE
(
y.exchange = 'NASDAQ CM' OR
y.exchange = 'NASDAQ GM'
)
AND k.t BETWEEN 20150101 AND 20150110
ORDER BY t ASC
获取错误#1241 - 操作数应包含1列
尝试将这两个查询合并在一起,以便在20天前完成,但无法使此查询生效。
当查询向下运行时,表格存储k,获取close_adj的日期和从-20天前关闭的变化。
答案 0 :(得分:0)
正如@Gordon所提到的,你有一个子查询,它返回多个值,其中MySQL期望一个(单个)标量值。一种解决方法是使用WHERE IN
代替您拥有的内容:
WHERE t IN
(
SELECT z.t
FROM usa_stocks_daily_bar z
WHERE t < k.t
ORDER BY z.t DESC
LIMIT 20
)