dsfg3241 2011-08-19 06:44
浏览 76
已采纳

Zend DB中带有'like'子句的Order子句

Query:

select * from table_name ORDER BY name like 'C%' DESC;

This query work fine in MySql, but i am unable build the query using Zend DB. I am getting error while executing.

PHP DB code:

$result = $this->getDefaultAdapter() ->select() ->from($this->_name,array('*')) ->order("name like 'C%' DESC") ->query() ->fetchAll();

Error:

Column not found: 1054 Unknown column 'name like 'C%'' in 'order clause' 

Thanks in advance

  • 写回答

2条回答 默认 最新

  • douhu7807 2011-08-19 06:50
    关注

    Zend_Db_Select tries to delimit strings as column names, but it skips doing that if you pass an object of type Zend_Db_Expr instead of a string:

    ->order(new Zend_Db_Expr("name like 'C%' DESC"))->
    

    There's also an undocumented shortcut: the column-delimiting function assumes that any string containing parentheses is very likely to be an expression instead of just a column name. So the following would work too:

    ->order( "(name like 'C%' DESC)" )->
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来