SQL返回值作为参数



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

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

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-评论表,2-喜欢或不喜欢表,有comment_id, user_id, likevar, dislikevar和eventdate列)

select * 
from likeordislike 
where comment_id = @comment_id and likevar = 0 and dislikevar = 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

相关内容

  • 没有找到相关文章

最新更新