简体   繁体   English

检查包含项目以逗号分隔的列表作为SQL Server中的查询结果

[英]check contain item in comma separated list as a query result in sql server

I have a Post table in SQL Server and for performance reason, this table has a column(UsersWhoLiked) in the form of comma separated values that specify which UserIds that liked a Post as following. 我在SQL Server中有一个Post表,出于性能原因,该表有一个以逗号分隔值形式的列(UsersWhoLiked),用于指定喜欢以下Post的UserId。

Id | Subject |  UsersWhoLiked
---|---------|-----------
1  | red     |  u1,u2
2  | blue    |  u2, u3,u4,u5
3  | green   |  
4  | gray    |  u3, u4, u7

I want to write a query that gets UserId as a parameter and return List of Post as a result so that in the result, every post have a column that specifies the user liked or not the post. 我想编写一个将UserId作为参数并返回结果列表的查询,以便在结果中,每个帖子都有一个列来指定用户喜欢还是不喜欢该帖子。

Example: For UserId: u3 示例:对于UserId:u3

I expect that: 我希望:

Id | Subject |  Liked
---|---------|-----------
1  | red     |  0
2  | blue    |  1
3  | green   |  0
4  | gray    |  1

Query: 查询:

SELECT Id, Subject FROM Post

But I don't know how to add Liked column to the Select statement 但是我不知道如何将喜欢的列添加到选择语句

You can use the following: 您可以使用以下内容:

SELECT Id, Subject, 
    IIF(CHARINDEX('u3,', REPLACE(CONCAT(UsersWhoLiked, ','), ' ', '')) > 0, 1, 0) AS Liked 
FROM Post

demo: http://sqlfiddle.com/#!18/3a588/1/0 演示: http : //sqlfiddle.com/#!18/3a588/1/0


You should use a second table to store the likes: 您应该使用第二个表来存储点赞:

CREATE TABLE PostLikes (
    PostId INT FOREIGN KEY REFERENCES Post(Id),
    UserId INT FOREIGN KEY REFERENCES Users(Id),
    CONSTRAINT PK_PostLikes PRIMARY KEY (PostId, UserId)
);

So you can use the following SELECT to get the expected information: 因此,您可以使用以下SELECT获取所需的信息:

SELECT Id, Subject, IIF(ISNULL(PostLikes.UserId, 0) > 0, 1, 0) AS Liked
FROM Post LEFT JOIN PostLikes ON Post.Id = PostLikes.PostId AND PostLikes.UserId = 3

demo: http://sqlfiddle.com/#!18/176ce/4/0 演示: http : //sqlfiddle.com/#!18/176ce/4/0

You can try the below: 您可以尝试以下方法:

DECLARE @id varchar(10)='u3'
SELECT Id, Subject, 
CASE WHEN (',' + UsersWhoLiked + ',') LIKE '%,'+@id+',%' THEN 1 ELSE 0 END Liked
FROM Post

I don't how storing comma separated values improves performance, but this is bad idea. 我不存储逗号分隔的值如何提高性能,但这不是一个好主意。 In your case you should be careful with substrings. 在您的情况下,应该小心使用子字符串。 Such as, if you are looking for u3 , it should not match u31 . 例如,如果您正在寻找u3 ,则它不应与u31匹配。 So put , before and after each value in UsersWhoLiked column 因此,UsersWhoLiked列中的每个值之前和之后UsersWhoLiked

declare @userId varchar(10) = 'u3'
select 
    Id, Subject
    , Liked = isnull(sign(charindex(',' + @userId + ',', ',' + UsersWhoLiked + ',')), 0)
from Post
select *, case PATINDEX('%u3%', userwholikes) --dont use u3(comma), only use u3
when 0 then                                   --because u3 becomes last one
0
else 1
end likes
from yourtable

i give this, by my understanding.. 根据我的理解,我给出了这个。

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

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