0

我有两张桌子:

Table Name: One
id  | status
----+--------
1   | 1
2   | 1
3   | 1

Table Name: Two
id  | model
----+--------
1   | m001
2   | m002
3   | m003

假设我想更改 to 的m001状态0。如何使用 SQL 语句实现这一点?即如果model = m001,获取id,转到table.one,将状态更改为0。

4

2 回答 2

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')
于 2012-12-13T09:54:03.033 回答
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
于 2012-12-13T09:54:13.843 回答