gpt4 book ai didi

mysql - 如何以手动指定的顺序检索项目

转载 作者:行者123 更新时间:2023-12-01 00:25:11 25 4
gpt4 key购买 nike

我在 PHP 中有一个 id 列表。例如:

$id = (4,6,3,2,5,1)

当我执行这样的查询时:

$query = mysql_query("SELECT * FROM table WHERE ID IN ($id)");

它检索到我想要的结果,但项目按插入顺序返回。

相反,我想按照数组中给定的顺序检索结果:

information 4, information 6, information 3, information 2, information 5, information 1 

我该怎么做?

谢谢大家!解决方案是 ORDER BY FIELD (ID, $id);

最佳答案

试试这个

$query = "SELECT * FROM table WHERE ID IN ($id) ORDER BY FIELD(ID,$id) ";

你可以看到这个http://marco-pivetta.com/mysql-custom-sorting-rule-mysql/

关于mysql - 如何以手动指定的顺序检索项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15589165/

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