我遇到了一个问题,我需要根据存储在另一个表中的值更新一个表。但是,第二个表包含与查询无关的行。例如:
Table1
id | active
------------
1 | Yes
2 | Yes
3 | Yes
4 | Yes
Table2
id | type | value
--------------------
1 | date | 2011
1 | name | Glen
2 | date | 2012
2 | name | Mike
我想读取'date'类型的值并跳过name,并在此过程中更新table1。
我把以下内容放在一起:
UPDATE table1 a, tabel2 b
SET a.active='no'
WHERE a.id = b.id
AND b.type='date'
AND b.value='2011'
这似乎并不合适。
任何帮助都会很棒。
答案 0 :(得分:1)
id
是加入表格的关键。
UPDATE table1 a, tabel2 b
SET a.active='no'
WHERE a.id = b.id
AND b.type='date'
AND b.value='2011'
答案 1 :(得分:1)
试试这个:
UPDATE table1
SET active = 'no'
WHERE a.id
IN (
SELECT b.id FROM table2 WHERE type = 'date' AND value = '2011'
)
答案 2 :(得分:0)
这适用于自然联接
UPDATE table1
SET active='no'
WHERE id in
(
select id from table1 natural join table2
where
type='date'
AND value='2011'
)