从另一个表中的查询更新表

时间:2013-05-30 20:39:31

标签: sql database sql-update

UPDATE devicenumplanmap 
SET devicenumplanmap.e164mask = '+1' 
FROM devicenumplanmap 
WHERE (select devicenumplanmap.pkid, devicenumplanmap.e164mask 
       from devicenumplanmap, device 
       where devicenumplanmap.fkdevice = device.pkid 
         and device.name like '%RDP' 
         and devicenumplanmap.e164mask like '819%')

发生语法错误。

我们想要从另一个表中的搜索更新表devicenumplanmap。

3 个答案:

答案 0 :(得分:1)

看起来您正在尝试使用UPDATE JOIN。根据您的RDBMS,语法略有不同。

<强> MySQL的:

UPDATE devicenumplanmap d
    JOIN device de ON d.fkdevice = de.pkid
SET d.e164mask = '+1' 
WHERE de.name like '%RDP' 
       AND d.e164mask like '819%'

SQL Server:

UPDATE  d
SET d.e164mask = '+1' 
FROM devicenumplanmap d 
    JOIN device de ON d.fkdevice = de.pkid
WHERE de.name like '%RDP' 
       AND d.e164mask like '819%'

更普遍的方法:

UPDATE devicenumplanmap 
SET e164mask = '+1' 
WHERE e164mask like '819%' AND
    fkdevice IN (
        SELECT pkid
        FROM Device 
        WHERE name like '%RDP' 
)

答案 1 :(得分:0)

尝试类似的东西:

UPDATE devicenumplanmap 
SET devicenumplanmap.e164mask = '+1' 
FROM devicenumplanmap 
WHERE **exists(select ***
       from devicenumplanmap, device 
       where devicenumplanmap.fkdevice = device.pkid 
         and device.name like '%RDP' 
         and devicenumplanmap.e164mask like '819%')

答案 2 :(得分:0)

这应该适用,简单易用

UPDATE devicenumplanmap
SET devicenumplanmap.e164mask = '+1' 
FROM devicenumplanmap dp 
INNER JOIN device d
ON dp.fkdevice = d.pkid
WHERE d.name like '%RDP' 
and dp.e164mask like '819%'