【发布时间】:2014-11-11 17:37:00
【问题描述】:
我有一张包含这些数据的表格
mysql> SELECT * FROM prueba;
+----+------+------+------+
| id | U1 | U2 | cant |
+----+------+------+------+
| 2 | U123 | U124 | 2 |
+----+------+------+------+
每次执行该语句时,我都需要将 'cant' 增加 1。 但如果该行不存在,则必须创建它。 U1 和 U2 列是数据对,这意味着 U1,U2 对是唯一的,这就是搜索记录是否存在的条件。
这是我到目前为止所做的。
select U1,U2,cant,
(
case when exists (select U1,U2 from prueba where U1='U123'
and U2='U124')
then 1
else 0
end
) as tmp
from prueba
【问题讨论】:
-
MAX(cant) + 1这样的东西怎么样?
标签: mysql insert case increment