SQL update query using joins

后端 未结 11 1821
天涯浪人
天涯浪人 2020-11-22 01:15

I have to update a field with a value which is returned by a join of 3 tables.

Example:

select
    im.itemid
    ,im.sku as iSku
    ,gm.SKU as GSKU         


        
11条回答
  •  [愿得一人]
    2020-11-22 01:54

    One of the easiest way is to use a common table expression (since you're already on SQL 2005):

    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
        , 
    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 = 
    

    The query execution engine will figure out on its own how to update the record.

提交回复
热议问题