如何根据另一张表中另一列的值更新一列

时间:2019-03-29 09:26:56

标签: sql sql-server tsql sql-update

我有一个table A,其中有2个主要列:NameSettingsSettings列将具有3个值:1、2或3。

例如:

Name     Settings
Andre        1
Andre        1
Betty        3
Charles      1
Charles      1

请注意,1个Name只能有1个Settings:1、2或3

我还有另一个table B,其中有两列:NameTypeType列包含许多值:'TypeA''TypeB''TypeC' ......

例如:

Name       Type
Andre      TypeA
Andre      TypeA
Andre      TypeC
Betty      TypeB
Betty      TypeB
Charles    TypeB
Charles    TypeA
Charles    TypeA

请注意,1个Name可以有多个相同或不同的Type。我们只考虑TypeATypeB

现在,我想使用Settings中的table A列来更新'Type'中的table B列,如果:

  • Type中的table B列的值为'TypeA',然后将两个表中具有相同Name的所有记录设置为1 Settings中的table A列。
  • Type中的table B列的值为'TypeB',然后将两个表中具有相同Name的所有记录设置为2 Settings中的table A列。
  • Type中的table B列同时具有'TypeA''TypeB'的值,然后设置所有具有相同{{1} },从NameSettings列的3。

table A的结果将如下所示:

table A

我该怎么办? 到目前为止,我已经尝试过:

Name     Settings
Andre        1
Andre        1
Andre        1
Betty        2
Charles      3
Charles      3

4 个答案:

答案 0 :(得分:2)

解决此问题的不同方法。我将添加一些列以使生活更轻松。之后,更新变得无关紧要:

USE TEMPDB

CREATE TABLE A (Names VARCHAR (20), Sorts VARCHAR (20), A INT, B INT, AB INT)

INSERT INTO A VALUES 
('Andre'   ,   'TypeA' ,0 ,0, 0),
('Andre'   ,   'TypeA' ,0 ,0, 0),
('Andre'   ,   'TypeC' ,0 ,0, 0),
('Betty'   ,   'TypeB' ,0 ,0, 0),
('Betty'   ,   'TypeB' ,0 ,0, 0),
('Charles' ,   'TypeB' ,0 ,0, 0),
('Charles' ,   'TypeA' ,0 ,0, 0),
('Charles' ,   'TypeA' ,0 ,0, 0) 

UPDATE A
SET A = 1 
WHERE Sorts = 'TypeA'
  AND NOT EXISTS ( SELECT * FROM A AS A2
                WHERE A.Names = A2.Names
                  AND A2.Sorts = 'TypeB' )

UPDATE A
SET B = 1 
WHERE Sorts = 'TypeB'
  AND NOT EXISTS ( SELECT * FROM A AS A2
                WHERE A.Names = A2.Names
                  AND A2.Sorts = 'TypeA' )

UPDATE A
SET AB = 1 
WHERE Sorts = 'TypeA'
  AND  EXISTS ( SELECT * FROM A AS A2
             WHERE A.Names = A2.Names
               AND A2.Sorts = 'TypeB' )

SELECT * FROM A

-编辑:另一个选择是使用CTE,因为OP不能更改或创建表。同样的事情,添加额外的功能后,您可以使用CTE进行更新。

CREATE TABLE D (Names VARCHAR (20), Sorts VARCHAR (20))
INSERT INTO D VALUES 
    ('Andre'   ,   'TypeA'),
    ('Andre'   ,   'TypeA'),
    ('Andre'   ,   'TypeC'),
    ('Betty'   ,   'TypeB'),
    ('Betty'   ,   'TypeB'),
    ('Charles' ,   'TypeB'),
    ('Charles' ,   'TypeA'),
    ('Charles' ,   'TypeA'); 

WITH CTE AS 

(
    SELECT *,
         CASE WHEN Sorts = 'TypeA' AND NOT EXISTS ( SELECT * FROM D AS D2
                                                WHERE D.Names = D2.Names
                                                  AND D2.Sorts = 'TypeB' ) THEN 1 ELSE 0 END AS A,
          CASE WHEN Sorts = 'TypeB' AND NOT EXISTS ( SELECT * FROM D AS D2
                                                 WHERE D.Names = D2.Names
                                                   AND D2.Sorts = 'TypeA' ) THEN 1 ELSE 0 END AS B,
          CASE WHEN Sorts = 'TypeB' AND EXISTS ( SELECT * FROM D AS D2
                                             WHERE D.Names = D2.Names
                                               AND D2.Sorts = 'TypeA' ) THEN 1 ELSE 0 END AS AB
    FROM D
)

SELECT * FROM CTE 

答案 1 :(得分:1)

这是我的方法!我已将TableA更新为三种不同的类型。

请参见以下代码:

UPDATE TableA SET TableA.Settings = 1
WHERE Exists(
  SELECT 1 FROM TableB B
  WHERE TableA.Name = B.Name
  AND B.Type = 'TypeA'
)

UPDATE TableA SET TableA.Settings = 2
WHERE Exists(
  SELECT 1 FROM TableB B
  WHERE TableA.Name = B.Name
  AND B.Type = 'TypeB'
)

UPDATE TableA SET TableA.Settings = 3
WHERE Exists(
  SELECT 1 FROM TableB B1
  INNER JOIN TableB B2
  ON(B1.Name = B2.Name AND B1.Type != B2.Type)
  WHERE TableA.Name = B1.Name
  AND ((B1.Type = 'TypeA' AND B2.Type = 'TypeB')
    OR (B1.Type = 'TypeB' AND B2.Type = 'TypeA'))
)

Here is the working demo!

希望,这会对您有所帮助。

答案 2 :(得分:0)

from collections.abc import Iterable


class Choices(Iterable, metaclass=MetaChoices):
    pass

答案 3 :(得分:0)

尝试一下

  

从(SELECT'Andre'AS名称,1 AS中选择​​进入#table1   设置UNION ALL SELECT'Andre',1 UNION ALL SELECT   '贝蒂',3 UNION ALL SELECT'查尔斯',1 UNION ALL SELECT   '查尔斯',1)XX

     

从(SELECT'Andre'AS名称,'TypeA'AS   类型UNION SELECT'Andre','TypeA'UNION SELECT'Andre','TypeC'   UNION SELECT'Betty','TypeB'UNION SELECT'Betty','TypeB'UNION   SELECT'Charles','TypeB'UNION SELECT'Charles','TypeA'UNION   SELECT'Charles','TypeA')YY

     

更新XX SET XX.Setting = CASE                         存在时(
                                        SELECT
                                            1个                                         从                                             #table2 YY                                         哪里                                             YY。名称= XX。名称                                             AND YY.Type ='TypeA'

                                  EXCEPT 

                                  SELECT  
                                      1
                                  FROM
                                      #table2 YY
                                  WHERE 
                                      YY.Name = XX.Name
                                      AND YY.Type = 'TypeB'
                                  ) THEN 1
                  WHEN EXISTS(            
                                  SELECT  
                                      1
                                  FROM
                                      #table2 YY
                                  WHERE 
                                      YY.Name = XX.Name
                                      AND YY.Type = 'TypeB'

                                  EXCEPT 

                                  SELECT  
                                      1
                                  FROM
                                      #table2 YY
                                  WHERE 
                                      YY.Name = XX.Name
                                      AND YY.Type = 'TypeA'
                                  ) THEN 2
                  WHEN EXISTS(            
                                  SELECT  
                                      1
                                  FROM
                                      #table2 YY
                                  WHERE 
                                      YY.Name = XX.Name
                                      AND YY.Type = 'TypeA'

                                  INTERSECT 

                                  SELECT  
                                      1
                                  FROM
                                      #table2 YY
                                  WHERE 
                                      YY.Name = XX.Name
                                      AND YY.Type = 'TypeB'
                                  ) THEN 3
              END                                                       FROM  #table1 XX