mysql 单个if,MySQL如何在单个查询中执行if是否存在增量

I just wish to have a table to store a number of events by day.

Given a table:

create table totals (

entryday date,

total int(11) default 0 not null,

primary key (entryday) );

How can I write a simple query which increments, but creates an necessary?

I tried this - but it is not incrementing (it remains at 1):

REPLACE totals SET total = total + 1, entryday = "08-01-11"

Obviously this could be done in 2 queries quite simply, but it's through JDBC calls and may be called many times, so 1 query would be better.

解决方案

You probably want ON DUPLICATE KEY:

INSERT INTO totals (entryday, total)

VALUES ("08-01-11", 1)

ON DUPLICATE KEY UPDATE total = total + 1

That'll set the "08-01-11" total to 1 if a row doesn't already exist for that date and increment the total by 1 if it does.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值