【问题标题】:SQL Syntax select something from a table and insert the result into a new table if it doesn't existSQL 语法从表中选择某些内容,如果结果不存在,则将结果插入新表中
【发布时间】:2014-01-28 13:18:57
【问题描述】:

我对 SQL 语法很陌生,我在 Windows Server 上使用 MSSQL 2012 有以下场景:

2 个表,我们称它们为 table1 和 table2

我现在想通过电子邮件从 dbo.table1 中选择一个用户并取出他的 UserID ,稍后如果表还没有此 UserID,我需要将此 UserID 插入到 dbo.table2 中。

所以我开始做一个简单的选择:

Select from dbo.table1 where 'email' = 'xxx@xxx.xx'

现在,如果 dbo.table2 没有类似的 UserID,我需要选择该帐户的 UserID 并将其推送到 dbo.table2。

我如何做到这一点?

【问题讨论】:

  • 你同时标记了 mysql 和 sql-server...它是什么?

标签: sql sql-server windows-server-2012


【解决方案1】:

您可以尝试以下方法:

INSERT INTO dbo.table2 (UserID)
SELECT UserID
FROM dbo.table1
WHERE NOT EXISTS (SELECT UserID
FROM dbo.table2)

【讨论】:

  • @RayofCommand 答案对您有帮助吗?如果是approve
猜你喜欢
  • 2012-06-24
  • 2016-11-27
  • 1970-01-01
  • 2020-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-05
相关资源
最近更新 更多