更新值对在SQL中匹配的位置

问题描述 投票:0回答:4

我需要更新此表:

中心:

+-----+------------+---------+--------+
|  id |  country   | process | center |
+-----+------------+---------+--------+
|  1  |     1      |    1    |   1    |
|  2  |     1      |    2    |   1    |
|  3  |     1      |    3    |   1    |
|  4  |     2      |    1    |   1    |
|  5  |     2      |    2    |   1    |
|  6  |     2      |    3    |   1    |
|  7  |     3      |    1    |   1    |
|  8  |     3      |    2    |   1    |
|  9  |     3      |    3    |   1    |
+-----+------------+---------+--------+

在选择过程中,我检索两个临时表:

TempCountries:

+-----+------------+
|  id |  country   |
+-----+------------+
|  1  |     1      |
|  2  |     3      |
+-----+------------+

和TempProcesses:

+-----+------------+
|  id |  process   |
+-----+------------+
|  1  |     2      |
|  2  |     3      |
+-----+------------+

在子查询中,我获得了所有可能的值组合:

SELECT TempCountries.countryId, TempProcesses.processesId FROM TempCenterCountries,TempCenterProcesses

返回:

+-----+------------+---------+
|  id |  country   | process |
+-----+------------+---------+
|  1  |     1      |    2    |
|  2  |     1      |    3    |
|  3  |     3      |    2    |
|  4  |     3      |    3    |
+-----+------------+---------+

在选择过程中,用户为这些组合选择一个中心。假设center = 7.现在我需要更新子表中存在子查询组合的中心值。

所以,

UPDATE Centers SET center = 7 WHERE ?

所以我得到:

+-----+------------+---------+--------+
|  id |  country   | process | center |
+-----+------------+---------+--------+
|  1  |     1      |    1    |   1    |
|  2  |     1      |    2    |   7    |
|  3  |     1      |    3    |   7    |
|  4  |     2      |    1    |   1    |
|  5  |     2      |    2    |   1    |
|  6  |     2      |    3    |   1    |
|  7  |     3      |    1    |   1    |
|  8  |     3      |    2    |   7    |
|  9  |     3      |    3    |   7    |
+-----+------------+---------+--------+
sql where updates
4个回答
0
投票

在运行更新查询之前,您需要具有国家/地区和流程的完全匹配。所以,像下面的查询将帮助您实现这一目标。如果存在记录,则基本上更新列

WITH (SELECT TempCountries.countryId, TempProcesses.processesId
FROM TempCenterCountries,
 TempCenterProcesses) AS TempTables,
UPDATE Centers
SET center = 7
WHERE EXISTS (SELECT 1
          FROM TempTables tmp
          WHERE country = tmp.countryId and process = tmp.processesId
         );

如果国家/地区和流程都与您在临时表中提取的国家/地区匹配,则更新记录。


1
投票

并非所有sql实现都允许您在使用update时使用from子句。幸运的是,在您的情况下,因为您正在使用笛卡尔积来获得所有组合,这意味着您在两个值之间没有任何约束。

UPDATE  Centers
SET center = 7
WHERE   country IN (SELECT countryId FROM TempCountries)
AND process IN (SELECT processId FROM TempCenterProcesses)

1
投票

试试这个标准的sql,

Update Centers
set center = 7
where country in (select country from TempCenterCountries)
   and process in (select process from TempCenterProcesses)

0
投票

使用更新加入 -

对于Sql Server

update c set SET center = 7 from Centers c
join
(SELECT TempCountries.countryId, TempProcesses.processesId FROM TempCenterCountries join TempCenterProcesses
)A on c.countryid=A.countryid and c.processesId=A.processId

对于Mysql -

    update Centers c  
    join
    (SELECT TempCountries.countryId, TempProcesses.processesId FROM TempCenterCountries join TempCenterProcesses
    )A on c.countryid=A.countryid and c.processesId=A.processId
    set SET center = 7
© www.soinside.com 2019 - 2024. All rights reserved.