简体   繁体   English

MySql按顺序排列数组

[英]MySql order by case when array

I'd like to know if there is more efficient way to write the loop of cases. 我想知道是否有更有效的方法来编写案例循环。

$search = "ORDER BY CASE WHEN created_by = :created_by THEN -1                                             
           WHEN cat_id = 24  THEN -1 
           WHEN cat_id = 26  THEN -1 
           ELSE created_at END LIMIT :limit, :perpage";

I wonder if 我怀疑是否

WHEN cat_id = 24  THEN -1 
WHEN cat_id = 26  THEN -1 

could be 可能

WHEN cat_id = IN(24,26) THEN -1

if not how can I go about it? 如果不是我该怎么办呢? Do I have to make some sort of a for loop? 我是否必须制作某种for循环?

If it's just two values then you can use OR operator, eg: 如果它只是两个值,那么你可以使用OR运算符,例如:

WHEN (cat_id = 24 or cat_id = 26)  THEN -1

If you need to compare with multiple values but they are not sequencial then you can use IN operator, eg 如果您需要与多个值进行比较但它们不是顺序的,那么您可以使用IN运算符,例如

WHEN (cat_id IN (24, 26))  THEN -1

If the values are in range then you can use BETWEEN , eg: 如果值在范围内,那么您可以使用BETWEEN ,例如:

WHEN (cat_id BETWEEN 21 and 30)  THEN -1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM