简体   繁体   English

检查是否存在,如果是,则更新1 ++,如果不插入

[英]Check if exists, if so, update by 1++, if not insert

Hey guys quick question, I currently have an insert statement $query= "INSERT into new_mail VALUES ('$to1', '0')"; 嘿伙计们快速提问,我目前有一个插入语句$query= "INSERT into new_mail VALUES ('$to1', '0')"; where fields are username, and message_number Currently what I would do to check if the entry exists, is do a select query then check the number of rows with mysql_num_rows (php). 其中字段是用户名,而message_number目前我要做的是检查条目是否存在,是做一个select查询然后用mysql_num_rows (php)检查行数。 If rows==1 then I get the current message_number and set it equal to 如果rows == 1,那么我得到当前的message_number并将其设置为等于

$row['message_number']+1. 

Then I update that entry with another query. 然后我用另一个查询更新该条目。

Is there an easier way to do all this in just mysql with just one query (check if exists, if not insert, if so update message_number, increase by 1)? 是否有一种更简单的方法只用一个查询在mysql中完成所有这些(检查是否存在,如果不是插入,如果是这样更新message_number,增加1)?

根据表格的结构,您可以使用INSERTON DUPLICATE KEY UPDATE (链接到MySQL手册)功能:

INSERT into new_mail VALUES ('$to1', '0') ON DUPLICATE KEY UPDATE message_number=message_number+1

Use INSERT...ON DUPLICATE KEY UPDATE . 使用INSERT...ON DUPLICATE KEY UPDATE The MySQL manual has an example which does almost exactly what you need: MySQL手册有一个例子,几乎完全符合你的需要:

INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;

To make this work you need to add a UNIQUE index on the column that you use to check for duplicates. 要使其工作,您需要在用于检查重复项的列上添加UNIQUE索引。 There is one important warning though: 但有一个重要警告:

In general, you should try to avoid using an ON DUPLICATE KEY clause on tables with multiple unique indexes. 通常,您应该尽量避免在具有多个唯一索引的表上使用ON DUPLICATE KEY子句。

Got a little confused by your question and your table structures but I think you want something like this. 你的问题和你的桌面结构有点困惑,但我想你想要这样的东西。

INSERT INTO new_mail (username, message_number)
VALUES ($username, $message_number) 
ON DUPLICATE KEY UPDATE message_number=message_number + 1;

This is presuming username is your primary key (more likely something like userid). 这假设username是你的主键(更像是userid)。 Hope this helps. 希望这可以帮助。

EDIT: The ON DUPLICATE KEY UPDATE answers are better, but you could do this (eludes the select query): 编辑:ON DUPLICATE KEY UPDATE答案更好,但你可以这样做(逃避选择查询):

Assuming you're using the mysqli extenson: 假设你正在使用mysqli extenson:

$db = //Some construction of mysqli object;
$sql = 'UPDATE tablename SET RowValue = RowValue + 1 WHERE message_number = ?';
$updateStatement = $db->prepare($sql);
$updateStatement->bind_param('i', $message_number);
$message_number = //Set message number;
$updateStatement->execute();
if ($updateStatement->affectedRows == 0) {
    $sql = 'INSERT INTO tablename (RowValue,  message_number) VALUES (?, ?)';
    $insertStatement = $db->prepare($sql);
    $insertStatement->bind_param('ii', $rowValue, $messageNumber);
    $rowValue = something;
    $messageNumber = something;
    $insertStatement->execute();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM