关于sql server:使用连接的SQL更新查询

SQL update query using joins

我必须用3个表的联接返回的值更新一个字段。

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SELECT
    im.itemid
    ,im.sku AS iSku
    ,gm.SKU AS GSKU
    ,mm.ManufacturerId AS ManuId
    ,mm.ManufacturerName
    ,im.mf_item_number
    ,mm.ManufacturerID
FROM
    item_master im, group_master gm, Manufacturer_Master mm
WHERE
    im.mf_item_number LIKE 'STA%'
    AND im.sku=gm.sku
    AND gm.ManufacturerID = mm.ManufacturerID
    AND gm.manufacturerID=34

我想用在上述条件中加入的其他值更新表item_mastermf_item_number字段值。

如何在MS SQL Server中执行此操作?


1
2
3
4
5
6
7
8
9
UPDATE im
SET mf_item_number = gm.SKU --etc
FROM item_master im
JOIN group_master gm
    ON im.sku = gm.sku
JOIN Manufacturer_Master mm
    ON gm.ManufacturerID = mm.ManufacturerID
WHERE im.mf_item_number LIKE 'STA%' AND
      gm.manufacturerID = 34

为了澄清…UPDATE子句可以引用FROM子句中指定的表别名。所以在这种情况下,im是有效的。

一般实例

1
2
3
4
5
6
UPDATE A
SET foo = B.bar
FROM TableA A
JOIN TableB B
    ON A.col1 = B.colx
WHERE ...


最简单的方法之一是使用公共表表达式(因为您已经使用了SQL 2005):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
WITH cte AS (
SELECT
    im.itemid
    ,im.sku AS iSku
    ,gm.SKU AS GSKU
    ,mm.ManufacturerId AS ManuId
    ,mm.ManufacturerName
    ,im.mf_item_number
    ,mm.ManufacturerID
    , <your other field>
FROM
    item_master im, group_master gm, Manufacturer_Master mm
WHERE
    im.mf_item_number LIKE 'STA%'
    AND im.sku=gm.sku
    AND gm.ManufacturerID = mm.ManufacturerID
    AND gm.manufacturerID=34)
UPDATE cte SET mf_item_number = <your other field>

查询执行引擎将自行决定如何更新记录。


适应mysql——在UPDATE中没有FROM子句,但是这样可以:

1
2
3
4
5
6
7
8
9
10
11
12
UPDATE
    item_master im
    JOIN
    group_master gm ON im.sku=gm.sku
    JOIN
    Manufacturer_Master mm ON gm.ManufacturerID=mm.ManufacturerID
SET
    im.mf_item_number = gm.SKU --etc
WHERE
    im.mf_item_number LIKE 'STA%'
    AND
    gm.manufacturerID=34


没有使用上面的SQL,但下面是一个基于join语句更新表的示例。

1
2
3
4
5
6
7
8
UPDATE p
SET    p.category = c.category
FROM   products p
       INNER JOIN prodductcatagories pg
            ON  p.productid = pg.productid
       INNER JOIN categories c
            ON  pg.categoryid = c.cateogryid
WHERE  c.categories LIKE 'whole%'

您可以指定其他表,用于确定在UPDATE语句中使用"FROM"子句更新的方式和内容,如下所示:

1
2
3
4
5
6
7
UPDATE item_master
SET mf_item_number = (SOME VALUE)
FROM
   group_master AS gm
   JOIN Manufacturar_Master AS mm ON ........
WHERE
 .... (your conditions here)

在WHERE子句中,需要提供将这些表绑定在一起的条件和联接操作。

马克


1
2
3
4
5
6
7
8
MySQL: IN general, make necessary changes par your requirement:

UPDATE
    shopping_cart sc
    LEFT JOIN
    package pc ON sc. package_id = pc.id    
SET
    sc. amount = pc.amount


试试这个…

1
2
3
4
UPDATE t1.Column1 = VALUE
FROM tbltemp AS t1
INNER JOIN tblUser AS t2 ON t2.ID = t1.UserID
WHERE t1.[column1]=VALUE AND t2.[Column1] = VALUE;

可以使用以下查询:

1
2
3
4
5
6
7
8
9
UPDATE im
SET mf_item_number = (SOME VALUE)
FROM item_master im
JOIN group_master gm
    ON im.sku = gm.sku
JOIN Manufacturer_Master mm
    ON gm.ManufacturerID = mm.ManufacturerID
WHERE im.mf_item_number LIKE 'STA%' AND
      gm.manufacturerID = 34    `sql`

您可以使用MERGE命令进行更新,对MATCHEDNOT MATCHED有更多的控制:(我稍微更改了源代码以证明我的观点)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
USE tempdb;
GO
IF(OBJECT_ID('target') > 0)DROP TABLE dbo.target
IF(OBJECT_ID('source') > 0)DROP TABLE dbo.source
CREATE TABLE dbo.Target
    (
      EmployeeID INT ,
      EmployeeName VARCHAR(100) ,
      CONSTRAINT Target_PK PRIMARY KEY ( EmployeeID )
    );
CREATE TABLE dbo.Source
    (
      EmployeeID INT ,
      EmployeeName VARCHAR(100) ,
      CONSTRAINT Source_PK PRIMARY KEY ( EmployeeID )
    );
GO
INSERT  dbo.Target
        ( EmployeeID, EmployeeName )
VALUES  ( 100, 'Mary' );
INSERT  dbo.Target
        ( EmployeeID, EmployeeName )
VALUES  ( 101, 'Sara' );
INSERT  dbo.Target
        ( EmployeeID, EmployeeName )
VALUES  ( 102, 'Stefano' );

GO
INSERT  dbo.Source
        ( EmployeeID, EmployeeName )
VALUES  ( 100, 'Bob' );
INSERT  dbo.Source
        ( EmployeeID, EmployeeName )
VALUES  ( 104, 'Steve' );
GO

SELECT * FROM dbo.Source
SELECT * FROM dbo.Target

MERGE Target AS T
USING SOURCE AS S
ON ( T.EmployeeID = S.EmployeeID )
WHEN MATCHED THEN
    UPDATE SET T.EmployeeName = S.EmployeeName + '[Updated]';
GO
SELECT '-------After Merge----------'
SELECT * FROM dbo.Source
SELECT * FROM dbo.Target