1

我昨天在这里问了一个问题

现在我改变了我的代码如下

declare @like bit, @dislike bit

IF EXISTS (select * from likeordislike 
           where comment_id = @comment_id and likevar = 1 and dislikevar = 0)
BEGIN
    SET @like    = 'true'
    SET @dislike = 'false'
END
ELSE IF EXISTS (select * from likeordislike 
                where comment_id = @comment_id and likevar = 0 and dislikevar = 1)
BEGIN
    SET @like    = 'false'
    SET @dislike = 'true'
END
ELSE
BEGIN
    SET @like = 'false'
    SET @dislike ='false'
END

SELECT     
    comment_id, comment_post_id, comment_user_id, 
    comment_text, comment_like, comment_dislike, 
    comment_reply_to_id, comment_date,
    @like as begendi,
    @dislike as begenmedi
FROM         
    comment 
ORDER BY 
    comment_date DESC

我正在尝试选择评论和评论喜欢或不喜欢状态。

我在我的 asp.net 网页上使用数据列表。我需要每个评论的返回值。我必须提供@comment_id 参数,此查询为每个评论(begendi = 1, begenmedi = 0)返回相同的结果。我怎么解决这个问题?

ScopeIdentity还是需要返回值?谢谢(我有两个表 1-comment 表,2-likeordislike 表,其中包含 comment_id、user_id、likevar、dislikevar 和 eventdate 列)

select * 
from likeordislike 
where comment_id = @comment_id and likevar = 0 and dislikevar = 1
4

1 回答 1

1

我不确定您究竟想在这里实现什么,但我认为您可以在不使用程序方法并仅使用连接的情况下实现您想要的结果:

SELECT  comment_id, 
        comment_post_id, 
        comment_user_id, 
        comment_text, 
        comment_like, 
        comment_dislike, 
        comment_reply_to_id, 
        comment_date,
        begendi = CAST(CASE WHEN LikeOrDislike.Likes > 0 OR LikeOrDislike.Dislikes = 0 THEN 1 ELSE 0 END AS BIT),
        begenmedi = CAST(CASE WHEN LikeOrDislike.Dislikes > 0 AND LikeOrDislike.Likes = 0 THEN 1 ELSE 0 AS BIT)
FROM    Comment
        LEFT JOIN 
        (   SELECT  Comment_ID,
                    Likes = COUNT(CASE WHEN LikeVar = 1 AND DislikeVar = 0 THEN 1 END),
                    Dislikes = COUNT(CASE WHEN LikeVar = 0 AND DislikeVar = 1 THEN 1 END),
                    Other = COUNT(CASE WHEN LikeVar = DislikeVar THEN 1 END)
            FROM    LikeOrDislike
            GROUP BY Comment_ID
        ) LikeOrDislike
            ON Comment.Comment_ID = LikeOrDislike.Comment_ID
ORDER BY comment_date DESC
于 2013-04-11T09:39:02.103 回答