SQL Update查询产品购物车

时间:2012-09-12 02:41:42

标签: sql sql-server

ID  ProductId   MemberId    SessionId      CreatedOn    Quantity
62  1           2                          2012-09-11   1
63  2           2                          2012-09-11   1
64  1           0   agzdeoqubwokfme05wwmne55   2012-09-11   1
65  3           0   agzdeoqubwokfme05wwmne55   2012-09-11   1

我有上表,我需要制作一个程序。 我的方案是没有登录添加项目到购物车和登录时合并所有项目。

我将在过程@MemberId(登录会员ID),@ SessionID

中传递两个参数

我想要把表放在下面。

ID  ProductId   MemberId    SessionId      CreatedOn    Quantity
62  1           2                          2012-09-11   2
63  2           2                          2012-09-11   1
65  3           2                              2012-09-11   1

一个产品与另一个产品合并,产生数量2.那一行应该消失。

3 个答案:

答案 0 :(得分:2)

假设SQL SERVER 2005或更高版本

CREATE TABLE #T (ID INT , ProductId INT,  MemberId INT,   SessionId VARCHAR(100),  CreatedOn DATETIME,   Quantity INT)
INSERT INTO #T SELECT 62,1 ,2 ,' ','2012-09-11',1
INSERT INTO #T SELECT 63,  2,2, ' ','2012-09-11',1
INSERT INTO #T SELECT 64,  1,0,'agzdeoqubwokfme05wwmne55','2012-09-11',1
INSERT INTO #T SELECT 65,  3,0,'agzdeoqubwokfme05wwmne55','2012-09-11',1

;With CTE AS 
(SELECT 
    ID
    ,ProductId
    ,MemberId = MAX(MemberId) OVER(PARTITION BY ProductID ORDER bY(SELECT 1))
    ,SessionID
    ,CreatedOn = CONVERT(VARCHAR(10),MAX(CreatedOn) OVER(PARTITION BY ProductID ORDER bY(SELECT 1)),20)
    ,Quantity = SUM(Quantity) OVER(PARTITION BY ProductID ORDER bY(SELECT 1))
    ,Rn = ROW_NUMBER() OVER(PARTITION BY ProductID ORDER bY(SELECT 1))
FROM #T)

SELECT ID,ProductId,MemberId,SessionID,CreatedOn,Quantity FROM CTE WHERE Rn = 1

DROP TABLE #T

将结果显示为

ID  ProductId   MemberId    SessionID              CreatedOn    Quantity
62  1            2                                  2012-09-11    2
63  2            2                                  2012-09-11    1
65  3            0          agzdeoqubwokfme05wwmne55 2012-09-11   1

最后,如果您想更新表格,请点击此处

UPDATE #T
SET 
    #T.ID = c.ID
    ,#T.ProductId =c.ProductId
    ,#T.MemberId = c.MemberId
    ,#T.SessionID = c.SessionID
    ,#T.CreatedOn = c.CreatedOn
    ,#T.Quantity = c.Quantity
FROM #T
INNER JOIN CTE c
ON #T.ID = c.ID
WHERE c.Rn = 1

结果是

ID  ProductId   MemberId    SessionId               CreatedOn   Quantity
62  1              2                                 2012-09-11   2
63  2              2                                 2012-09-11   1
64  1              0    agzdeoqubwokfme05wwmne5      2012-09-11   1
65  3              0    agzdeoqubwokfme05wwmne55     2012-09-11   1

完整的查询

;With CTE AS 
(SELECT 
    ID
    ,ProductId
    ,MemberId = MAX(MemberId) OVER(PARTITION BY ProductID ORDER bY(SELECT 1))
    ,SessionID
    ,CreatedOn = CONVERT(VARCHAR(10),MAX(CreatedOn) OVER(PARTITION BY ProductID ORDER bY(SELECT 1)),20)
    ,Quantity = SUM(Quantity) OVER(PARTITION BY ProductID ORDER bY(SELECT 1))
    ,Rn = ROW_NUMBER() OVER(PARTITION BY ProductID ORDER bY(SELECT 1))
FROM #T)

UPDATE #T
SET 
    #T.ID = c.ID
    ,#T.ProductId =c.ProductId
    ,#T.MemberId = c.MemberId
    ,#T.SessionID = c.SessionID
    ,#T.CreatedOn = CONVERT(VARCHAR(10),c.CreatedOn,20)
    ,#T.Quantity = c.Quantity
FROM #T
INNER JOIN CTE c
ON #T.ID = c.ID
WHERE c.Rn = 1

SELECT * FROM #T

DROP TABLE #T

适用于任何版本的SQL

UPDATE #T
SET 
    #T.ID = c.ID
    ,#T.ProductId =c.ProductId
    ,#T.MemberId = c.MemberId
    ,#T.SessionID = c.SessionID
    ,#T.CreatedOn = c.CreatedOn
    ,#T.Quantity = c.Quantity
FROM #T
INNER JOIN (SELECT t.ID,t.ProductID,t.MemberId,t.SessionId,t.CreatedOn,x.Quantity
            FROM #T t 
            JOIN (
                    SELECT
                            ID = MIN(ID)
                            ,ProductID 
                            ,MemberId = MAX(MemberId)       
                            ,CreatedOn = MAX(CreatedOn)
                            ,Quantity = SUM(Quantity) 
                    FROM #T 
                    GROUP BY ProductID
                    )X
            ON t.ID =X.ID) c
ON #T.ID = c.ID


SELECT * FROM #T
DROP TABLE #T

答案 1 :(得分:1)

SELECT ProductID, 
       max(MemberId), 
       max(CreatedOn),
       sum(Quantity) 
FROM theTable 
WHERE SessionID=<id> or MemberID=<id>
GROUP BY ProductID

答案 2 :(得分:1)

更新可能不是最佳解决方案,删除所有记录并插入新记录可能更好。

无论如何,如果你想更新然后删除额外的那些,你需要做这样的事情。

假设ID是此处的唯一键。这是完整的逻辑,请仔细阅读

DECLARE @Temp1 TABLE 
(
--datafields same AS your table

)

DECLARE @Temp2 TABLE 
(
--datafields same AS your table
)

INSERT INTO @Temp1
SELECT ID,
       ProductID, 
       MemberId,
       SessionId, 
       CreatedOn,
       Quantity 
FROM theTable 
WHERE SessionID= @SessionID  or MemberID= @MemberID 

UPDATE a
SET SessionID =  @SessionID  , MemberID= @MemberID 
FROM @Temp1 AS a
--WHERE  SessionID= @SessionID  OR MemberID= @MemberID 

INSERT INTO @Temp2
SELECT MAX(ID),
       ProductID, 
       max(MemberId),
       MAX(SessionId),
       max(CreatedOn),
       sum(Quantity) 
FROM @Temp1
GROUP BY ProductID 

UPDATE a
SET a.MemberId = b.MemberId ,a.SessionId=b.SessionId ,a.CreatedOn=b.CreatedOn,a.Quantity=b.Quantity 
FROM YourTable AS a
INNER JOIN @Temp2 AS b
ON a.ID=b.ID      

DELETE  FROM YourTable
WHERE   ID IN ( SELECT  ID
                    FROM    @Temp1 AS ta
                    WHERE   NOT EXISTS ( SELECT 1
                                         FROM   @Temp2 AS tb
                                         WHERE  ta.ID = tb.ID ) )