【问题标题】:mysql: is there any way we can fetch records in different order than stored order?mysql:有什么方法可以按照与存储顺序不同的顺序获取记录?
【发布时间】:2017-08-23 15:33:05
【问题描述】:

我们有下表的列: 类别 ID |以 CategoryId 作为主键的 CategoryName。

我们有以下数据:

0   Other
1   Bumper
2   Door
3   Roof
4   Fender

我从这些表中使用 select * 并且我只获得上述序列中的行,即首先是 0,最后是 4。

有什么办法可以让我最终得到 0 吗?即 1,2,3,4,0?

【问题讨论】:

标签: mysql


【解决方案1】:
select * from yourtable order by CategoryId=0,CategoryId

您可以在 order by 子句中使用任意表达式。通过首先按 CategoryId=0 排序,您将获得所有为假的记录,然后是为真的记录(显然只有一个,因为它是主键)。然后每组记录按 CategoryId 排序。

【讨论】:

    猜你喜欢
    • 2014-04-25
    • 2013-01-24
    • 1970-01-01
    • 2017-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 2022-11-01
    相关资源
    最近更新 更多