如何将此查询重写为 Doctrine DBAL 查询或更好的 createQuery() 结构?当我使用 createQuery() 时,“data_last”未被识别为日期,而是被识别为字符串并生成错误。也许更好的解决方案是在 symfony 端进行内部左连接和计算?Doctrine 无法识别 ROUND 函数。
$sql='SELECT
q.fund_id AS tfi,
f.fundName AS name,
( SELECT `date`
FROM quotes
WHERE DATE_SUB( "'. $sToday .'", INTERVAL 1 MONTH ) <= `date` AND `fund_id` = `tfi`
ORDER BY `date` DESC
LIMIT 1) AS date_last,
( SELECT `value`
FROM quotes
WHERE `fund_id` = `tfi` AND `date` = `date_last`
LIMIT 1) AS value_last,
( SELECT `date`
FROM quotes
WHERE DATE_SUB( date_last, INTERVAL 1 MONTH ) <= `date` AND `fund_id` = `tfi`
ORDER BY `date` ASC
LIMIT 1) AS date_1m,
( SELECT `value`
FROM quotes
WHERE `fund_id` = `tfi` AND `date` = `date_1m`
LIMIT 1) AS value_1m,
( SELECT ROUND( (value_last-value_1m)/value_1m*100, 2) ) AS chg_1m
FROM quotes q
LEFT JOIN funds f
ON ( q.fund_id = f.id )
GROUP BY q.fund_id
ORDER BY f.fundName';