есть таблица test таблица с полями
name created_at active
record 2008 1
record 2007 1
record 2006 1
record1 2008 1
record1 2007 1
record1 2006 1
..................................
another ----
one more ----
нужно сделать апдейт таблицы: все одинаковые созданные поля сделать неактивными(active = 0) кроме самых свежих из однинаковых, чтобы таким образом в итоге получить
name created_at active
record 2008 1
record 2007 0
record 2006 0
record1 2008 1
record1 2007 0
record1 2006 0
...................................
another ----
one more ----
сложный update
- Игорь Акопян
- Сообщения: 1440
- Зарегистрирован: 13 окт 2004, 17:11
- Откуда: СПБ
- Контактная информация:
какой сервер?

update test
set active = 0
where active = 1
and exists (select 1 from test b
where b.name = test.name
and b.active = 1
and b.created_at > test.created_at)
set active = 0
where active = 1
and exists (select 1 from test b
where b.name = test.name
and b.active = 1
and b.created_at > test.created_at)