如何首先返回具有特定值的行? [英] How do I return rows with a specific value first?

查看:23
本文介绍了如何首先返回具有特定值的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的查询首先返回表中某列包含特定值的行,然后按字母顺序返回其余的行.

I want my query to return the rows of the table where a column contains a specific value first, and then return the rest of the rows alphabetized.

如果我有一张类似于这个例子的表格:

If I have a table something like this example:

 - Table: Users
 - id - name -  city
 - 1    George  Seattle
 - 2    Sam     Miami
 - 3    John    New York
 - 4    Amy     New York
 - 5    Eric    Chicago
 - 6    Nick    New York

并且使用该表,我希望我的查询首先返回包含纽约的行,然后返回按城市字母顺序排列的其余行.这是否可以仅使用一个查询来完成?

And using that table I want to my query to return the rows which contain New York first, and then the rest of the rows alphabetized by city. Is this possible to do using only one query?

推荐答案

在 SQL Server、Oracle、DB2 和许多其他数据库系统上,这是您可以使用的:

On SQL Server, Oracle, DB2, and many other database systems, this is what you can use:

ORDER BY CASE WHEN city = 'New York' THEN 1 ELSE 2 END, city

这篇关于如何首先返回具有特定值的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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