使用join更新查询

时间:2015-04-30 06:57:55

标签: sql join sql-update informix

我需要帮助才能使用连接查询更新数据库。这不是一个简单的查询,而是一种混乱。

我创建了一个选择查询来选择routelist(d.name),它等于' TP-424-RL'。

select n.dnorpattern as RoutePattern, 
rp.name as Partition, 
d.name as RouteList,
rl.selectionOrder,
rg.name as RouteGroup 
from numplan as n 
inner  join routepartition as rp 
  on  rp.pkid=n.fkroutepartition 
inner join devicenumplanmap as dnpm 
  on dnpm.fknumplan=n.pkid 
inner join device as d 
  on dnpm.fkdevice=d.pkid 
inner join routelist as rl 
  on rl.fkdevice = d.pkid 
inner join routegroup as rg 
  on rg.pkid=rl.fkroutegroup and n.tkpatternusage=5 
where  d.name='TP-424-RL';

同样,我想更新routelist(d.name =' SME-IT')而不是' TP-424-RL'。 d.name(设备表中的名称不提供routelist direclty,但是通过连接查询的组合给出了d.name = routelist)

我尝试了这个并且它没有用。

update (
select n.dnorpattern as RoutePattern, 
rp.name as Partition, 
d.name as RouteList , 
rl.selectionOrder, 
rg.name as RouteGroup 
from numplan as n 
inner join routepartition as rp 
  on  rp.pkid=n.fkroutepartition 
inner join devicenumplanmap as dnpm 
  on dnpm.fknumplan=n.pkid 
inner join device as d 
  on dnpm.fkdevice=d.pkid 
inner join routelist as rl 
  on rl.fkdevice = d.pkid 
inner join routegroup as rg 
  on rg.pkid=rl.fkroutegroup and n.tkpatternusage=5 
where d.name='TP-424-RL') set device.name='CME-IT' where device.name='TP-424-RL';

有人可以帮我吗?

3 个答案:

答案 0 :(得分:2)

由于您的查询中没有device.name,因此无效。 另外,对我来说这看起来很奇怪 我可能会这样做:(将与sql服务器一起工作,我不知道你的rdbms,所以我选择我最了解的)

update device
set name = 'CME-IT'
from numplan as n 
inner join routepartition as rp on  rp.pkid=n.fkroutepartition 
inner join devicenumplanmap as dnpm on dnpm.fknumplan=n.pkid 
inner join device as on dnpm.fkdevice = device.pkid 
inner join routelist as rl on rl.fkdevice = device.pkid 
inner join routegroup as rg on rg.pkid=rl.fkroutegroup and n.tkpatternusage=5 
where device.name='TP-424-RL'

答案 1 :(得分:2)

查看Informix中UPDATE语句的语法。未描述连接表示法更新,这就是您在尝试使用它时出现错误的原因。

您需要使用一般形式的UPDATE语句:

Integer s = 0;
Integer j = s;
s = 2;

和...部分将是一个查询,用于标识当前设备名称为arr = ['images', 'videos', 'pictures']; for (var i = 0; i < arr.length; i++) { $.ajax({ url: arr[i], success: function(data) { } }); } 的设备表中行的主键(或其他候选(唯一)键)以及行应该更新。 (我假设它不像'应该更新设备名UPDATE device SET name = 'SME-IT' -- Or did you mean CME-IT? The question says both WHERE pkid IN (SELECT ...) 的所有行那样简单。)

一个简单的版本可能是:

TP-424-RL

我怀疑查询可以简化,但我不确定您的架构的所有细节。 TP-424-RL条件可以移动;它似乎最符合UPDATE device SET name = 'SME-IT' -- Or did you mean CME-IT? The question says both WHERE pkid IN (SELECT d.pkid FROM numplan AS n JOIN routepartition AS rp ON rp.pkid = n.fkroutepartition JOIN devicenumplanmap AS dnpm ON dnpm.fknumplan = n.pkid JOIN device AS d ON dnpm.fkdevice = d.pkid JOIN routelist AS rl ON rl.fkdevice = d.pkid JOIN routegroup AS rg ON rg.pkid = rl.fkroutegroup AND n.tkpatternusage = 5 WHERE d.name='TP-424-RL' ) 的{​​{1}}条款,或者加入AND n.tkpatternusage = 5WHERESELECT的{​​{1}}条件。优化器会将它移动到它为降压提供最多的位置,这可能会在ON(aka n)表的扫描中进行过滤。

答案 2 :(得分:1)

您需要使用大括号加入替换值。如果您的查询符合“可更新”的要求,表格,您可以使用两列

进行更新