我需要在SQL Server中使用其“父”表中的数据更新此表,如下所示:
表:销售
id (int)
udid (int)
assid (int)
表:ud
id (int)
assid (int)
sale.assid包含更新ud.assid的正确值。
什么查询将执行此操作?我在考虑加入,但我不确定是否可能。
我需要在SQL Server中使用其“父”表中的数据更新此表,如下所示:
表:销售
id (int)
udid (int)
assid (int)
表:ud
id (int)
assid (int)
sale.assid包含更新ud.assid的正确值。
什么查询将执行此操作?我在考虑加入,但我不确定是否可能。
当前回答
这在SQL Server中应该有效:
update ud
set assid = sale.assid
from sale
where sale.udid = id
其他回答
PostgreSQL:
CREATE TABLE ud (id integer, assid integer);
CREATE TABLE sales (id integer, udid integer, assid integer);
UPDATE ud
SET assid = sales.assid
FROM sales
WHERE sales.id = ud.id;
Teradata Aster提供了实现目标的另一种有趣方式:
MERGE INTO ud --what table should be updated
USING sale -- from what table/relation update info should be taken
ON ud.id = sale.udid --join condition
WHEN MATCHED THEN
UPDATE SET ud.assid = sale.assid; -- how to update
后期的,后期的
UPDATE table1
SET COLUMN = value
FROM table2,
table3
WHERE table1.column_id = table2.id
AND table1.column_id = table3.id
AND table1.COLUMN = value
AND table2.COLUMN = value
AND table3.COLUMN = value
SQL不是真正可移植的另一个例子。
对于MySQL,应该是:
update ud, sale
set ud.assid = sale.assid
where sale.udid = ud.id;
有关更多信息,请阅读多表更新:http://dev.mysql.com/doc/refman/5.0/en/update.html
UPDATE [LOW_PRIORITY] [IGNORE] table_references
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
在MS ACCESS中:
UPDATE ud
INNER JOIN sale ON ud.id = sale.udid
SET ud.assid = sale.assid;