Como faço para participar de mesas em uma atualização
-
03-07-2019 - |
Pergunta
Eu tenho a seguinte consulta:
select count(L.ID)
from LA inner join L on (LA.leadid = L.ID)
where L.status = 5
and L.city = "cityname"
and Date(LA.Datetime) < Date_Sub(Now(), INTERVAL 6 MONTH);
que procura por registros com status de 5 em uma determinada cidade que são mais velhos do que 6 meses (a data para a qual é armazenada em Los Angeles). Isso retorna cerca de 4k resultados. Gostaria de atualizar o valor do status para 1 em cada um desses registros, e assim por meus olhares de atualização como:
update L, LA
set L.status = 1
where L.status = 5
and L.city = "cityname"
and Date(LA.SomeDatetime) < Date_Sub(Now(), INTERVAL 6 MONTH);
mas barracas fora e bloqueia o db. Eu suspeito que há um problema, porque não há como juntar-se, mas eu tento algo como:
update L, LA
from L inner join LA on (L.OID = LA.leadid)
set L.status = 1
where L.status = 5
and L.syscity = "cityname"
and Date(LA.SomeDatetime) < Date_Sub(Now(), INTERVAL 6 MONTH);
e, obviamente, não vai funcionar porque não há 'de' está em uma atualização.
Editar> Estou usando o MySQL
Solução
update L
set L.status = 1
where L.status = 5
and L.city = "cityname"
and EXISTS (
select * from LA
where Date(LA.SomeDatetime) < Date_Sub(Now(), INTERVAL 6 MONTH)
and LA.leadid = L.ID
)
Outras dicas
Para MySQL
, você pode usar velho juntar sintaxe:
UPDATE l, la
SET l.status = 1
WHERE l.status = 5
AND l.city = "cityname"
AND la.leadid = l.id
AND DATE(la.datetime) < DATE_SUB(NOW(), INTERVAL 6 MONTH)
Eu faria isso:
update L
set status = 1
from LA
where L.OID = LA.leadid
and L.status = 5
and L.syscity = "cityname"
and Date(LA.SomeDatetime) < Date_Sub(Now(), INTERVAL 6 MONTH)
Veja também:
SQL atualização de uma tabela para outro baseado em um jogo ID
No SQL Server 2005 este trabalho:
Update L
set L.status = 1
from
L
--
JOIN LA
on (LA.leadid = L.id)
where
L.status = 5
and L.city = "cityname"
and Date(LA.Datetime) < Date_Sub(Now(), INTERVAL 6 MONTH);