我有两张桌子:
Table Name: One
id | status
----+--------
1 | 1
2 | 1
3 | 1
Table Name: Two
id | model
----+--------
1 | m001
2 | m002
3 | m003
假设我想将m001
的状态更改为0
。如何使用SQL语句实现此目的?即如果model = m001,则获取id,转到table.one,将状态更改为0。
答案 0 :(得分:2)
由于您没有提及RDBMS
,请尝试以下任何一项:
MySQL
UPDATE One a
INNER JOIN Two b
ON a.ID = b.ID
SET a.Status = 0
WHERE b.model = 'm001'
MSSQL
UPDATE a
SET a.Status = 0
FROM One a
INNER JOIN Two b
ON a.ID = b.ID
WHERE b.model = 'm001'
哪个适用于其他DBMS
UPDATE One
SET Status = 0
WHERE ID = ( SELECT ID
FROM two
WHERE model = 'm001')
答案 1 :(得分:2)
UPDATE TabelOne as a SET Status = 0
WHERE a.id = (select id from two where model = 'm001')
我的SQL -
UPDATE TabelOne as a
INNER JOIN two as b ON a.id = b.id and b.model = 'm001'
SET Status = 0