How can i sort a mysql result by a list of prioritised ids?

前端 未结 2 391

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 pr

相关标签:
2条回答
  • 2021-01-21 20:54

    use FIELD

    SELECT * 
    FROM products 
    order by FIELD(id,59,47,28,29,20), id desc 
    limit 50
    
    • Ordering by specific field values with MySQL
    0 讨论(0)
  • 2021-01-21 20:55

    Actually, your pseudo code also comes pretty close to such a query:

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

    This uses the fact that a condition evaluates to 0 or 1.

    0 讨论(0)
提交回复
热议问题