如何减去不同表中存在的sql中的两列

时间:2011-06-25 18:02:37

标签: asp.net sql

Update TotalItems 
set TotalItems.No_Items_Present = TotalItems.Total_Items - ItemsTable.No_Of_Items 
where TotalItems.Item_Name = ItemsTable.Item_Name

我的查询无效。任何解决方案????

4 个答案:

答案 0 :(得分:0)

试试这个:

UPDATE a SET a.No_Items_Present = a.Total_Items - b.No_Of_Items
FROM TotalItems a INNER JOIN ItemsTable b ON a.Item_Name = b.Item_Name

答案 1 :(得分:0)

在MS SQL中:

update ttl 
set No_Items_Present = ttl.Total_Items - itm.No_Of_Items 
from TotalItems ttl
    join ItemsTable itm on
        ttl.Item_Name = itm. Item_Name

在MySQL中:

update TotalItems ttl
    join ItemsTable itm on
        ttl.Item_Name = itm. Item_Name
set tt1.No_Items_Present = ttl.Total_Items - itm.No_Of_Items 

答案 2 :(得分:0)

update(
select ti.no_items_present,
       ti.total_items,
       it.no_of_items
   from total_items ti inner join
       itemstable it on ti.item_name=it.item_name)
set no_items_present=total_items-no_of_items

答案 3 :(得分:0)

在MySQL中你会做这样的事情:

UPDATE TotalItems t, ItemsTable i
SET t.No_Items_Present = t.Total_Items - i.No_Of_Items
WHERE t.Item_Name = i.Item_Name