如何从SQL Server中的选择中更新?
在 sql Server 中,可以将行插入带有insert的表中。选择
语句:
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)
我会修改 Robin的出色答案< /a>到以下:
如果没有某个条款,您将影响不需要受影响的行,这可能(可能)引起索引重新计算或消防触发器确实不应该发射。
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
尚未提及的另一种可能性是将
选择
语句本身将其自身插入CTE,然后更新CTE。这具有一个好处,即可以轻松地自行运行
选择
语句,请先检查结果,但是如果在源和目标表。这也具有与其他四个答案中所示的语法的专有
更新相同的限制。如果源表位于一对多加入的许多方面,则不确定哪个可能的匹配加入记录将在
update update
中使用(MERGE
的问题/代码>如果尝试多次更新同一行,则通过提出错误来避免)。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的情况下执行更新选择语句(如果您在没有共同字段之间更新的表。
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).
这是另一种有用的语法:
它通过使用“存在的地方”检查是否为null进行检查。
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:
从SQL数据库中的Inner Join进行SELECT的更新,
因为这篇文章的答复太多了,这些帖子是最重大投票的,我认为我也会在这里提供建议。尽管这个问题非常有趣,但我已经在许多论坛网站上看到过,并使用内在加入和屏幕截图制定了解决方案。
首先,我创建了一个名为 schoolold 的表,并插入了几个关于其列名称并执行的记录。
然后,我执行选择命令以查看插入的记录。
然后,我创建了一个名为 schoolnew 的新表,并在上面执行了上述操作。
然后,要查看其中的插入记录,我执行选择命令。
现在,我想在第三行和第四行中进行一些更改,要完成此操作,我执行 update 命令内联机。
要查看更改,我执行 select 命令。
您可以看到表格 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):
在这里巩固所有不同的方法。
样本表结构如下,将从 product_bak 到 product 表更新。
表 product
表 product_bak
1。选择“更新
2”。使用公共表表达式
3。
在此 merge 语句中合并,我们可以插入如果找不到目标中的匹配记录,而是存在于源中,请找到语法:
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:
通过
CTE
更新比其他答案更可读:Updating through
CTE
is more readable than the other answers here:下面的示例使用派生表,从子句之后的选择语句返回旧值和新值以进行进一步更新:
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,则可以从另一个表中更新一个表,而无需指定JOIN,只需从 Whews 子句链接两个表即可。这使得更简单的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:
使用:
或
:
如果两个表中的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.:
”中
在
“ https://stackoverflow.com/a/2334741/1501497
“输出”
:通过这种方式,我看到了即将发生的一切。当我对所看到的东西感到满意时,我将回滚
将其更改为commit
。我通常需要记录我所做的工作,因此在运行滚动式查询时,我使用
“ to text”
选项,并且同时保存脚本和输出的结果。 (当然,如果我更换了太多行,这是不切实际的)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:
从选择语句更新的另一种方法:
The other way to update from a select statement:
选项1:使用内部加入:
选项2:相关子查询
Option 1: Using Inner Join:
Option 2: Co related Sub query
在更新一个表格中,从SQL Server中的另一个表更新一个表格时,该语句的语法。
The syntax for the UPDATE statement when updating one table with data from another table in SQL Server.
重要的是要指出,就像其他人一样, mysql 或://en.wikipedia.org/wiki/mariadb“ rel =” nofollow noreferrer“> mariadb 使用其他语法。此外,它也支持使用语法非常方便的(与T/SQL相反)。内在联接也是加入的代名词。因此,原始问题中最好的查询将在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)
在SQL Server 2008(或更新)中,使用 merge << /a>
替代:
In SQL Server 2008 (or newer), use
MERGE
Alternatively: