Sql 如何使用其他表中的条件更新表

Sql 如何使用其他表中的条件更新表,sql,oracle,join,sql-update,Sql,Oracle,Join,Sql Update,我有“Person”表和“Fisica”表,这是“Person”的扩展。这两个表都由字段名Id关联,我想根据包含这两个表的条件更新这些表。 例如: Tables: Persona(Id, Name, Money) Fisica(Id, LastName, Year) 关于数据: Persona(1, X, 5) Persona(2, A, 10) Fisica(1, Y, 1990) Fisica(2, B, 2000) 我想在Persona.Name='X'时设置Persona.Name=

我有“Person”表和“Fisica”表,这是“Person”的扩展。这两个表都由字段名Id关联,我想根据包含这两个表的条件更新这些表。 例如:

Tables:
Persona(Id, Name, Money)
Fisica(Id, LastName, Year)
关于数据:

Persona(1, X, 5)
Persona(2, A, 10)
Fisica(1, Y, 1990)
Fisica(2, B, 2000)
我想在Persona.Name='X'时设置Persona.Name=some_值,Fisica.LastName=other_值,Fisica.Year=number,这样就可以得到结果

Persona(1, some_Value, 5)
Persona(2, A, 10)
Fisica(1, other_Value, number)
Fisica(2, B, 2000)

我在Oracle工作

不能用一条语句更新两个表。因此,您需要两个
UPDATE
语句

所以你必须先更新一个,然后更新另一个。问题是,哪一个是第一个

这其实并不重要,但是如果您首先更新Persona并更改名称,那么在更新Fisica时必须使用新名称。像这样:

update Persona
set name = 'some_Value'
where name = 'current_Name'
;
update Fisica
set lastname = 'other_Value',
    year = number
where id = (
  select id
  from Persona
  where name = 'some_Value'
)
;
如果首先更新Fisica,则两次都使用旧的name值,如下所示:

update Fisica
set lastname = 'other_Value',
    year = number
where id = (
  select id
  from Persona
  where name = 'current_Value'
)
;
update Persona
set name = 'some_Value'
where name = 'current_Value'
;
Update table1 t1, table2 t2 set t2.field2="ABC" where t1.id=t2.id
编号
必须替换为实际数值


良好的编码实践是将两条语句放在同一事务中,并且仅在它们都成功时提交。

不能用一条语句更新两个表。因此,您需要两个
UPDATE
语句

所以你必须先更新一个,然后更新另一个。问题是,哪一个是第一个

这其实并不重要,但是如果您首先更新Persona并更改名称,那么在更新Fisica时必须使用新名称。像这样:

update Persona
set name = 'some_Value'
where name = 'current_Name'
;
update Fisica
set lastname = 'other_Value',
    year = number
where id = (
  select id
  from Persona
  where name = 'some_Value'
)
;
如果首先更新Fisica,则两次都使用旧的name值,如下所示:

update Fisica
set lastname = 'other_Value',
    year = number
where id = (
  select id
  from Persona
  where name = 'current_Value'
)
;
update Persona
set name = 'some_Value'
where name = 'current_Value'
;
Update table1 t1, table2 t2 set t2.field2="ABC" where t1.id=t2.id
编号
必须替换为实际数值


良好的编码实践是将这两条语句放在同一事务中,并且只有在它们都成功时才提交。

尽管Oracle没有提供与mysql相比的功能,在mysql中我们可以这样编写查询:

update Fisica
set lastname = 'other_Value',
    year = number
where id = (
  select id
  from Persona
  where name = 'current_Value'
)
;
update Persona
set name = 'some_Value'
where name = 'current_Value'
;
Update table1 t1, table2 t2 set t2.field2="ABC" where t1.id=t2.id
但您可以在Oracle中使用单个查询更新多个表以获取视图

因此,为上述两个表创建视图
Persona,Fisica
,并编写如下查询:

 CREATE OR REPLACE FORCE VIEW  "Persona_Fisica" ("PersonaID", "Name", "Money", "FisicaID", "Lastname", "Year") AS 
      select P.Id as PersonaID,
        Name as Name,
        Money as Money,
        F.Id as FisicaID,
        LastName as Lastname,
        year as Year
     from Persona P,
        Fisica F 
     where P.ID=F.ID
/
Update Persona_Fisica set Name=some_Value, LastName=other_Value,Year=number were Name='X',

尽管Oracle不提供与mysql相比的功能,我们可以在mysql中编写如下查询:

update Fisica
set lastname = 'other_Value',
    year = number
where id = (
  select id
  from Persona
  where name = 'current_Value'
)
;
update Persona
set name = 'some_Value'
where name = 'current_Value'
;
Update table1 t1, table2 t2 set t2.field2="ABC" where t1.id=t2.id
但您可以在Oracle中使用单个查询更新多个表以获取视图

因此,为上述两个表创建视图
Persona,Fisica
,并编写如下查询:

 CREATE OR REPLACE FORCE VIEW  "Persona_Fisica" ("PersonaID", "Name", "Money", "FisicaID", "Lastname", "Year") AS 
      select P.Id as PersonaID,
        Name as Name,
        Money as Money,
        F.Id as FisicaID,
        LastName as Lastname,
        year as Year
     from Persona P,
        Fisica F 
     where P.ID=F.ID
/
Update Persona_Fisica set Name=some_Value, LastName=other_Value,Year=number were Name='X',

您显示了两个错误的查询,但没有说出您想要的结果。向我们展示数据库模式、示例数据和期望输出。请阅读,并显示两个错误的查询,但不要说你想要的结果是什么。向我们展示数据库模式、示例数据和期望输出。请阅读并感谢您的回复。我想知道您提到的mysql查询(更新table1 t1,table2 t2 set t2.field2=“ABC”其中t1.id=t2.id)在Sql Server中是否有效。感谢您的回复。我想知道您提到的mysql查询(更新table1 t1,table2 t2 set t2.field2=“ABC”,其中t1.id=t2.id)是否适用于Sql Server。