选择查询的 SQL 更新查询

标签 sql sql-server t-sql

我有以下查询来列出两个表的员工。

我需要将 a.staffdiscountstartdate 更新为“20100428”,如何为此重写以下查询?

select 
    a.employeeid,
    b.employeeid 
from 
    tblEmployees a
        left join
    tblCards b 
        on
            a.employeeid=b.employeeid 
where 
    GroupStartDate < '20100301' 
and 
    StaffDiscountStartDate > '20100428' 
and 
    datediff(day,groupstartdate,staffdiscountstartdate)>1 
and
    b.employeeid is null

最佳答案

应该能够做到:

UPDATE a
SET a.staffdiscountstartdate = '20100428'
from tblEmployees a
    left join tblCards b on a.employeeid=b.employeeid 
where GroupStartDate < '20100301' 
and StaffDiscountStartDate > '20100428' 
and datediff(day,groupstartdate,staffdiscountstartdate)>1 
and b.employeeid is null

仅限 MS SQL。其他 SQL 版本不支持此语法。

关于选择查询的 SQL 更新查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2493881/

相关文章:

sql - 如果通过对表的特定索引使用 sp_autostats 将 auto_update_statistics 设置为 ON,是否会统计每个 DML?

.net - SQL Server 和 .NET : insert fails (silently! ) 在代码中,但在手动运行时不是

c# - 端口是从 SQL Server 自动收集的吗?

sql - 如何在 SQL Server 中获取过去两年和当年的记录

asp.net - 我无法使用 sql 查询的输出填充列表框

sql-server - SQL Server 相当于 Oracle LEAST?

sql - View 的复杂性——综合还是构建?

SQL 字符串函数

SQL 从未知文件名批量插入

sql - 创建非唯一索引时出现唯一索引错误 - SQL Server