返回信息流使用mysql。表如下:
create table status (
id int not null primary key auto increment,
value int not null,
dead tinyint(1) not null
);
要求:
写一个函数
public void increaseValue(int id, int newValue) {
// 根据id查找一行。
// 如果没有这个id,则插入一行:(id, newValue, 0)
// 如果有这个id,但是dead==1,那么没有动作
// 如果有这个id,dead==0,但是value>=newValue,没有动作
// 如果有这个id,dead==0,value<newValue,那么更新这一行,将value设置为newValue
}
尽量减少与服务器之间的消息往返次数。
我目前的实现:
INSERT IGNORE INTO status (id, value, dead)
VALUES (:id, :newValue, 0);
REPLACE status SET value=newValue
WHERE id = :id and dead = 0 and value < :newValue;
能再少一个语句不?
这是一条镜像帖。来源:北邮人论坛 / database / #5166同步于 2011/1/6
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Database机器人发帖
[求助]一个"更新"操作(MySQL)
wks
2011/1/6镜像同步1 回复
订阅后,新回复会通过你的通知中心匿名送达。
1 条回复
replace into?
【 在 wks (cloverprince) 的大作中提到: 】
: 使用mysql。表如下:
: create table status (
: id int not null primary key auto increment,
: ...................