我的代码是:
$query = "SELECT * FROM `chat`
WHERE fromthe = '$email' and tothe='theadmin' order by id desc;";
$query .= "SELECT * FROM `chat`
WHERE fromthe = 'theadmin' and tothe='$email' order by id desc";
mysqli_multi_query($con, $query);
使用此查询时,我得到了结果,但我的概念不起作用。
这个概念是:
这是在 php 服务器上的聊天应用程序中使用的,我使用此查询来获取客户端和管理员之间的消息。我必须按降序排列此结果的 id
。
我看到的结果是:
它使用其 id
订购管理员消息,并使用其 id
订购客户端消息。我希望将 id
排序为两个结果的降序排列。
最佳答案
您可能需要一个基于 UNION ALL 的单个查询
$query = "SELECT * FROM `chat`
WHERE fromthe = '$email' and tothe='theadmin'
UNION ALL
SELECT * FROM `chat`
WHERE fromthe = 'theadmin' and tothe='$email' order by id desc";
无论如何,您应该避免在 sql 中使用 php var(您面临 SQLinjection 的风险)..您应该查看准备好的语句和绑定(bind)参数
关于php - 当对两列使用 mysqli_multi_query 时,排序无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49590588/