如何按优先级ID列表对mysql结果排序? [英] How can i sort a mysql result by a list of prioritised ids?

查看:406
本文介绍了如何按优先级ID列表对mysql结果排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个庞大的产品数据库,每个星期天,我的脚本都会激活50个新产品. 问题是,根据我脚本中的某些规则,我发现我应该优先激活一些产品. (以您的示例为例,它应该始终优先激活其中名称为"hello kitty"和"meshuggah"的产品,然后再进行其他操作)

I have a large products database, and every sunday, my script is activating 50 new products. the thing is, based on some rules in my script, im finding some products i should prioritize activating. (for your example, lets say it should always prioritize activating products with the name 'hello kitty' and 'meshuggah' in them, before everything else)

我的数据库表包含有关产品的信息和唯一ID. 可以说脚本找到了这些ID中的5个,标题中有Hello Kitty.

my db table contains info about the product, and unique id. lets say that the script finds 5 of these ids, having hello kitty in the title.

那么,我该如何进行查询,以便优先级最高的项目排在最前面,然后是ID desc?

so, how can i make the query, so that the prioritized items will come on top, and then followed by id desc?

这里是我之后的一些伪代码:

here some pseudocode of what im after:

SELECT * FROM products order by (id='59', id='47', id='28', id='29', id='20'), id desc limit 50

结果应该先给我id 59、47、28、29和20,然后再在产品表中按id降序排列其他id.

the result should give me id 59, 47, 28, 29 and 20 first, then followed by other ids in the products table sorted by id descending.

在一个查询中有可能吗?

is that possible in one query?

推荐答案

使用FIELD

SELECT * 
FROM products 
order by FIELD(id,59,47,28,29,20), id desc 
limit 50

  • 使用MySQL按特定字段值排序
    • Ordering by specific field values with MySQL
    • 这篇关于如何按优先级ID列表对mysql结果排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆