当我使用 echo $select->__toString()
回显 sql 时:
SELECT `a`.`ChannelID`,
`a`.`CategoryID`,
`a`.`Country`,
`a`.`LocalName`,
if((`a`.`Type` = 'FreeText'
AND a.IsHybrid = 'YES'), 'Hybrid', if(`a`.`Type` = 'FreeText'
AND a.IsHybrid = 'NO', 'FreeText', a.Type)) AS TYPE,
`a`.`IsActive`,
`a`.`LastChangeDate`
FROM `ProCatCountry` AS `a`
INNER JOIN `ProCat` AS `b` ON a.CategoryID = b.CategoryID
ORDER BY FIELD(a.TYPE, 'Normal', 'Special', 'Structured'),TYPE ASC
LIMIT 25
我作为第二个字段排序的是在 if
子句中计算的 TYPE
字段。我添加这个是因为不使用二阶字段,会出现排序问题,因为字段a.TYPE
是ENUM
,所以我必须使用第二个来修复排序订单字段。
但是现在我添加了第二个字段,当我使用像 sqlyog 这样的 sql 客户端时,我运行成功并且很好,但是当我使用 return $this->_myAdapter->fetchAll($select);
到执行sql,出现TYPE is ambiguous
错误。我认为问题出在 Zend Framework fetchAll()
方法上,但我该如何解决呢?
最佳答案
您不能使用 TYPE
作为ORDER BY
参数,因为它只是一个 ALIAS
你的IF
陈述。你可以制作TYPE
作为ORDER BY
参数,如果它在子查询中。请参阅下面的示例:
SELECT * FROM(
SELECT `a`.`ChannelID`,
`a`.`CategoryID`,
`a`.`Country`,
`a`.`LocalName`,
if((`a`.`Type` = 'FreeText'
AND a.IsHybrid = 'YES'), 'Hybrid', if(`a`.`Type` = 'FreeText'
AND a.IsHybrid = 'NO', 'FreeText', a.Type)) AS TYPE,
`a`.`IsActive`,
`a`.`LastChangeDate`
FROM `ProCatCountry` AS `a`
INNER JOIN `ProCat` AS `b` ON a.CategoryID = b.CategoryID
ORDER BY FIELD(a.TYPE, 'Normal', 'Special', 'Structured') ASC
LIMIT 25) AS A ORDER BY TYPE ASC
关于php - 使用 Zend Framework 1.6.2 添加二阶字段时出现不明确错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28423089/