MySql 首先按特定字段值排序不起作用 [英] MySql Ordering by specific field value first does not work

查看:132
本文介绍了MySql 首先按特定字段值排序不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个新闻页面,我想在顶部显示用户所在的城市新闻.例如,这些是按时间降序排列的新闻.

Hello I have a news page, I want to show the user's city news at the top. For example these are the news ordered descending by time.

+----------+-----------+-----------------------+
| 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,然后是其他类似的新闻.

I live in the city 12. I want to see CityCode=12 at the top, then other news like this.

+----------+-----------+-----------------------+
| 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我怎样才能解决这个问题?谢谢

these codes did not work. I use mysql 5.5 how can i fix this? Thanks

推荐答案

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

try this : add desc in your order by clause for FIELD(CityCode,12)

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

检查 ORDER BY 的 FIELD 函数的限制

Check the limitations of FIELD function for ORDER BY

使用 FIELD 函数排序

这篇关于MySql 首先按特定字段值排序不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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