MySQL 将 +1 添加到字段(命中计数器)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4214419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:44:19  来源:igfitidea点击:

add +1 to field (hit counter)

mysql

提问by acctman

how do I add a +1 too the c_request field. every time I do and insert I want to add a 1 to the current number (ex. like a hit counter)

我如何在 c_request 字段中添加一个 +1。每次我做并插入时,我都想在当前数字上加一个 1(例如,像一个计数器)

mysql_query("INSERT INTO ed_names (com_id, c_date, c_time, c_type, c_request, c_by)
        VALUES ($id, CURRENT_DATE, CURRENT_TIME, '.($type == 'normal' ? 1 : 2).',0,$user)");     

$rid = mysql_insert_id();

回答by tbleckert

mysql_query("UPDATE ed_names SET c_request = c_request+1 WHERE id = 'x'");

回答by Kennethvr

use update if you want to add to an existing, if not, just enter 1

如果要添加到现有,请使用更新,否则,只需输入 1

INSERT INTO ed_names (com_id, c_date, c_time, c_type, c_request, c_by)
    VALUES ($id, CURRENT_DATE, CURRENT_TIME, '.($type == 'normal' ? 1 : 2).',1,$user) 

if you want to update you can do

如果你想更新你可以做

update ed_names set c_date = CURRENT_DATE, C_time = CURRENT_TIME, c_type = '.($type == 'normal' ? 1 : 2).''.($type == 'normal' ? 1 : 2).', c_request = c_request + 1, c_by = $user where com_id = $id