我想在我的前面选择前5个用户名 [英] I want first 5 username on top which will be chosen by me

查看:68
本文介绍了我想在我的前面选择前5个用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有usrName列的表测试。 table包含10个不同的UserName。

现在我想要检索所有UserName但我想要在我的前面选择前5个userName,然后我会休息



我尝试了什么:



i想要在我选择的前5个userName

i have a table test with a column usrName. table contains 10 different UserName.
now i want to retrieve all UserName but i want first 5 userName on top which will be chosen by me and rest after then

What I have tried:

i want first 5 userName on top which will be chosen by me

推荐答案

您可以添加一个包含TopUser信息的aditional列。

You can add an aditional column which holds the information TopUser or not.
CREATE TABLE test
(
   UserName char(30),
   TopMemberPos integer default 1
)



我们自己定义,'0'表示TopMember。



测试数据:


We define by ourself, that '0' means TopMember.

Test data:

INSERT INTO test (UserName, TopMemberPos) Values
('U1', '1'),
('U2', '0'),
('U3', '1'),
('U4', '0'),
('U5', '1'),
('U6', '0'),
('U7', '1'),
('U8', '0'),
('U9', '0');



测试查询:


Test query:

SELECT UserName from test order by TopMemberPos, UserName;



Testresult:


Testresult:

UserName
--------
U2                            
U4                            
U6                            
U8                            
U9                            
U1                            
U3                            
U5                            
U7                            



我希望它有所帮助。





更直观的逻辑/名称/惯例


I hope it helps.


More intuitive logic/Name/conventions

CREATE TABLE test
(
   UserName char(30),
   TopMember integer default 0
)



我们自己定义,'1'表示TopMember。



测试数据:


We define by ourself, that '1' means TopMember.

Test data:

INSERT INTO test (UserName, TopMember) Values
('U1', '0'),
('U2', '1'),
('U3', '0'),
('U4', '1'),
('U5', '0'),
('U6', '1'),
('U7', '0'),
('U8', '1'),
('U9', '1');



测试查询:


Test query:

SELECT UserName from test order by TopMember DESC, UserName;



Testresult:与上面相同


Testresult:Same as above


以各种方式可以做到这一点。

1.你可以向表中添加另一列以设置排序顺序,对于休息,您可以设置一个大的数量。作为排序顺序,让我们为你的情况说99.

2.在ORDER BY-

In various ways this can be done.
1. You can add another column to the table to set the sort order and for rest all you can set a large no. as sort order, let's say 99 for your case.
2. Using CASE WHEN in ORDER BY-
SELECT UuserName,....
FROM YourTable
ORDER BY CASE WHEN UserName IN('user9','user3'..) THEN 1 ELSE 99 END





还有很多其他方法可以做到这一点。



希望,它有帮助:)



There are many other approaches to do this too.

Hope, it helps :)


尝试使用 union [ ^ ]



try using union[^]

select usrName from mytable where usrName in ( 'user1','user2','user3','user4','user5')
union
select usrName from mytable where usrName not in ( 'user1','user2','user3','user4','user5')


这篇关于我想在我的前面选择前5个用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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