Order rows according to which condition is met?

时光毁灭记忆、已成空白 提交于 2020-01-05 02:51:32

问题


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. 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. (The file format is .mdb, if that matters)

Thank you =)


回答1:


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:

ORDER BY
  IIF( name LIKE "I%",     0,
  IIF( name LIKE "%ster%", 1,
  IIF( name LIKE "%lo%",   2,
    3
  ) ) ) ASC



回答2:


Generally speaking, you can put a case statement in your order by.

This would work in SQL Server, for example:

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-complicated-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)));



回答3:


I gave this a shot with Postgres, but I would assume you could use a similar technique with other DBs. I would create a column for each condition which returns a Boolean expression of whether the condition is met. 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.




回答4:


The Switch function is much more readable than a bunch of nested IIf statements:

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. Argument 1 is evaluated and if true the function returns argument 2. If arg 1 is false, then arg 3 is evaluated, etc.

Note the use of True as the penultimate argument which acts like a 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? for more info.



来源:https://stackoverflow.com/questions/10093814/order-rows-according-to-which-condition-is-met

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!