SET @qdate := '2014-02-17';
SELECT
com.trading_code,
com.business_segment_id,
dmkt.close_price,
dmkt.cdate,
dmkt.turnover,
dmkt.volume,
sh.total_no_share,
IF(dmkt.close_price IS NULL,
((SELECT
close_price
FROM
daily_mkt_status
WHERE
trading_code = com.trading_code
AND cdate <= @qdate
ORDER BY cdate DESC
LIMIT 1) * sh.total_no_share),
(dmkt.close_price * sh.total_no_share)) AS mcap
FROM
company AS com
LEFT JOIN
daily_mkt_status AS dmkt ON (com.trading_code = dmkt.trading_code
AND dmkt.cdate = @qdate )
LEFT JOIN
share_info AS sh ON (com.trading_code = sh.trading_code)
此代码仅在我使用像
这样的变量时才有效SET @qdate :- '2014-02-17'
但我希望它是来自另一个表格的日期数组
SET @qdate IN (SELECT cdate FROM market_index);
qdate将在表market_index中循环显示所有日期。我已经完成了所有的数组变量问题,但由于我的代码的这一部分而没有为我工作
cdate <= @qdate
但我需要这部分在那里或我的查询不会给我我想要的输出。
希望我很清楚,并提前多多谢谢。答案 0 :(得分:0)
为什么不简单地加入market_index
表?
SELECT
com.trading_code,
com.business_segment_id,
dmkt.close_price,
dmkt.cdate,
dmkt.turnover,
dmkt.volume,
sh.total_no_share,
mi.cdate
IF(dmkt.close_price IS NULL,
((SELECT
close_price
FROM
daily_mkt_status
WHERE
trading_code = com.trading_code
AND cdate <= mi.cdate
ORDER BY cdate DESC
LIMIT 1) * sh.total_no_share),
(dmkt.close_price * sh.total_no_share)) AS mcap
FROM
company AS com
JOIN market_index AS mi
LEFT JOIN daily_mkt_status AS dmkt ON (
com.trading_code = dmkt.trading_code
AND dmkt.cdate = mi.cdate
)
LEFT JOIN share_info AS sh ON (com.trading_code = sh.trading_code)
您可能还想添加GROUP BY
子句,以便摆脱不必要的重复