如何使用2个表的选择并集更新获取结果的表: 可以说我们有:(使用SQL Server)
select something
from table1 join
table2
on ???
where condition1
union
select something
from table1 join
table3
on ???
where condition2
代码
select it.itemid,it.itemgroupid,it.itemtype, it.itemname
from item it
left join part r on r.itemid = it.itemid
left join SALES sl on it.itemid=sl.itemid
where
it.itemtype =1
and r.partnumber =10
and sl.CREATEDDATE between '2010-01-01 00:00:00' and CURRENT_TIMESTAMP
UNION
select distinct it.itemid,it.itemgroupid,it.itemtype, it.itemname
from item it
left join salesdetail sa on sa.itemid=it.itemid
it.itemtype =1
where sa.SALESSTATUS =1
` 如何根据上面的Select查询的结果更新table1中的字段?
如果itemid与上面选择的输出相同的ite ID,我现在想将itemTable字段x更新为1
答案 0 :(得分:0)
请考虑将带有WHERE
的{{1}}子句用于更新。确保使用某些字段的名称进行更新。下面使用CTE作为IN
查询值。除了 itemid
IN
中删除。
SELECT
答案 1 :(得分:0)
我认为您可以尝试以下链接:
`UPDATE item SET item_id =
select it.itemid
from item it
left join part r on r.itemid = it.itemid
left join SALES sl on it.itemid=sl.itemid
where
it.itemtype =1
and r.partnumber =10
and sl.CREATEDDATE between '2010-01-01 00:00:00' and CURRENT_TIMESTAMP
UNION
select distinct it.itemid
from item it
left join salesdetail sa on sa.itemid=it.itemid
it.itemtype =1
where sa.SALESSTATUS =1`