gpt4 book ai didi

mysql - 使用 zend 框架编写 mysql 查询

转载 作者:可可西里 更新时间:2023-11-01 08:21:35 26 4
gpt4 key购买 nike

我有这个查询工作正常

SELECT t.username
FROM users t LEFT JOIN friends y ON t.id=y.user_id2 and y.user_id1=2
WHERE LOWER(t.username) LIKE 'ha%'
ORDER BY
CASE WHEN y.user_id2 IS NULL THEN 1
ELSE 0
END
,t.username;

我正在尝试用 zend 框架编写它,这就是我想出的

        $users = new Users;
$select = $users->select();
$select->setIntegrityCheck(false);
$select->from(array('t1' => 'users'), array('username'));
$select->joinLeft(array('t2' => 'friends'), 't1.id=t2.user_id2 and t2.user_id1 =2');
$select->where("LOWER(t1.username) like '$input%'");
$select->order("t1.username, CASE WHEN t2.user_id2 IS NULL THEN 1 ELSE 0 END ");
$listofusernames = $users->fetchAll($select);

但是它似乎不起作用,我得到了这个错误

Fatal error: Uncaught exception 'Zend_Db_Statement_Mysqli_Exception' with message 'Mysqli prepare error: Unknown column 't1.username, CASE WHEN t2.user_id2 IS NULL THEN 1 ELSE 0 END ' in 'order clause'' in /opt/lampp/htdocs/vote_old/library/Zend/Db/Statement/Mysqli.php:77 Stack trace: #0

显然它与嵌入在 order by 子句中的大小写有关。

您知道如何修复该代码吗?

谢谢

最佳答案

尝试将列放入数组 like

$select->order(array('t1.username',
new Zend_Db_Expr ('CASE WHEN t2.user_id2 IS NULL THEN 1 ELSE 0 END')));

关于mysql - 使用 zend 框架编写 mysql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6812220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com