如何在 SQL 中将数据从一行复制到另一行
How to copy data from one row to another in SQL
所以我有一个名为 customers
的 table 并且在那个 table 中有些地方填写了 zip
列,但是 state
列不是。
我们可以合理地假设,如果我们有另一个客户同时填写了 zip
和 state
列,并且 zip
匹配没有 state
的客户填充,未填充 state
的客户可以填充与其他匹配客户相同的值。
目标是在我们知道缺失值的另一行时填充它们。
示例:
BEFORE: AFTER:
/====================\ /====================\
| id | zip | state | | id | zip | state |
|----|-------|-------| |----|-------|-------|
| 0 | 12345 | FL | ==> | 0 | 12345 | FL |
| 1 | 67890 | CA | ==> | 1 | 67890 | CA |
| 2 | 67890 | | ==> | 2 | 67890 | CA |
| 3 | 12345 | | | 3 | 12345 | FL |
| 4 | 10101 | | | 4 | 10101 | |
\====================/ \====================/
我的问题: 我如何使用 MySQL 更新 table 并从另一行填充缺失的数据?
到目前为止,我有 SQL 用于查找缺失值:
SELECT *
FROM customers
WHERE country = 'united states'
AND (zip <> '' OR zip IS NOT NULL)
AND (state = '' OR state IS NULL)
您可以使用更新和内部联接
update my_table as a
inner join source_table as b on a.zip = b.zip
set a.state = b.state
where b.state is not null
or b.state = ''
AND country = 'united states' ;
你可以这样做:
UPDATE customers
INNER JOIN customers AS updateValues ON updateValues.zip = customers.zip AND updateValues.state IS NOT NULL AND updateValues.state != ''
SET customers.state = updateValues.state
WHERE (customers.zip <> '' AND customers.zip IS NOT NULL)
AND (customers.state = '' OR customers.state IS NULL)
(不确定 WHERE country = 'united states'
的来源,所以我删除了它)
我不会这样做。
https://en.wikipedia.org/wiki/List_of_ZIP_code_prefixes
邮政编码的前 3 位数字表示它所在的州。我会根据它做一些逻辑。
编辑:这不必太复杂
UPDATE CUSTOMERS
SET STATE = CASE
WHEN SUBSTRING(zip, 1,3) IN ('448','449', ect....) THEN 'OH'
....
....
END
WHERE STATE IS NULL
不确定性能,但您应该能够:
update table_name tn1
set tn1.state = (select max(tn2.state) from table_name tn2 where tn2.zip = tn1.zip and tn2.state is not null)
where tn1.state is null
所以我有一个名为 customers
的 table 并且在那个 table 中有些地方填写了 zip
列,但是 state
列不是。
我们可以合理地假设,如果我们有另一个客户同时填写了 zip
和 state
列,并且 zip
匹配没有 state
的客户填充,未填充 state
的客户可以填充与其他匹配客户相同的值。
目标是在我们知道缺失值的另一行时填充它们。
示例:
BEFORE: AFTER:
/====================\ /====================\
| id | zip | state | | id | zip | state |
|----|-------|-------| |----|-------|-------|
| 0 | 12345 | FL | ==> | 0 | 12345 | FL |
| 1 | 67890 | CA | ==> | 1 | 67890 | CA |
| 2 | 67890 | | ==> | 2 | 67890 | CA |
| 3 | 12345 | | | 3 | 12345 | FL |
| 4 | 10101 | | | 4 | 10101 | |
\====================/ \====================/
我的问题: 我如何使用 MySQL 更新 table 并从另一行填充缺失的数据?
到目前为止,我有 SQL 用于查找缺失值:
SELECT *
FROM customers
WHERE country = 'united states'
AND (zip <> '' OR zip IS NOT NULL)
AND (state = '' OR state IS NULL)
您可以使用更新和内部联接
update my_table as a
inner join source_table as b on a.zip = b.zip
set a.state = b.state
where b.state is not null
or b.state = ''
AND country = 'united states' ;
你可以这样做:
UPDATE customers
INNER JOIN customers AS updateValues ON updateValues.zip = customers.zip AND updateValues.state IS NOT NULL AND updateValues.state != ''
SET customers.state = updateValues.state
WHERE (customers.zip <> '' AND customers.zip IS NOT NULL)
AND (customers.state = '' OR customers.state IS NULL)
(不确定 WHERE country = 'united states'
的来源,所以我删除了它)
我不会这样做。
https://en.wikipedia.org/wiki/List_of_ZIP_code_prefixes
邮政编码的前 3 位数字表示它所在的州。我会根据它做一些逻辑。
编辑:这不必太复杂
UPDATE CUSTOMERS
SET STATE = CASE
WHEN SUBSTRING(zip, 1,3) IN ('448','449', ect....) THEN 'OH'
....
....
END
WHERE STATE IS NULL
不确定性能,但您应该能够:
update table_name tn1
set tn1.state = (select max(tn2.state) from table_name tn2 where tn2.zip = tn1.zip and tn2.state is not null)
where tn1.state is null