MySql首先按特定字段值排序不起作用

时间:2014-09-16 08:23:45

标签: mysql sql-order-by

你好我有一个新闻页面,我想在顶部显示用户的城市新闻。 例如,这些是按时间顺序下降的新闻。

+----------+-----------+-----------------------+
| CityCode | entrytime |      newsheader       |
+----------+-----------+-----------------------+
|       11 |      3800 | great opening         |
|       10 |      3700 | flood alert           |
|       12 |      3600 | new mall              |
|       13 |      3500 | pollution at the city |
|       13 |      3400 | new mayor             |
|       12 |      3300 | house fire            |
|       11 |      3200 | traffic accident      |
|       10 |      3000 | Festival at city      |
+----------+-----------+-----------------------+

我住在这个城市12.我想在顶部看到CityCode = 12,然后是其他类似的新闻。

+----------+-----------+-----------------------+
| CityCode | entrytime |      newsheader       |
+----------+-----------+-----------------------+
|       12 |      3600 | new mall              |
|       12 |      3300 | house fire            |
|       11 |      3800 | great opening         |
|       10 |      3700 | flood alert           |
|       13 |      3500 | pollution at the city |
|       13 |      3400 | new mayor             |
|       11 |      3200 | traffic accident      |
|       10 |      3000 | Festival at city      |
+----------+-----------+-----------------------+

我试过

Select * from news order by FIELD(CityCode,12),entrytime desc

Select * from news order by CityCode=12,entrytime desc 

这些代码不起作用。我使用mysql 5.5 我怎样才能解决这个问题?感谢

2 个答案:

答案 0 :(得分:1)

试试这个:在你的order by子句中添加desc为FIELD(CityCode,12)

Select * from news order by FIELD(CityCode,12) desc,entrytime desc

检查ORDER BY的FIELD函数的限制

Order BY using FIELD function

答案 1 :(得分:0)

尝试以下查询: -

SELECT *
FROM NEWS
ORDER BY CASE WHEN CityCode=12 THEN 1 ELSE 2 END;

这是小提琴 -

http://sqlfiddle.com/#!2/f5dde/1