【问题标题】:How to check and update table #1 data from table #2?如何检查和更新表 #2 中的表 #1 数据?
【发布时间】:2016-09-15 05:12:43
【问题描述】:

我的数据库中有 2 个表。

表 #1

-------------------------------------------------------------------------
| name | family | phone | email | gender | phone2 | address | birthdate |
-------------------------------------------------------------------------

表#2

-----------------------------------------
| gender | address | phone | birthdate |
-----------------------------------------

在表 #1 中的列地址和电话 2 为空,列的性别和出生日期值与表 #2 相同。

当每行中的性别和出生日期都相同时,如何从表 #2 中读取数据并使用表 #2 地址和电话列中的值更新表 #1 中的地址和电话 2?

例如: 这是表#1中的一些数据

-------------------------------------------------------------------------
| name | family | phone | email | gender | phone2 | address | birthdate |
-------------------------------------------------------------------------
| john | doe    | 12345| t@t.com| Male  |         |         | 1980-01-01|
-------------------------------------------------------------------------
| mike | clark  | 65432| x@y.com| Male  |         |         | 1990-01-01|
-------------------------------------------------------------------------
| Sara | King   | 875465| a@b.com|Female|         |         | 1970-01-01|
-------------------------------------------------------------------------

这是表#2中的一些数据

-----------------------------------------
| gender | address | phone | birthdate  |
-----------------------------------------
| Male   | 1704test|0457852|1980-01-01  |
-----------------------------------------
| Female | 1705abcs|0986532|1970-01-01  |
-----------------------------------------
| Male   | 1602cyzd|0326589|1990-01-01  |
-----------------------------------------

我想用表 #2 中的数据更新表 #1 并检查性别和出生日期并使表 #1 像

-------------------------------------------------------------------------
| name | family | phone | email | gender | phone2 | address | birthdate |
-------------------------------------------------------------------------
| john | doe    | 12345| t@t.com| Male   |0457852 |1704test | 1980-01-01|
-------------------------------------------------------------------------
| mike | clark  | 65432| x@y.com| Male   |0326589  |1602cyzd| 1990-01-01|
-------------------------------------------------------------------------
| Sara | King   | 875465| a@b.com|Female |0986532  |1705abcs| 1970-01-01|
-------------------------------------------------------------------------

我该怎么做?

【问题讨论】:

  • 请不要交叉发布您的问题

标签: sql-server tsql ssms sql-server-2014 database-administration


【解决方案1】:

您可以在更新语句中使用JOIN

UPDATE t1
SET t1.phone2 = t2.phone, t1.address = t2.address
FROM table1 t1
JOIN table2 t2 on t1.gender = t2.gender and t1.birthdate = t2.birthdate

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2011-10-25
    • 1970-01-01
    • 2017-07-25
    • 2013-01-20
    • 1970-01-01
    相关资源
    最近更新 更多