MySQL ORDER问题 [英] MySQL ORDER problem

查看:46
本文介绍了MySQL ORDER问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的SQL查询,但是缺少某些内容,但是我还没有找到解决该问题的答案. 事实是,我选择了一些带有多个ID的字段,并且希望结果按此特定顺序排序.

I have a pretty simple SQL query but something is missing and I didn't find an answer for this problem yet. The thing is that I select some fields with several ids and I want the result to be ordered in this particular order.

查询如下

SELECT `content`.* 
FROM   `content` 
WHERE  (user_id = "1" AND ( id = "4" OR id = "7" OR id = "5" OR id = "8" )) 

默认顺序为"id ASC"(id是我的主键),但在这种情况下,我希望该顺序为4,7,5,8.

The default order is "id ASC" (id is my primary key), but I want the order to be 4,7,5,8 in this particular case.

有什么想法吗?

推荐答案

这将满足您的要求:

select * 
from myTable
order by field(myID, 8, 7, 6) desc;

您可以设置要显示的任何ID(或其他任何ID)的顺序,此后将跟随其他任何ID.希望有帮助.

You can set the order of whatever ID's (or whatever) youwant to appear, and any others will follow after that. Hope that helps.

有关此内容的更多信息@ http://dev .mysql.com/doc/refman/5.0/zh-CN/string-functions.html#function_field

More on this @ http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_field

这篇关于MySQL ORDER问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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