gpt4 book ai didi

php - 如何按照与 OR 语句相同的顺序排列 mysql 结果?

转载 作者:行者123 更新时间:2023-11-29 04:59:15 25 4
gpt4 key购买 nike

我有一个 ID 数组,这些 ID 已按相关性降序排列,但我需要进行查询以获取每个 ID 的详细信息。

如何让返回的结果按照我的 Where 语句的顺序排列?

SELECT DISTINCT `content_object_set_instances`.`id` AS content_object_set_instance_id, `content_object_set_instances`.`title` AS content_object_set_instance_title, `content_object_set_instances`.`name` AS content_object_set_instance_name, `content_object_set_instances`.`description` AS content_object_set_instance_description FROM (`content_object_set_instances`) WHERE `content_object_set_instances`.`id` = 213 OR `content_object_set_instances`.`id` = 216 OR `content_object_set_instances`.`id` = 212

目前他们返回的是这样的:

ID  | title |  name  |  description
212 | Mr Spot | name... | description...
213 | Bamboo | name... | description...
216 | Beautious | name... | description...

最佳答案

在您的特定情况下,只有 3 个值,您可以使用 ORDER BY FIELD()。尝试以下操作:

SELECT DISTINCT ... FROM `content_object_set_instances`
WHERE ...
ORDER BY FIELD(id, 213, 216, 212)

关于php - 如何按照与 OR 语句相同的顺序排列 mysql 结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3323667/

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