查询同一表以创建搜索屏幕的订购
我有一个表用户
和recentsearch
以及一个允许用户查找其他用户的屏幕。服务器返回用户数组,而无需考虑朋友和最近的搜索,我想包括IT客户端(客户端是指运行这些查询的iOS应用程序)。 我希望朋友和恢复搜索从服务器返回的东西中获得优先级。
我有点卡住。我不确定该怎么做。我在下面进行了查询,但这不起作用,因为它要求所有用户将设置为1的flag ShowonSearchScreen
设置为1,但这并不总是适用于朋友和最近的搜索。
flag showonsearchscreen 为服务器返回的用户设置为true。这意味着,用户
可以将标志设置为1,也可以成为朋友。
这些是我的表格:
create table User (
userUuid text not null primary key,
username text not null,
showOnSearchScreen int not null,
friends int not null
);
create table RecentSearch (
userUuid text not null primary key,
foreign key (userUuid) references User (userUuid) on delete cascade
);
这是我的查询,它不起作用(假设用户在某处搜索用“ g”的用户):
select *,
case
when exists(select 1
from User CustomUser
where User.userUuid = CustomUser.userUuid
and CustomUser.friends = 1
and CustomUser.username like '%g%') then 2
else 0 end as friendScore,
case
when exists(select 1
from RecentSearch
join User CustomUser on RecentSearch.userUuid = CustomUser.userUuid and
User.userUuid = RecentSearch.userUuid
where CustomUser.username like '%g%') then 1
else 0 end as recentSearchScore
from User
-- This is wrong...
where showOnSearchScreen = 1
order by friendScore + recentSearchScore;
是否必须通过外部连接来解决这?我有点困惑。
因此,关键点:
- 服务器将
user
s发送给客户端,客户端将它们upsert在数据库中,并将flag showonSearchScreen设置为1, - 查询应至少返回所有使用该标志的用户,将所有
- 用户设置为1个我希望是朋友,并且是朋友,并且具有与输入相似的用户名(我猜是withext%的效果都足够好)以优先于从服务器返回的用户,
- 适用于
user
s recentsearch ,如果用户从recentsearch
加入具有相似的用户名,请优先于从服务器返回的用户(但是朋友应在顶部显示)
I have a table User
and RecentSearch
and a screen on a website which allows the user to find other users. The server returns a user array without taking into account the friends and recent searches, I want to include it client side (the client means an iOS app running these queries). I want friends and recents searches to take precedence from the stuff the server returns.
I am a bit stuck. I am not sure how to do this. I made a query below, but that doesn't work because it requires all users to have the flag showOnSearchScreen
set to 1, but that doesn't always apply for friends and recent searches.
The flag showOnSearchScreen
is only set to true for users returned by the server. This means that is possible for a User
to have flag showOnSearchScreen
set to 1 and also be friends.
These are my tables:
create table User (
userUuid text not null primary key,
username text not null,
showOnSearchScreen int not null,
friends int not null
);
create table RecentSearch (
userUuid text not null primary key,
foreign key (userUuid) references User (userUuid) on delete cascade
);
This is my query which does not work (assuming the user is searching for users with a 'g' somewhere):
select *,
case
when exists(select 1
from User CustomUser
where User.userUuid = CustomUser.userUuid
and CustomUser.friends = 1
and CustomUser.username like '%g%') then 2
else 0 end as friendScore,
case
when exists(select 1
from RecentSearch
join User CustomUser on RecentSearch.userUuid = CustomUser.userUuid and
User.userUuid = RecentSearch.userUuid
where CustomUser.username like '%g%') then 1
else 0 end as recentSearchScore
from User
-- This is wrong...
where showOnSearchScreen = 1
order by friendScore + recentSearchScore;
Must this be resolved with outer joins? I am a bit confused.
So keypoints:
- Server sends
User
s to client, client upserts them in the database and sets flag showOnSearchScreen to 1 - The query should return at least all users with that flag set to 1
- I want users who are friends and have a username similar to the input (I guess %sometext% would work good enough) to take precedence over the users returned from the server
- Same applies for
User
s fromRecentSearch
, if a user joined fromRecentSearch
has a similar username, take precedence over the users returned from the server (but friends should be shown at the top)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
IIUC-考虑
Union
查询,该查询可能会使用分数来解决您的子弹点以确定优先级或订购:IIUC - Consider a
UNION
query that potentially resolves your bullet points using score to determine precedence or ordering: