【问题标题】:Returning Conditional Columns in SQL Server在 SQL Server 中返回条件列
【发布时间】:2018-01-21 20:01:19
【问题描述】:

在这里完成 SQL Server 菜鸟。我不知道如何去寻找答案,因为我大多不知道从什么开始搜索。

我的 SQL Server 数据库中有 BLOG 表,用于存储类似的博客文章

ID Title                             DatePosted
-----------------------------------------------
1  Why Batman is Greater than Sup... 07/15/2017
2  10 Reasons Superman is the wor... 08/02/2017 
3  Sick of Metropolis? Move to Go... 08/03/2017

我有另一个关系表存储用户喜欢的博客,即,

UserID  PostID  DateLiked
-------------------------------
232413  2       08/03/2017
232413  1       07/30/2017
234285  2       08/03/2017

现在我想做的是在我的BLOG 表上调用一个简单的SELECT *,但将UserID 作为参数传递给该查询以确定该用户是否喜欢该博客,所以我的结果集看起来像这样。

给定用户 ID:232413

ID Title                             DatePosted  IsLiked
--------------------------------------------------------
1  Why Batman is Greater than Sup... 07/15/2017  1
2  10 Reasons Superman is the wor... 08/02/2017  1
3  Sick of Metropolis? Move to Go... 08/03/2017  0

这在 SQL Server/数据库中可行吗?非常感谢任何提示或有用的阅读!

【问题讨论】:

  • 为什么Sick of Metropolis? Move to...IsLiked = 1 ?喜欢的帖子 ID 是 1 和 2。
  • 啊,谢谢@rcs

标签: sql-server database


【解决方案1】:

假设 DateLiked 列可以为空。
我将大小写的结果转换为比特,因为我认为您希望将其作为布尔值。
将 userId 参数作为 @paramUserId 传递

DECLARE @paramUserId AS INT;
SELECT b.Id,
       b.Title,
       b.DatePosted,
       CAST(CASE WHEN sb.DateLiked IS NULL THEN 0 ELSE 1) AS BIT) AS IsLiked
FROM BLOG AS b
INNER JOIN storesBlogs AS sb ON b.ID = sb.PostID
WHERE sb.UserID = @paramUserId 

【讨论】:

  • 注意:应该是LEFT JOIN,而不是INNER JOINWHERE 子句应该是连接条件的一部分。
  • 你测试过你的代码吗?根据我的理解,如果“like”表中没有这个UserId的相关记录,那么查询结果应该是空的。
  • 是的,如果没有用户与博客有关系,则结果为空
  • 把 sb.UserId = @paramUserId 作为 where 或 on 条件的一部分应该没有区别。
  • 我喜欢在where子句中指定参数。
【解决方案2】:

这是在 sqlite3 上的结果。

select a.ID, a.Title, a.DatePosted, case UserId when 232413 then 1 else 0 end IsLiked from BLOG a inner join LikeTable b on a.id = b.post_id;

2|10 Reasons Superman is the wor...|08/02/2017|1
1|Why Batman is Greater than Sup...|07/15/2017|1
2|10 Reasons Superman is the wor...|08/02/2017|0

select a.ID, a.Title, a.DatePosted, case UserId when 232413 then 1 else 0 end IsLiked from BLOG a left join LikeTable b on a.id = b.post_id;

1|Why Batman is Greater than Sup...|07/15/2017|1
2|10 Reasons Superman is the wor...|08/02/2017|1
2|10 Reasons Superman is the wor...|08/02/2017|0
3|Sick of Metropolis? Move to Go...|08/03/2017|0

没有结果满足 PO 的要求,但声明#2 值得考虑。

【讨论】:

    【解决方案3】:
    select id,title,dateposted,0 as IsLiked from blog where id not in (select postid from user_liked where  userid =232413)
    union 
    select id,title,dateposted,1 as IsLiked from blog where id in (select postid from user_liked where  userid =232413)
    order by  IsLiked desc 
    
    Result -
    
    id  title                              dateposted IsLiked
    
    1   Why Batman is Greater than Sup...   2017-07-15 1
    
    2   10 Reasons Superman is the wor...   2017-08-02 1
    
    3   Sick of Metropolis? Move to Go...   2017-08-03 0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-07
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多