如何从SQL Server中的SELECT更新?
在SQL Server中 ,可以使用SELECT
语句insert
表中:
INSERT INTO Table (col1, col2, col3)
SELECT col1, col2, col3
FROM other_table
WHERE sql = 'cool'
是否还可以通过SELECT
更新 ? 我有一个包含这些值的临时表,并想使用这些值更新另一个表。 也许是这样的:
UPDATE Table SET col1, col2
SELECT col1, col2
FROM other_table
WHERE sql = 'cool'
WHERE Table.id = other_table.id
#1楼
使用别名:
UPDATE tSET t.col1 = o.col1FROM table1 AS tINNER JOIN table2 AS o ON t.id = o.id
#2楼
这可能是执行更新的利基原因(例如,主要在过程中使用),或者可能对其他人显而易见,但是还应指出,您可以在不使用join的情况下执行update-select语句(以防您要更新的表格之间没有公共字段)。
updateTable
setTable.example = a.value
fromTableExample a
whereTable.field = *key value* -- finds the row in Table AND a.field = *key value* -- finds the row in TableExample a
#3楼
为了进行记录(以及其他类似的搜索记录),您可以在MySQL中执行以下操作:
UPDATE first_table, second_table
SET first_table.color = second_table.color
WHERE first_table.id = second_table.foreign_id
#4楼
简单的方法是:
UPDATEtable_to_update,table_info
SETtable_to_update.col1 = table_info.col1,table_to_update.col2 = table_info.col2WHEREtable_to_update.ID = table_info.ID
#5楼
这是另一种有用的语法:
UPDATE suppliers
SET supplier_name = (SELECT customers.nameFROM customersWHERE customers.customer_id = suppliers.supplier_id)
WHERE EXISTS (SELECT customers.nameFROM customersWHERE customers.customer_id = suppliers.supplier_id);
它使用“ WHERE EXIST”检查它是否为空。
#6楼
以下示例使用派生表(在FROM子句之后的SELECT语句)返回旧值和新值以进行进一步更新:
UPDATE x
SET x.col1 = x.newCol1,x.col2 = x.newCol2
FROM (SELECT t.col1,t2.col1 AS newCol1,t.col2,t2.col2 AS newCol2FROM [table] tJOIN other_table t2ON t.ID = t2.ID) x
#7楼
如果使用MySQL而不是SQL Server,则语法为:
UPDATE Table1
INNER JOIN Table2
ON Table1.id = Table2.id
SET Table1.col1 = Table2.col1,Table1.col2 = Table2.col2
#8楼
UPDATETable_A
SETTable_A.col1 = Table_B.col1,Table_A.col2 = Table_B.col2
FROMSome_Table AS Table_AINNER JOIN Other_Table AS Table_BON Table_A.id = Table_B.id
WHERETable_A.col3 = 'cool'
#9楼
单程
UPDATE t
SET t.col1 = o.col1, t.col2 = o.col2
FROM other_table o JOIN t ON t.id = o.id
WHERE o.sql = 'cool'
#10楼
采用:
drop table uno
drop table doscreate table uno
(uid int,col1 char(1),col2 char(2)
)
create table dos
(did int,col1 char(1),col2 char(2),[sql] char(4)
)
insert into uno(uid) values (1)
insert into uno(uid) values (2)
insert into dos values (1,'a','b',null)
insert into dos values (2,'c','d','cool')select * from uno
select * from dos
等等:
update uno set col1 = (select col1 from dos where uid = did and [sql]='cool'),
col2 = (select col2 from dos where uid = did and [sql]='cool')
要么:
update uno set col1=d.col1,col2=d.col2 from uno
inner join dos d on uid=did where [sql]='cool'select * from uno
select * from dos
如果两个表中的ID列名称相同,则只需将表名称放在要更新的表之前,并为所选表使用别名,即:
update uno set col1 = (select col1 from dos d where uno.[id] = d.[id] and [sql]='cool'),
col2 = (select col2 from dos d where uno.[id] = d.[id] and [sql]='cool')
#11楼
而且,如果您想将表与自身连接(这种情况不会经常发生):
update t1 -- just reference table alias here
set t1.somevalue = t2.somevalue
from table1 t1 -- these rows will be the targets
inner join table1 t2 -- these rows will be used as source
on .................. -- the join clause is whatever suits you
#12楼
以下解决方案适用于MySQL数据库:
UPDATE table1 a , table2 b
SET a.columname = 'some value'
WHERE b.columnname IS NULL ;
#13楼
UPDATE table AS a
INNER JOIN table2 AS b
ON a.col1 = b.col1
INNER JOIN ... AS ...
ON ... = ...
SET ...
WHERE ...
#14楼
使用SQL数据库中的INNER JOIN从SELECT更新
由于此帖子的回复太多,投票最多,我想在这里也提供我的建议。 尽管这个问题非常有趣,但是我在很多论坛站点上都看到过,并使用带有屏幕截图的INNER JOIN提出了解决方案。
首先,我创建了一个以schoolold命名的表,并针对其列名插入了几条记录并执行了该记录。
然后,我执行了SELECT命令以查看插入的记录。
然后,我创建了一个名为schoolnew的新表,并对该表执行了类似的操作。
然后,要查看其中插入的记录,我执行SELECT命令。
现在,在这里我想在第三行和第四行中进行一些更改,以完成此操作,我使用INNER JOIN执行UPDATE命令。
要查看更改,我执行SELECT命令。
您可以看到如何通过使用带有UPDATE语句的INNER JOIN将表schoolold的第三和第四记录轻松替换为表schoolnew 。
#15楼
通过CTE
更新比这里的其他答案更具可读性:
;WITH cteAS (SELECT col1,col2,idFROM other_tableWHERE sql = 'cool')
UPDATE A
SET A.col1 = B.col1,A.col2 = B.col2
FROM table AINNER JOIN cte BON A.id = B.id
#16楼
另一种方法是使用派生表:
UPDATE t
SET t.col1 = a.col1,t.col2 = a.col2
FROM (
SELECT id, col1, col2 FROM @tbl2) a
INNER JOIN @tbl1 t ON t.id = a.id
样本数据
DECLARE @tbl1 TABLE (id INT, col1 VARCHAR(10), col2 VARCHAR(10))
DECLARE @tbl2 TABLE (id INT, col1 VARCHAR(10), col2 VARCHAR(10))INSERT @tbl1 SELECT 1, 'a', 'b' UNION SELECT 2, 'b', 'c'INSERT @tbl2 SELECT 1, '1', '2' UNION SELECT 2, '3', '4'UPDATE t
SET t.col1 = a.col1,t.col2 = a.col2
FROM (
SELECT id, col1, col2 FROM @tbl2) a
INNER JOIN @tbl1 t ON t.id = a.idSELECT * FROM @tbl1
SELECT * FROM @tbl2
#17楼
从select语句更新的另一种方法:
UPDATE A
SET A.col = A.col,B.col1 = B.col1
FROM first_Table AS A
INNER JOIN second_Table AS B ON A.id = B.id WHERE A.col2 = 'cool'
#18楼
UPDATE TQ
SET TQ.IsProcessed = 1, TQ.TextName = 'bla bla bla'
FROM TableQueue TQ
INNER JOIN TableComment TC ON TC.ID = TQ.TCID
WHERE TQ.IsProcessed = 0
为了确保您要更新的内容,请先选择
SELECT TQ.IsProcessed, 1 AS NewValue1, TQ.TextName, 'bla bla bla' AS NewValue2
FROM TableQueue TQ
INNER JOIN TableComment TC ON TC.ID = TQ.TCID
WHERE TQ.IsProcessed = 0
#19楼
甚至有一个更短的方法 ,这可能会让您感到惊讶:
样本数据集:
CREATE TABLE #SOURCE ([ID] INT, [Desc] VARCHAR(10));
CREATE TABLE #DEST ([ID] INT, [Desc] VARCHAR(10));INSERT INTO #SOURCE VALUES(1,'Desc_1'), (2, 'Desc_2'), (3, 'Desc_3');
INSERT INTO #DEST VALUES(1,'Desc_4'), (2, 'Desc_5'), (3, 'Desc_6');
码:
UPDATE #DEST
SET #DEST.[Desc] = #SOURCE.[Desc]
FROM #SOURCE
WHERE #DEST.[ID] = #SOURCE.[ID];
#20楼
如果使用的是SQL Server,则可以在不指定联接的情况下从另一个表更新一个表,而只需从where
子句链接两个表即可。 这使SQL查询更加简单:
UPDATE Table1
SET Table1.col1 = Table2.col1,Table1.col2 = Table2.col2
FROMTable2
WHERETable1.id = Table2.id
#21楼
在接受的答案中,之后:
SET
Table_A.col1 = Table_B.col1,
Table_A.col2 = Table_B.col2
我会补充:
OUTPUT deleted.*, inserted.*
我通常要做的是将所有内容放入回滚的事务中,并使用"OUTPUT"
:这样,我可以看到将要发生的所有事情。 当我对看到的内容满意时,将ROLLBACK
更改为COMMIT
。
我通常需要记录所做的工作,因此在运行回滚查询时,我会使用"results to Text"
选项,并且保存脚本和OUTPUT的结果。 (如果我更改太多行,这当然是不切实际的)
#22楼
在这里合并所有不同的方法。
- 选择更新
- 用通用表表达式更新
- 合并
示例表结构在下面,并将从Product_BAK更新为Product表。
产品
CREATE TABLE [dbo].[Product]([Id] [int] IDENTITY(1, 1) NOT NULL,[Name] [nvarchar](100) NOT NULL,[Description] [nvarchar](100) NULL
) ON [PRIMARY]
产品_比克
CREATE TABLE [dbo].[Product_BAK]([Id] [int] IDENTITY(1, 1) NOT NULL,[Name] [nvarchar](100) NOT NULL,[Description] [nvarchar](100) NULL) ON [PRIMARY]
1.选择更新
update P1set Name = P2.Namefrom Product P1inner join Product_Bak P2 on p1.id = P2.idwhere p1.id = 2
2.使用通用表表达式进行更新
; With CTE as(select id, name from Product_Bak where id = 2)update Pset Name = P2.namefrom product P inner join CTE P2 on P.id = P2.idwhere P2.id = 2
3.合并
Merge into product P1using Product_Bak P2 on P1.id = P2.idwhen matched thenupdate set p1.[description] = p2.[description], p1.name = P2.Name;
在此Merge语句中,如果未在目标中找到匹配记录但在源中存在匹配记录,则可以进行插入操作,请查找语法:
Merge into product P1using Product_Bak P2 on P1.id = P2.id;when matched thenupdate set p1.[description] = p2.[description], p1.name = P2.Name;WHEN NOT MATCHED THENinsert (name, description)values(p2.name, P2.description);
#23楼
declare @tblStudent table (id int,name varchar(300))
declare @tblMarks table (std_id int,std_name varchar(300),subject varchar(50),marks int)insert into @tblStudent Values (1,'Abdul')
insert into @tblStudent Values(2,'Rahim')insert into @tblMarks Values(1,'','Math',50)
insert into @tblMarks Values(1,'','History',40)
insert into @tblMarks Values(2,'','Math',30)
insert into @tblMarks Values(2,'','history',80)select * from @tblMarksupdate m
set m.std_name=s.namefrom @tblMarks as m
left join @tblStudent as s on s.id=m.std_idselect * from @tblMarks
#24楼
选项1:使用内部联接:
UPDATEA
SETA.col1 = B.col1,A.col2 = B.col2
FROMSome_Table AS AINNER JOIN Other_Table AS BON A.id = B.id
WHEREA.col3 = 'cool'
选项2:与公司相关的子查询
UPDATE table
SET Col1 = B.Col1, Col2 = B.Col2
FROM (SELECT ID, Col1, Col2 FROM other_table) B
WHERE B.ID = table.ID
#25楼
UPDATE table1
SET column1 = (SELECT expression1FROM table2WHERE conditions)
[WHERE conditions];
使用SQL Server中的另一个表中的数据更新一个表时,UPDATE语句的语法
#26楼
我会针对以下内容修改Robin的出色答案 :
UPDATE Table
SET Table.col1 = other_table.col1,Table.col2 = other_table.col2
FROMTable
INNER JOIN other_table ON Table.id = other_table.id
WHERETable.col1 != other_table.col1
OR Table.col2 != other_table.col2
OR (other_table.col1 IS NOT NULLAND Table.col1 IS NULL
)
OR (other_table.col2 IS NOT NULLAND Table.col2 IS NULL
)
没有WHERE子句,您甚至会影响不需要受影响的行,这可能(可能)导致索引重新计算或触发本不应该触发的触发器。
#27楼
我仅添加此内容,以便您可以看到一种快速的编写方式,以便您可以在执行更新之前检查要更新的内容。
UPDATE Table
SET Table.col1 = other_table.col1,Table.col2 = other_table.col2
--select Table.col1, other_table.col,Table.col2,other_table.col2, *
FROM Table
INNER JOIN other_table ON Table.id = other_table.id
#28楼
在SQL Server 2008(或更高版本)中,使用MERGE
MERGE INTO YourTable TUSING other_table S ON T.id = S.idAND S.tsql = 'cool'
WHEN MATCHED THENUPDATE SET col1 = S.col1, col2 = S.col2;
或者:
MERGE INTO YourTable TUSING (SELECT id, col1, col2 FROM other_table WHERE tsql = 'cool') SON T.id = S.id
WHEN MATCHED THENUPDATE SET col1 = S.col1, col2 = S.col2;
#29楼
尚未提及的另一种可能性是将SELECT
语句本身插入CTE中,然后更新CTE。
;WITH CTEAS (SELECT T1.Col1,T2.Col1 AS _Col1,T1.Col2,T2.Col2 AS _Col2FROM T1JOIN T2ON T1.id = T2.id/*Where clause added to exclude rows that are the same in both tablesHandles NULL values correctly*/WHERE EXISTS(SELECT T1.Col1,T1.Col2EXCEPTSELECT T2.Col1,T2.Col2))
UPDATE CTE
SET Col1 = _Col1,Col2 = _Col2
这样做的好处是,很容易首先自己运行SELECT
语句以理清结果,但是,如果源和目标表中的列名相同,则确实需要您对列进行别名别名。
这也与其他四个答案中显示的专有UPDATE ... FROM
语法具有相同的限制。 如果源表位于一对多联接的多个端,则无法确定Update
中将使用哪些可能匹配的联接记录(如果尝试执行以下操作, MERGE
引发错误来避免此问题多次更新同一行)。
#30楼
UPDATE table
SET Col1 = i.Col1, Col2 = i.Col2
FROM (SELECT ID, Col1, Col2 FROM other_table) i
WHERE i.ID = table.ID
如何从SQL Server中的SELECT更新?相关推荐
- 如何从 SQL Server 中的 SELECT 更新?
问题描述: 在 SQL Server 中,可以使用 INSERT- SELECT 语句将行插入到表中: INSERT INTO Table (col1, col2, col3) SELECT col1 ...
- 如何从SQL Server中的SELECT语句更新
In this article, we will learn different methods that are used to update the data in a table with th ...
- 在SQL Server中使用JOIN更新表?
我想更新在其他表上进行联接的表中的列,例如: UPDATE table1 a INNER JOIN table2 b ON a.commonfield = b.[common field] SET a ...
- SQL server中的SELECT查询语句执行顺序
各位大牛们好第一次写博客有点小激动!以后我会把自己的心得分享给大家,求各种评论 SQL server 中SELSECT查询语句的执行顺序如下: (8) SELECT (9) DISTINCT (1 ...
- 如何在SQL Server中的SELECT TOP 中使用变量
在 TOP 中使用变量 以下示例使用变量获得 AdventureWorks 数据库的 dbo.Employee 表中列出的前 10 个雇员. USE AdventureWorks ; GO DECLA ...
- sql server 并发_并发问题– SQL Server中的理论和实验
sql server 并发 介绍 (Introduction) Intended audience 目标听众 This document is intended for application dev ...
- SQL Server中的Datediff移植到Oracle计算有误解决方案
天数的计算 SQL Server中: select datediff(day,'2012-05-02 10:11','2012-05-01 11:11') 结果为1天,只判断日期中的天数,是不根据时间 ...
- SQL Server中SELECT会真的阻塞SELECT吗?
在SQL Server中,我们知道一个SELECT语句执行过程中只会申请一些意向共享锁(IS) 与共享锁(S), 例如我使用SQL Profile跟踪会话86执行SELECT * FROM dbo.T ...
- cte公用表表达式_在SQL Server中使用CTE进行插入和更新(公用表表达式)
cte公用表表达式 In CTEs in SQL Server; Querying Common Table Expressions the first article of this series, ...
最新文章
- ] 解决myeclipse中新建javaweb工程,无法使用Web App Libraries问题
- chrome浏览器模拟手机 地理定位
- 深入Java中文编码乱码问题及最优解决方法
- Source Insight 4.0安装教程(PS:附安装包及卸载重新安装等注意事项)
- Anatomy of a Flink Program(Flink程序的剖析)
- pandas Dataframe读取数据表是自定义列名
- 罗永浩吐槽卖小米、苹果被骂,卖华为也被骂,李楠:警惕键盘侠
- vue-router各个属性的作用及用法
- java excel 列,使用Java读取Excel工作表的单列
- 通过导航栏切换页面动画
- python浮点数转科学计数_python – 将float转换为字符串没有科学记数法和假精度...
- 美洽SDK通过广播结束消息提示
- Android Studio 高版本无法执行Java main方法的问题
- 大数据将走向何方?未来大数据的十大趋势评析
- NYOJ1016:德莱联盟(判线段相交)
- 《论语》原文及其全文翻译 学而篇1
- python-Matplotlib图形上添加箭头指示
- 中国ACM橡胶市场调研与投资预测报告(2022版)
- 哈佛《幸福课》 第4课 积极的环境能改变人
- 计算机研究生论文写作技巧