[英]Order rows according to which condition is met?
I have a pretty simple question: Is it possible to order the rows retrieved according to the which condition is met?我有一个非常简单的问题:是否可以根据满足的条件对检索到的行进行排序? For example, I have a table of people, and I want to retrieve all people whose names begin with an "I", or end with an "ster", or contain "lo", ordered according to which condition of these is met.
例如,我有一个人表,我想检索所有名字以“I”开头,或以“ster”结尾,或包含“lo”的人,并根据满足这些条件的顺序进行排序。 First the rows that match the first condition, then the rows that match the second, and so on.
首先是匹配第一个条件的行,然后是匹配第二个条件的行,依此类推。 ( Without duplicated: if a row meets the first condition, it shouldn't show again for the second condition)
(没有重复:如果一行满足第一个条件,则不应再次显示第二个条件)
Edit: I work with Visual C#, and I manage the DB with MS Access.编辑:我使用 Visual C#,并使用 MS Access 管理数据库。 (The file format is.mdb, if that matters)
(文件格式是.mdb,如果重要的话)
Thank you =)谢谢 =)
Generally speaking, you can put a case statement in your order by
.一般来说,你可以在你的
order by
中加上一个案例陈述。
This would work in SQL Server, for example:这适用于 SQL 服务器,例如:
order by (case when myCol like 'I%' then 1 when myCol like '%ster' then 2 when myCol like '%lo%' then 3 end)
Which DBMS are you using?
您使用哪个数据库管理系统?
UPDATE更新
For MS-ACCESS, you could use the IIF
statement as shown in this answer:对于 MS-ACCESS,您可以使用此答案中所示的
IIF
语句:
ms-access-complicated-order-by ms-access-复杂-order-by
Based upon that, you probably want something along the lines of:基于此,您可能需要以下内容:
SELECT *
FROM people
WHERE [name] Like "I*" Or [name] Like "*ster" Or [name] Like "*lo*"
ORDER BY IIf([name] Like "I*", 1, IIf([name] Like "*ster", 2, IIf([name] Like "*lo*", 3, 4)));
Something like this ought to work:这样的事情应该有效:
SELECT * FROM people
ORDER BY
CASE WHEN name LIKE "I%" THEN 0
WHEN name LIKE "%ster" THEN 1
WHEN name LIKE "%lo%" THEN 2
ELSE 3
END ASC;
In Access you may have to resort to nested IIF()
s though:在 Access 中,您可能不得不求助于嵌套的
IIF()
:
ORDER BY
IIF( name LIKE "I%", 0,
IIF( name LIKE "%ster%", 1,
IIF( name LIKE "%lo%", 2,
3
) ) ) ASC
I gave this a shot with Postgres, but I would assume you could use a similar technique with other DBs.我用 Postgres 试了一下,但我假设您可以对其他数据库使用类似的技术。 I would create a column for each condition which returns a Boolean expression of whether the condition is met.
我会为每个条件创建一个列,返回一个 Boolean 表达式是否满足条件。 Then, order by that column:
然后,按该列排序:
select
substr(Name, 1, 1) = 'I' as StartsWithI,
Name like '%ster' as EndsWithSter
from MyTable
order by StartsWithI desc, EndsWithSter desc
UPDATE:更新:
The ms-access
tag was added after I posted this answer, but I'll leave it up in case it helps anyone. ms-access
标签是在我发布此答案后添加的,但我会保留它以防它对任何人有帮助。
The Switch
function is much more readable than a bunch of nested IIf
statements: Switch
function 比一堆嵌套的IIf
语句更具可读性:
SELECT * FROM [People]
WHERE [Name] Like "I*"
OR [Name] Like "*ster"
OR [Name] Like "*lo*"
ORDER BY Switch([Name] Like "I*", 1,
[Name] Like "*ster", 2,
True, 3)
Switch
is used by passing argument pairs of conditional : result .通过传递conditional : result参数对来使用
Switch
。 Argument 1 is evaluated and if true the function returns argument 2. If arg 1 is false, then arg 3 is evaluated, etc.评估参数 1,如果 function 为真,则返回参数 2。如果参数 1 为假,则评估参数 3,依此类推。
Note the use of True
as the penultimate argument which acts like a CASE ELSE
.请注意使用
True
作为倒数第二个参数,其作用类似于CASE ELSE
。
Be aware that depending on the context, you may need to convert *
to %
.请注意,根据上下文,您可能需要将
*
转换为%
。 See Why does LIKE behave differently when being called from VB6 app?请参阅为什么 LIKE 在从 VB6 应用程序调用时表现不同? for more info.
获取更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.