gpt4 book ai didi

mysql - 使用相同的表更新表中的各个 ID 的列。

转载 作者:行者123 更新时间:2023-11-29 10:42:44 24 4
gpt4 key购买 nike

如何从同一个表中更新各个 ID 的 Opportunity_Record_Type、Opportunity_Division 和 Fiscal_Period? PK 是 Opportunity_ID。

+--------------------+-------------------------+----------------------+---------------+-----------------------+--------------------------+
| Opportunity_ID | Opportunity_Record_Type | Opportunity_Division | Fiscal_Period | Script_Execution_Week | Script_Execution_Quarter |
+--------------------+-------------------------+----------------------+---------------+-----------------------+--------------------------+
| LKASDJKLkasdkl23k4 | NULL | NULL | NULL | 2017-21 | 2017-Q2 |
| LKASDJKLkasdkl23k4 | NULL | NULL | NULL | 2017-22 | 2017-Q2 |
| LKASDJKLkasdkl23k4 | NULL | NULL | NULL | 2017-23 | 2017-Q2 |
| LKASDJKLkasdkl23k4 | NULL | NULL | NULL | 2017-24 | 2017-Q2 |
| LKASDJKLkasdkl23k4 | NULL | NULL | NULL | 2017-25 | 2017-Q2 |
| LKASDJKLkasdkl23k4 | Services | SBS | 2017-Q2 | 2017-26 | 2017-Q3 |
| LKASDJKLkasdkl23k4 | Services | SBS | 2017-Q2 | 2017-27 | 2017-Q3 |
| LKASDJKLkasdkl23k4 | Services | SBS | 2017-Q2 | 2017-28 | 2017-Q3 |
| LKASDJKLkasdkl23k4 | NULL | NULL | NULL | 2017-7 | 2017-Q1 |
| LKASDJKLkasdkl23k4 | NULL | NULL | NULL | 2017-8 | 2017-Q1 |
| LKASDJKLkasdkl23k4 | NULL | NULL | NULL | 2017-9 | 2017-Q1 |
| SDFSDKLJ2ELKMLKASM | NULL | NULL | NULL | 2017-20 | 2017-Q2 |
| SDFSDKLJ2ELKMLKASM | NULL | NULL | NULL | 2017-21 | 2017-Q2 |
| SDFSDKLJ2ELKMLKASM | NULL | NULL | NULL | 2017-22 | 2017-Q2 |
| SDFSDKLJ2ELKMLKASM | NULL | NULL | NULL | 2017-23 | 2017-Q2 |
| SDFSDKLJ2ELKMLKASM | NULL | NULL | NULL | 2017-24 | 2017-Q2 |
| SDFSDKLJ2ELKMLKASM | NULL | NULL | NULL | 2017-25 | 2017-Q2 |
| SDFSDKLJ2ELKMLKASM | Mobile | Mobile | 2017-Q2 | 2017-26 | 2017-Q3 |
| SDFSDKLJ2ELKMLKASM | Mobile | Mobile | 2017-Q2 | 2017-27 | 2017-Q3 |
| SDFSDKLJ2ELKMLKASM | Mobile | Mobile | 2017-Q2 | 2017-28 | 2017-Q3 |
+--------------------+-------------------------+----------------------+---------------+-----------------------+--------------------------+

最佳答案

你可以做到

UPDATE table1 t JOIN (
SELECT Opportunity_ID, Opportunity_Record_Type, Opportunity_Division, Fiscal_Period
FROM table1
WHERE Opportunity_Record_Type IS NOT NULL
AND Opportunity_Division IS NOT NULL
AND Fiscal_Period IS NOT NULL
GROUP BY Opportunity_ID, Opportunity_Record_Type, Opportunity_Division, Fiscal_Period
) q
ON t.Opportunity_ID = q.Opportunity_ID
SET t.Opportunity_Record_Type = q.Opportunity_Record_Type,
t.Opportunity_Division = q.Opportunity_Division,
t.Fiscal_Period = q.Fiscal_Period
WHERE t.Opportunity_Record_Type IS NULL
OR t.Opportunity_Division IS NULL
OR t.Fiscal_Period IS NULL

这是一个dbfiddle演示

关于mysql - 使用相同的表更新表中的各个 ID 的列。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45176919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com