简体   繁体   English

多项选择-相同的表格

[英]MULTIPLE SELECTS - SAME TABLE

Here goes... 开始...

I have 5 records in a table entitled MyTable with a single field called Se. 我在名为MyTable的表中有5条记录,其中有一个名为Se的字段。 The 5 records contain the following values (1-5): 5条记录包含以下值(1-5):

se=1 se=2 se=3 se=4 se=5

I want to have the records returned to me as follows: 我希望将记录返回给我,如下所示:

SELECT * FROM MyTable WHERE se >= 3
UNION
SELECT * FROM MyTable WHERE se < 3
ORDER BY se ASC

My objective is to get records returned as: 我的目标是将记录返回为:

3,4,5,1 2 3,4,5,1 2

but naturally I get... 但我自然会...

1,2,3,4,5 1,2,3,4,5

Can you help me? 你能帮助我吗? Can MSSQL Server even do this? MSSQL Server可以做到这一点吗?

Thanks in advance for any assistance. 在此先感谢您的协助。

Try this 尝试这个

SELECT 1, * FROM MyTable WHERE se >= 3
UNION ALL
SELECT 2, * FROM MyTable WHERE se < 3
ORDER BY 1, se ASC

You can use a single select and a condition in the sorting: 您可以在排序中使用单选和条件:

select *
from MyTable
order by (case when se >= 3 then 0 else 1 end), se

(The parentheses around the case is not needed, I just added them to make the code clearer.) (不需要在括号中加上括号,我只是添加了它们以使代码更清楚。)

If you're looking for a hack for that specific scenario: 如果您正在寻找针对特定情况的黑客:

SELECT * FROM MyTable ORDER BY (se + 2) % 5

Example on PostgreSQL: PostgreSQL上的示例:

$ WITH MyTable(se) AS (VALUES
$     (1), (2), (3), (4), (5)
$ )
$ SELECT * FROM MyTable ORDER BY (se + 2) % 5;
 se
----
  3
  4
  5
  1
  2
(5 rows)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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