Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按照ID在给定列表中的顺序排列数据_Php_Mysql_Sql Server_Wordpress - Fatal编程技术网

Php 按照ID在给定列表中的顺序排列数据

Php 按照ID在给定列表中的顺序排列数据,php,mysql,sql-server,wordpress,Php,Mysql,Sql Server,Wordpress,我在经营wordpress 因此,有一个逗号分隔的post id字符串,并且需要按照字符串中列出的顺序对找到的post进行排序 没有sql查询,我只需在参数中添加'orderby'=>'post\u in',就可以实现这一点 但是我需要通过sql查询来完成。以下是我的SQL查询: $idList = '2,50,10,25,150,1200,356'; $sql = "SELECT * FROM wp_posts WHERE ID IN ($idList)"; 上面的查询返回结果的顺序越来越大

我在经营wordpress

因此,有一个逗号分隔的post id字符串,并且需要按照字符串中列出的顺序对找到的post进行排序

没有sql查询,我只需在参数中添加
'orderby'=>'post\u in'
,就可以实现这一点

但是我需要通过sql查询来完成。以下是我的SQL查询:

$idList = '2,50,10,25,150,1200,356';
$sql = "SELECT * FROM wp_posts WHERE ID IN ($idList)";

上面的查询返回结果的顺序越来越大,如下所示:
$idList='2,10,25,501503561200'但我需要它们在我的字符串中的顺序。没有找到如何在sql查询中执行相同的操作。有什么想法吗?

在mySQL中,可以使用

编辑:

看看这个。我引用的是

然而,请注意

1-这会降低SQL的可移植性,因为其他DBMS可能没有这样的可移植性 功能。 2-当您的语言列表(或排序依据的其他值) 更长的时间,最好有一个单独的表与sortorder 列,并将其连接到您的查询以进行排序


您可以使用
…ORDER BY FIND_IN_SET(ID,$idList)

查询:

SELECT * 
 FROM wp_posts 
WHERE ID IN ($idList)
ORDER BY FIND_IN_SET(ID, $idList)
FIND_IN_SET (search string, string list)
注意:

SELECT * 
 FROM wp_posts 
WHERE ID IN ($idList)
ORDER BY FIND_IN_SET(ID, $idList)
FIND_IN_SET (search string, string list)
在集合()中查找函数

当字符串列表中不存在搜索字符串时,此函数返回0,如果任一参数为NULL,则返回NULL

语法:

SELECT * 
 FROM wp_posts 
WHERE ID IN ($idList)
ORDER BY FIND_IN_SET(ID, $idList)
FIND_IN_SET (search string, string list)

MySQL服务器还是sql服务器?MySQL具有字段功能。您可以尝试
从wp_帖子中选择*,其中ID按字段(ID,2,50,10,251501200356)排序(ID,2,50,10,251501200356)