[英]MySQL PHP - SELECT WHERE based on a different position
Is it possible to sort data based on an order value set on a certaid IDs? 是否可以根据在证书ID上设置的订单值对数据进行排序?
I want to make the following: 我要进行以下操作:
ID - Position
1 - 3rd
2 - 4th
3 - 5th
4 - 6th
10 - 7th
11 - 8th
12 - 9th
13 - 10th
14 - 11th
83 - 2nd
84 - 1st
I've been trying to implement this one, but it doesn't generate the correct order: 我一直在尝试实现这一点,但它不会生成正确的顺序:
SELECT * FROM table_name WHERE id IN (1,2,3,4,5,6,7,81,82) ORDER BY id = 3 DESC, id = 4 DESC, id = 5 DESC, id = 6 DESC, id = 7 DESC, id = 8 DESC, id = 9 DESC, id = 1 DESC, id = 2 DESC
If I was unclear, please ask me for more info. 如果我不清楚,请询问我更多信息。 Thank you in advance.
先感谢您。
SELECT * FROM table_name WHERE id IN (1,2,3,4,5,6,7,81,82) ORDER BY CAST(SUBSTR(position, 1, CHAR_LENGTH(position) - 2) as unsigned)
Yes you can use order by FIELD
, below is an example you can modify the way you want. 是的,您可以
order by FIELD
使用order by FIELD
,以下是您可以修改所需方式的示例。
SELECT * FROM
table_name
WHERE id IN (1,2,3,4,5,6,7,81,82)
order by FIELD(id,3,4,5,1,2,6,7,81,82)
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_field http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_field
I think this is sufficient you dont need to add anything in this. 我认为这已经足够,您无需在其中添加任何内容。
SELECT * FROM table_name WHERE id IN (1,2,3,4,5,6,7,81,82) ORDER BY id DESC
or you can use, if want in particular sequence of ID, 或者您可以使用(如果需要特定的ID顺序),
SELECT * FROM table_name WHERE id IN (1,2,3,4,5,6,7,81,82) order by FIELD(id,3,4,5,6,7,8,9,1,2)
or you can use ORDER BY CASE 或者您可以使用ORDER BY CASE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.