我有下面的表格
CREATE TABLE `data` (
`acquire_time` decimal(26,6) NOT NULL,
`sample_time` decimal(26,6) NOT NULL,
`channel_id` mediumint(8) unsigned NOT NULL,
`value` varchar(40) DEFAULT NULL,
`status` tinyint(3) unsigned DEFAULT NULL,
`connected` tinyint(1) unsigned NOT NULL,
PRIMARY KEY (`channel_id`,`acquire_time`),
UNIQUE KEY `index` (`channel_id`,`sample_time`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;对于每个channel_id,我希望找到具有最大获取时间的行,并将值更改为NULL,将状态更改为NULL,并将其连接到0。这个是可能的吗?手册说,您不能更新表,也不能从子查询中的同一个表中选择...
谢谢您抽时间见我。
发布于 2011-04-19 11:15:02
update data join
(
select channel_id, max(acquire_time) acquire_time
from data
group by channel_id
) x on x.channel_id = data.channel_id and x.acquire_time = data.acquire_time
set
value = null,
status = null,
connected = 0发布于 2011-04-19 11:11:50
尝尝这个。
update data data_1
set value = null, status = null
where not exists (
select 1
from data data_2
where data_2.channel_id = data_1.channel_id
and data_2.acquire_time > data_1.acquire_time
)如果这不起作用,试试这个:
update data as data_1
left join data as data_2
on data_2.channel_id = data_1.channel_id
and data_2.acquire_time > data_1.acquire_time
set data_1.value = null, data_1.status = null
where data_2.acquire_time is nullhttps://stackoverflow.com/questions/5710777
复制相似问题