sql update for 1 table包含在子句3表中

时间:2017-06-28 18:52:04

标签: mysql sql

我做了Select语句:

Select table1.id, table1.date, table1.name, table2.text
from table1
inner join table2 on table1.id=table2.id
where table2.date =< '20-05-2017'
and table2.date > '20-05-2017'
and table2.statement = '%text%'

这个工作,但现在我必须更新它,但也有第三个表 所以我需要

Update table1
set table1.groupid = 10
where table2.date =< '20-05-2017'
and table2.date > '20-05-2017'
and table2.statement = '%text%'
and table3.term = 100

我怎样才能加入所有这些表,所以我的声明有效,table3.otherid = table1.otherid有很多行,所以我不能把它们带到n ++中,并为它们形成这样的所有

update table1
set table1.groupid = 
where table1.id in (all the id)

3 个答案:

答案 0 :(得分:0)

您可以使用Update-Join

update table1
    inner join table2 on table1.id=table2.id
    inner join table3.otherid=table1.otherid
set table1.groupid = 10
where table2.date =< '20-05-2017'
    and table2.date > '20-05-2017'
    and table2.statement = '%text%'
    and table3.term = 100

如果这不是您要找的,请清楚解释您的问题。

答案 1 :(得分:0)

如果您使用的是mysql 您可以使用内部联接的更新
(我有用于table3内连接你条件table3.otherid = table1.otherid ..确保这是正确的)

  Update table1
  inner join table2 on table1.id=table2.id
  inner join table3 on table3.otherid=table1.otherid 
  set table1.groupid = 10
  where table2.date =< '20-05-2017'
  and table2.date > '20-05-2017'
  and table2.statement = '%text%'
  and table3.term = 100

如果你正在使用postgresql,你可以使用这个sintax

  Update table1 t1
  set t1.groupid = 10
  FROM table1 ttt1
  inner join table2 on ttt1.id=table2.id
  inner join table3 on table3.otherid=ttt1.otherid 
  where table2.date =< '20-05-2017'
  and table2.date > '20-05-2017'
  and table2.statement = '%text%'
  and table3.term = 100

答案 2 :(得分:0)

嗯,

Update table1
  inner join table2 on table1.id=table2.id
  inner join table3 on table3.otherid=table1.otherid 
  set table1.groupid = 10
  where table2.date =< '20-05-2017'
  and table2.date > '20-05-2017'
  and table2.statement = '%text%'
  and table3.term = 100

通过示例编写我有语法错误 错误:语法错误在或附近&#34;内部&#34; SQL状态:42601