我正试图从表中删除除最后10个结果外的所有结果。这不应该太难,但他们也必须符合一定的标准。
桌子看起来是这样的;
chat_id
location_id
user_id
message
created
从技术上讲,它必须删除具有特定location_id的所有内容,并保留表中最后10条记录。
对于Active Record类,这可能吗?
T提前存储
这根本不是一个最佳解决方案,但我想这可能会有所帮助。
根据你的问题,我假设标准是一个特定的location_id
。
如果创建的字段是一个时间戳字段,并且最后10条记录是指最近的10条记录,则可以使用类似于以下的查询:
DELETE FROM `table`
WHERE created<
(select min(a.cdate) from
(
select b.created as cdate from `table` b
where b.location_id=<<loc_id>>
order by b.created desc limit 10
) a
)
AND location_id=<<loc_id>>;
过一段时间就会出现活动记录。您可以尝试以下sql查询:
$this->db->query('
delete from table_name where chat_id NOT IN
(select chat_id from table_name where location_id=1292 order by chat_id DESC LIMIT 10)
')