将SQL转换为Doctrine DBAL或createQuery()

时间:2012-03-24 08:14:13

标签: sql symfony doctrine-orm dbal createquery

如何将此查询重写为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';

1 个答案:

答案 0 :(得分:0)

哇,我不会浪费任何时间来重写查询这种复杂性。使用Doctrine Native SQL

,你肯定会更好