我试图将非常复杂的MySQL查询转换为DQL。当我尝试使用或比较名称 total_days 的别名列时,主要问题就出现了,这里是示例 DQL查询:
SELECT (1+1) AS total_days,
(CASE WHEN (total_days = 1) THEN 1 ELSE 0 END) AS test
FROM AppBundle:PeriodPrices
查询更复杂,但我这里只是为了测试目的而保持简单!这是一个例外:
An exception occurred while executing '
SELECT (1 + 1) AS sclr0,
(CASE WHEN (sclr0 = 1) THEN 1 ELSE 0 END) AS sclr1
FROM PeriodPrices p0_':
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sclr0' in 'field list'
这是Symfony框架转换后的DQL到SQL查询,因为我可以看到别名字段sclr0在CASE语句中不可见。
注意,如果我使用IF STATEMENT,则查询正在运行:
SELECT
(1+1) AS total_days,
IF( (SELECT total_days ) = 2, 1, 0) AS test
结果:
total_days test
2 1