怎麼對mysql表中的時間進行更新。 匿名使用者 1級 2012-08-11 回答

update borrow set Returntime1=‘2012-11-18 15:41:18’ , breturn=‘已’ where ID= 1;

同時更新多個欄位內容是用,號分開,不是用and連線!

怎麼對mysql表中的時間進行更新。 好小夥永垂不朽 1級 2012-08-11 回答

使用mysql的過程,經常會遇到一個問題,比如說某張”log”表,用於儲存某種記錄,隨著時間的不斷的累積資料,但是隻有最新的一段時間的資料是有用的;這個時候會遇到效能和容量的瓶頸,需要將表中的歷史資料進行歸檔。

下面描述一種典型的做法:

比如說表結構如下:

create table `history` (

`id` int(11) not null,

`value` text,

`addtime` timestamp default current_timestamp,

primary key (`id`),

index idx_addtime(`addtime`)

) engine=innodb default charset=utf8

這張表中儲存有2012年2013年兩年的資料,現在需要將2012年的資料備份歸檔起來,但是2013年年初的資料還需要被查詢,因此不能簡單的進行如下的動作:

create table history_tmp like history;

rename table history to history_2012,history_tmp to history;

需要在新表中保留2013年年初的資料,可以參照下面的流程進行:

create table history_tmp like history;

maxid=select max(id) from history;

minid=select id from history where addtime>“2013-01-01 00:00” order by addtime asc limit 1;

last=0;

set autocommit=1;

for(i=minid;i

=last and id

=last and id<=maxid;

alter table history rename to history_2012;

alter table history_tmp rename to history;

unlock tables;

commit;

說明:

使用alter table xx rename to xx,而不是rename是因為mysql的一個bug, bug地址 ,直接rename會出現”error 1192 (hy000): can’t execute the given command because you have active locked tables or an active transaction”錯誤。

需要使用lock history write來防止新的寫入。

這個方式是假設這個表在有插入和查詢操作,如果有update、delete操作可以透過類似osc的演算法使用trigger來實現。

不能直接使用insert select where id>minid這種方式,因為這樣會導致slave的延遲,而且遲遲不能提交的事務會導致undo log無法purge。