LEFT JOIN在两个或多个表上

时间:2017-06-29 07:41:37

标签: sql sql-server

使用默认值或自定义用户值返回产品列表的最佳方法是什么?例如:

产品

product_id   name              category
---------------------------------------
PROD1        SM-W620NZKBDBT    2 
SOFT1        MS-UIOA2189090    2
SOFT2        LE-UIOSAI91000    2

设置(默认设置,任何用户都可以使用 - user_id是nulll)

product_id   title           color    user_id
----------------------------------------------
PROD1        Samsung         black    NULL
SOFT1        MS Office-365   NULL     NULL

设置(用户特定设置,基于默认设置构建)

product_id   title           color    user_id
----------------------------------------------
PROD1        Samsung/WIFI    dark     123
SOFT3        MS Windows 10   N/A      123 

*如果可能,*用户设置应位于同一个表中

user_id = 123和category = 2

的用户的产品列表的预期输出
product_id   title           color     
-------------------------------------- 
PROD1        Samsung/WIFI    dark      <- product with user settings
SOFT1        MS Office-365   NULL      <- product with default settings
SOFT2        LE-UIOSAI91000  NULL      <- product without settings

产品列表和设置的项目非常少且用途有限。

3 个答案:

答案 0 :(得分:1)

SELECT TOP (1) WITH TIES
    *
FROM products       P
LEFT JOIN settings  S ON S.product_id = P.product_id AND (S.user_id = @user_id OR S.user_id IS NULL)
ORDER BY ROW_NUMBER() OVER(PARTITION BY P.product_id ORDER BY S.user_id DESC)

答案 1 :(得分:0)

CREATE TABLE #Table1
    ([product_id] varchar(5), [name] varchar(14), [category] int)
;

INSERT INTO #Table1
    ([product_id], [name], [category])
VALUES
    ('PROD1', 'SM-W620NZKBDBT', 2),
    ('SOFT1', 'MS-UIOA2189090', 2),
    ('SOFT2', 'LE-UIOSAI91000', 2)
;
CREATE TABLE #Table2
    ([product_id] varchar(5), [title] varchar(13), [color] varchar(5), [user_id] varchar(4))
;

INSERT INTO #Table2
    ([product_id], [title], [color], [user_id])
VALUES
    ('PROD1', 'Samsung', 'black', NULL),
    ('SOFT1', 'MS Office-365', NULL, NULL)

CREATE TABLE #Table3
    ([product_id] varchar(5), [title] varchar(13), [color] varchar(4), [user_id] int)
;

INSERT INTO #Table3
    ([product_id], [title], [color], [user_id])
VALUES
    ('PROD1', 'Samsung/WIFI', 'dark', 123),
    ('SOFT3', 'MS Windows 10', 'N/A', 123)

SELECT 
A.PRODUCT_ID,ISNULL(B.TITLE,A.NAME) TITLE,B.COLOR 
FROM #TABLE1 A  LEFT JOIN 
 #TABLE3 B ON A.PRODUCT_ID=B.PRODUCT_ID  
 AND USER_ID = 123
ORDER BY  user_id DESC

答案 2 :(得分:0)

试试这个:

Declare @Products Table (product_id varchar(25) , name varchar(50) , category int);
Declare @Settings Table (product_id varchar(25) , title varchar(50) , color varchar(25) , user_id int);

Insert into @Products values
('PROD1' , 'SM-W620NZKBDBT' , 2) , 
('SOFT1' , 'MS-UIOA2189090' , 2) ,
('SOFT2' , 'LE-UIOSAI91000' , 2) ;

Insert into @Settings values
('PROD1' , 'Samsung' , 'black' , Null),
('SOFT1' , 'MS Office-365' , Null , Null),
('PROD1' , 'Samsung/WIFI' , 'Dark' , 123),
('SOFT3' , 'MS Windows 10' , 'N/A' , 123);


SELECT A.product_id product_id ,
       coalesce(A.Name,B.title) Tile,
       B.Color
From @Products A Left join @Settings B ON A.product_id = B.product_id and USER_ID = 123;

Demo