0

嗨,我的 sql 编写经验很少,我正在尝试从一个表中进行简单的选择,然后更新另一个表

所以例如

select id from customer where name like 'Test'

然后对于它找到的每个 id,因为可能有多个

update customeractive set active=0 where id= "from the last table"
4

5 回答 5

2

你可以这样做:

update customeractive
    set active=0
    where id in (select id from customer where name like 'Test');

这是标准 SQL,适用于任何数据库。

于 2013-08-19T13:09:49.257 回答
1
UPDATE customeractive
SET active=0
WHERE id IN 
(
    SELECT id
    FROM customer
    WHERE name LIKE 'Test'
)
于 2013-08-19T13:09:38.283 回答
0

如果您使用的是 Microsoft SQL Server,另一种方法是:

UPDATE customeractive
SET active=0
from customeractive join customer on customeractive.id=customer.id
where name like '% Test%'

顺便说一句,我认为您可能是指在 LIKE 语句中使用通配符(例如 %)

于 2013-08-19T13:22:47.560 回答
0
update customeractive
    set active = 0
from customeractive act
    inner join customer cust on act.id = cust.id
where cust.name like 'Test'
于 2013-08-19T13:12:21.183 回答
0

如果您使用的是 SQL Server,您可以执行一些操作,例如在临时表中选择所需的 ID,然后更新所需的 ID。

在你的例子中:

Select ID into #TempTable from Customer Where name like 'Teste'

Update   CustomerActive
Set    active = 0
Where Id in
(Select ID from #TempTable).

另一种方法是使用联接

Update    CustomerActive
Set active = 0
From CustomerActive CA
Join Customer C
On CA.ID = C.ID
Where C.name like 'Test'

如果您不使用 SQL Server,则可以使用相同的概念。只需检查如何在其他语言上完成即可。

希望它能解决你的问题。

于 2013-08-19T13:13:43.833 回答