在重复密钥更新 - 多列 [英] On Duplicate Key Update - Multiple Columns

查看:158
本文介绍了在重复密钥更新 - 多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在重复键更新中使用 insert ...时,更新多个列的语法是什么?

When using insert... on duplicate key update, what is the syntax to update multiple columns?

INSERT INTO table1 (col1, col2, col3, col4) VALUES (’$val1’, ‘$val2’, ‘$val3’, ‘$val4’)
ON DUPLICATE KEY UPDATE col2=‘$val2’, col3=‘$val3’, col4=‘$val4’ // <-- not sure

更新:我在PHP中使用。因为这是一个语法问题,所以非常相关。

Update: I am using this within PHP. Since this is a syntax question, it very relevant.

$result = mysql_query("INSERT INTO table1 (col1, col2, col3, col4) 
                         VALUES (’$val1’, ‘$val2’, ‘$val3’, ‘$val4’)
                         ON DUPLICATE KEY UPDATE (col2=‘$val2’, col3=‘$val3’, col4=‘$val4’)")

更新。

推荐答案

INSERT INTO table1
  (`col1`, `col2`, `col3`, `col4`)
VALUES
  ('val1', 'val2', 'val3', 'val4')
ON DUPLICATE KEY UPDATE
  `col2`='val2',
  `col3`='val3', [...]


$ b b

我固定了您的报价和记号。

I fixed your quotes and tickmarks.

编辑

在PHP中:

$result = mysql_query("
     INSERT INTO table1
         (col1, col2, col3, col4)
     VALUES
         ('" . $val1 . "',  '" . $val2 . "', '" . $val3 . "', '" . $val4 . "')
     ON DUPLICATE KEY UPDATE
         col2='" . $val2 . "',
         col3='" . $val3 . "',
         col4='" . $val4 . "'"
 );

请注意,值由单引号括起来'。如果值是数字类型(INT,FLOAT等),则可以删除这些引号。只要不使用 count 类型或<$列名称,列名称就可以选择反引号c $ c> table 。

Note that the values are surrounded by single quotation marks '. If the values are a number type (INT, FLOAT, etc) you can drop those quotation marks. Backticks are optional around the column names as long as you are not using column names like count, type, or table.

在PHP示例中,字符串连接用于清楚地分离变量。

In the PHP example, string concatenation is used to clearly separate out the variables.

这篇关于在重复密钥更新 - 多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆