我在使用zend框架mysql groupby查询时遇到了麻烦。我想要实现的是这个查询
SELECT * FROM `price_list` AS `pl`
GROUP BY
CASE WHEN `pl`.`sub_folder` IS NULL THEN
`pl`.`id`
ELSE
`pl`.`sub_folder`
END
ORDER BY `pl`.`date_added` DESC
以下是我用zend方式尝试的内容
$sql = $this->select()->setIntegrityCheck(false);
$sql->from(array('pl'=>$this->_name), array(''))
->columns($cols)
->group("case when pl.sub_folder is null then pl.id else pl.sub_folder end");
但在情况条件下输出此sql字符串的引号错误:
SELECT * FROM `price_list_pdf` AS `pl`
GROUP BY `CASE WHEN pl`.`sub_folder is null then
pl`.`id
ELSE
pl`.`sub_folder
end`
ORDER BY `pl`.`date_added` DESC
如何清除字符串中的`?还是有更好的方法吗?请帮忙..
答案 0 :(得分:2)
对于未转义的查询,请使用Expressions
之类的
$sql = $this->select()->setIntegrityCheck(false);
$sql->from(array('pl'=>$this->_name), array(''))
->columns($cols)
->group(new Zend_Db_Expr("case when pl.sub_folder is null then pl.id else pl.sub_folder end"));