简体   繁体   中英

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. 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 =)

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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM