简体   繁体   English

关于SQL Server查询和加入

[英]Regarding SQL Server query and joining

Basically I have 3 tables user , colors and usercolor . 基本上我有3个表usercolorsusercolor

Table structure: 表结构:

  • User table has columns like UserID , UserName User表具有诸如UserIDUserName
  • Color table has columns like ColorID , ColorName Color表具有诸如ColorIDColorName
  • UserColor table has columns UserID , ColorID UserColor表具有UserIDColorID

When user login and go to my view first time then all color will be fetch and show in view with checkboxes as a result user can select any or many. 当用户首次登录并进入我的视图时,所有颜色都将被提取并带有复选框显示在视图中,因此用户可以选择任何一个或多个。

When any user select any color or many color from UI and save in db then userid and color id will be saved in user colors table but the same user again come to view then all colors will be show and if he previously selected anyone then those color name checkbox will be shown as checked. 当任何用户从UI中选择任何一种颜色或多种颜色并保存在db中时,userid和color id将被保存在用户颜色表中,但是同一用户再次查看则将显示所有颜色,如果他先前选择了任何人,则这些颜色名称复选框将显示为选中状态。

I prepare a SQL statement to fetch all color data joining two table colors & usercolor , but the moment I specify userid then one records is fetching that is not right. 我准备了一条SQL语句来获取所有连接两个表colorsusercolor颜色数据,但是当我指定userid一条记录就被提取了,这是不对的。 I want to show always all distinct color name for every user and if user selected any then that color name show be shown by selected checkbox. 我想始终为每个用户显示所有不同的颜色名称,如果用户选择了任何颜色,则该颜色名称将通过选中的复选框显示。

Here is my query: 这是我的查询:

SELECT        
    dbo.Colors.ColorID, dbo.Colors.ColorName, 
    ISNULL(dbo.UserColor.UserID, 0) AS IsSelected
FROM
    dbo.UserColor 
INNER JOIN
    dbo.Users ON dbo.UserColor.UserID = dbo.Users.UserID 
RIGHT OUTER JOIN
    dbo.Colors ON dbo.UserColor.ColorID = dbo.Colors.ColorID
WHERE        
   (dbo.Users.UserID = 1)

The above query return one data but I need to show all color names and one extra field like IsSelected which indicate which color user had selected previously. 上面的查询返回一个数据,但是我需要显示所有颜色名称和一个额外的字段,例如IsSelected ,以指示用户先前选择的颜色。 please help me to construct the SQL . 请帮我构造SQL。 Thanks 谢谢

Pretty sure you are looking for something along these lines. 可以肯定,您正在按照这些思路寻找东西。

select c.ColorID
    , c.ColorName
    , IsSelected = case when uc.ColorID is null then 0 else 1 end
from dbo.Colors c
left join dbo.UserColor uc on uc.ColorID = c.ColorID and uc.UserID = 1 --leave this in the join or it becomes an inner join
left join dbo.Users u on u.UserID = uc.UserID

You may try this version with a subquery 您可以通过子查询尝试使用此版本

DECLARE @userID as INT = NULL;

 SELECT uc.ColorID, uc.ColorName, max(uc.IsSelected) 
 FROM
 (
  SELECT 
        c.ColorID
        , c.ColorName
        , uc.UserID
        , IsSelected = case when uc.UserID = @userID then 1 else 0 end
    FROM dbo.Colors c
    LEFT JOIN dbo.UserColor uc on c.ColorID = uc.ColorID 
   ) uc
   WHERE uc.UserID = @userID OR uc.IsSelected = 0 
   GROUP BY uc.ColorID, uc.ColorName

in this way you get always all colors censed in dbo.Colors for the user you want selected based on previous saved choices. 这样,您将始终在dbo中获得所有颜色。要根据先前保存的选择为您选择的用户选择颜色。

You may try its working on rextester 您可以尝试在雷斯特上工作

Are you sure that you want a user to have many colors? 您确定要让用户使用多种颜色吗? Your structure allows a user to have that. 您的结构允许用户拥有该结构。 You could simplify by moving ColorId to User table and skip UserColor table... 您可以通过将ColorId移到User表并跳过UserColor表来简化...

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

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