简体   繁体   English

使用Zend Framework 1.6.2添加二阶字段时发生歧义错误

[英]Ambiguous error occured when I add a second order field using Zend Framework 1.6.2

When I echo the sql using echo $select->__toString() : 当我使用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

What I order by as the second field is the TYPE field which is calculated in the if clause. 作为第二个字段,我要排序的是TYPE字段,它是在if子句中计算得出的。 I added this because without using the second order field, there will be sorting problem, because the field a.TYPE is ENUM , so I have to fix the sort using a second order field. 我添加此内容是因为不使用二阶字段,将存在排序问题,因为字段a.TYPEENUM ,所以我必须使用二阶字段来修复排序。

But now I added the second field, I runs successfully and nicely when I use sql client like sqlyog, but when I use return $this->_myAdapter->fetchAll($select); 但是现在我添加了第二个字段,当我使用像sqlyog之类的sql客户端时,我运行成功并且很好,但是当我使用return $this->_myAdapter->fetchAll($select); to execute the sql, there come a TYPE is ambiguous error. 要执行sql,会出现TYPE is ambiguous错误。 I think the problem lies in the Zend Framework fetchAll() method, but how can I fix it? 我认为问题出在Zend Framework的fetchAll()方法中,但是我该如何解决呢?

You can't use TYPE as an ORDER BY Parameter because it is an only an ALIAS of your IF statement. 您不能将TYPE用作ORDER BY参数,因为它只是IF语句的ALIAS You can make TYPE as an ORDER BY Parameter if it is in a sub-query. 如果在子查询中,则可以将TYPEORDER BY参数。 See my example below: 请参阅下面的示例:

 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM