如何从 SQL Server 中的 SELECT 进行更新?
在SQL Server中,可以使用INSERT..SELECT
语句将行插入到表中:
INSERT INTO Table (col1, col2, col3)
SELECT col1, col2, col3
FROM other_table
WHERE sql = 'cool'
是否也可以更新表与选择
?我有一个包含这些值的临时表,并且想使用这些值更新另一个表。也许是这样的:
UPDATE Table SET col1, col2
SELECT col1, col2
FROM other_table
WHERE sql = 'cool'
WHERE Table.id = other_table.id
In SQL Server, it is possible to insert rows into a table with an INSERT.. SELECT
statement:
INSERT INTO Table (col1, col2, col3)
SELECT col1, col2, col3
FROM other_table
WHERE sql = 'cool'
Is it also possible to update a table with SELECT
? I have a temporary table containing the values and would like to update another table using those values. Perhaps something like this:
UPDATE Table SET col1, col2
SELECT col1, col2
FROM other_table
WHERE sql = 'cool'
WHERE Table.id = other_table.id
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
在 SQL Server 2008(或更高版本)中,使用
MERGE
< /a>或者:
In SQL Server 2008 (or newer), use
MERGE
Alternatively:
我会修改 Robin 的出色答案< /a> 如下:
如果没有 WHERE 子句,您甚至会影响不需要受影响的行,这可能(可能)导致索引重新计算或触发实际上不应该触发的触发器。
I'd modify Robin's excellent answer to the following:
Without a WHERE clause, you'll affect even rows that don't need to be affected, which could (possibly) cause index recalculation or fire triggers that really shouldn't have been fired.
单程
One way
另一种尚未提及的可能性是将 SELECT 语句本身放入 CTE 中,然后更新 CTE。
这样做的好处是,可以很容易地首先单独运行 SELECT 语句来检查结果的完整性,但如果它们在源和中的名称相同,它确实需要您为上面的列命名。目标表。
这也与其他四个答案中显示的专有
UPDATE ... FROM
语法具有相同的限制。如果源表位于一对多联接的多侧,则无法确定哪些可能的匹配联接记录将在Update
中使用(MERGE< /code> 如果尝试多次更新同一行,则通过引发错误来避免)。
Another possibility not mentioned yet is to just chuck the
SELECT
statement itself into a CTE and then update the CTE.This has the benefit that it is easy to run the
SELECT
statement on its own first to sanity check the results, but it does requires you to alias the columns as above if they are named the same in source and target tables.This also has the same limitation as the proprietary
UPDATE ... FROM
syntax shown in four of the other answers. If the source table is on the many side of a one-to-many join then it is undeterministic which of the possible matching joined records will be used in theUpdate
(an issue thatMERGE
avoids by raising an error if there is an attempt to update the same row more than once).作为记录(以及其他像我一样搜索的人),您可以在 MySQL 中执行此操作,如下所示:
For the record (and others searching like I was), you can do it in MySQL like this:
使用别名:
Using alias:
简单的方法是:
The simple way to do it is:
这可能是执行更新的一个小众原因(例如,主要在过程中使用),或者对其他人来说可能是显而易见的,但还应该说明的是,您可以在不使用 join 的情况下执行 update-select 语句(如果您正在更新的表之间没有公共字段)。
This may be a niche reason to perform an update (for example, mainly used in a procedure), or may be obvious to others, but it should also be stated that you can perform an update-select statement without using join (in case the tables you're updating between have no common field).
这是另一个有用的语法:
它通过使用“WHERE EXIST”检查它是否为空。
Here is another useful syntax:
It checks if it is null or not by using "WHERE EXIST".
我添加这个只是为了让您可以看到一种快速的编写方法,以便您可以在更新之前检查将更新的内容。
I add this only so you can see a quick way to write it so that you can check what will be updated before doing the update.
如果您使用 MySQL 而不是 SQL Server,语法为:
If you use MySQL instead of SQL Server, the syntax is:
UPDATE from SELECT with INNER JOIN in SQL Database
由于这篇文章的回复太多,而且投票率最高,我想我也可以在这里提供我的建议。虽然这个问题很有趣,但我在很多论坛网站上都看到过,并使用INNER JOIN做了一个解决方案,并附有截图。
首先,我创建了一个名为 schoolold 的表,并根据其列名插入了一些记录并执行它。
然后我执行SELECT命令来查看插入的记录。
< /a>
然后我创建了一个名为 schoolnew 的新表,并对其执行了类似的上述操作。
< /a>
然后,要查看其中插入的记录,我执行 SELECT 命令。
< /a>
现在,我想对第三行和第四行进行一些更改,为了完成此操作,我使用 INNER JOIN 执行 UPDATE 命令。
< /a>
要查看更改,我执行SELECT 命令。
< /a>
您可以看到如何使用 INNER JOIN 和 UPDATE 语句轻松地将表 schoolold 的第三条和第四条记录替换为表 schoolnew。
UPDATE from SELECT with INNER JOIN in SQL Database
Since there are too many replies of this post, which are most heavily up-voted, I thought I would provide my suggestion here too. Although the question is very interesting, I have seen in many forum sites and made a solution using INNER JOIN with screenshots.
At first, I have created a table named with schoolold and inserted few records with respect to their column names and execute it.
Then I executed SELECT command to view inserted records.
Then I created a new table named with schoolnew and similarly executed above actions on it.
Then, to view inserted records in it, I execute SELECT command.
Now, Here I want to make some changes in third and fourth row, to complete this action, I execute UPDATE command with INNER JOIN.
To view the changes I execute the SELECT command.
You can see how Third and Fourth records of table schoolold easily replaced with table schoolnew by using INNER JOIN with UPDATE statement.
如果您想将表与其自身连接起来(这种情况不会经常发生):
And if you wanted to join the table with itself (which won't happen too often):
通过
CTE
更新比此处的其他答案更具可读性:Updating through
CTE
is more readable than the other answers here:在这里整合所有不同的方法。
示例表结构如下,将从 Product_BAK 更新到 Product 表。
表Product
表Product_BAK
1. 选择更新
2. 使用公共表表达式更新
3. 合并
在这个Merge 语句中,我们可以执行 insert if在目标中找不到匹配的记录,但在源中存在,请查找语法:
Consolidating all the different approaches here.
Sample table structure is below and will update from Product_BAK to Product table.
Table Product
Table Product_BAK
1. Select update
2. Update with a common table expression
3. Merge
In this Merge statement, we can do insert if not finding a matching record in the target, but exist in the source and please find syntax:
以下示例使用派生表(FROM 子句后的 SELECT 语句)来返回旧值和新值以进行进一步更新:
The following example uses a derived table, a SELECT statement after the FROM clause, to return the old and new values for further updates:
如果您使用的是 SQL Server,则可以从一个表更新另一个表,而无需指定联接,只需从
where
子句链接这两个表即可。这使得 SQL 查询更加简单:If you are using SQL Server you can update one table from another without specifying a join and simply link the two from the
where
clause. This makes a much simpler SQL query:另一种方法是使用派生表:
示例数据
The other way is to use a derived table:
Sample data
为了确保您更新的是您想要的内容,请首先选择
To make sure you are updating what you want, select first
甚至还有一种更短的方法,它可能会让您感到惊讶:
示例数据集:
代码:
There is even a shorter method and it might be surprising for you:
Sample data set:
Code:
使用:
EITHER:
OR:
如果两个表中的 ID 列名称相同,则只需将表名称放在要更新的表之前,并为所选表使用别名,即:
Use:
EITHER:
OR:
If the ID column name is the same in both tables then just put the table name before the table to be updated and use an alias for the selected table, i.e.:
在接受的答案中,在:之后
我会添加:
我通常做的是将所有内容放入回滚事务中并使用
“OUTPUT”
:通过这种方式我可以看到即将发生的一切。当我对所看到的内容感到满意时,我将ROLLBACK
更改为COMMIT
。我通常需要记录我所做的事情,因此在运行回滚查询时使用“结果到文本”选项,并保存脚本和输出结果。 (当然,如果我更改了太多行,这是不切实际的)
In the accepted answer, after the:
I would add:
What I usually do is putting everything in a roll backed transaction and using the
"OUTPUT"
: in this way I see everything that is about to happen. When I am happy with what I see, I change theROLLBACK
intoCOMMIT
.I usually need to document what I did, so I use the
"results to Text"
option when I run the roll-backed query and I save both the script and the result of the OUTPUT. (Of course this is not practical if I changed too many rows)以下解决方案适用于 MySQL 数据库:
The below solution works for a MySQL database:
从 select 语句更新的另一种方法:
The other way to update from a select statement:
选项 1:使用 Inner Join:
选项 2:Co 相关子查询
Option 1: Using Inner Join:
Option 2: Co related Sub query
使用 SQL Server 中另一个表中的数据更新一个表时 UPDATE 语句的语法。
The syntax for the UPDATE statement when updating one table with data from another table in SQL Server.
与其他人一样,需要指出的是 MySQL 或 MariaDB 使用不同的语法。它还支持非常方便的 USING 语法(与 T/SQL 相比)。 INNER JOIN 也是 JOIN 的同义词。因此,原始问题中的查询最好在 MySQL 中实现:
我在其他答案中没有看到所问问题的解决方案,因此我的两分钱。
(在 PHP 7.4.0 MariaDB 10.4.10 上测试)
It is important to point out, as others have, that MySQL or MariaDB use a different syntax. Also it supports a very convenient USING syntax (in contrast to T/SQL). Also INNER JOIN is synonymous with JOIN. Therefore the query in the original question would be best implemented in MySQL thusly:
I've not seen the a solution to the asked question in the other answers, hence my two cents.
(tested on PHP 7.4.0 MariaDB 10.4.10)