0

我有一张桌子

CREATE TABLE IF NOT EXISTS `b_sale_order_props_value` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `ORDER_ID` int(11) NOT NULL,
  `ORDER_PROPS_ID` int(11) DEFAULT NULL,
  `NAME` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `VALUE` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `CODE` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`ID`),
  UNIQUE KEY `IX_SOPV_ORD_PROP_UNI` (`ORDER_ID`,`ORDER_PROPS_ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1921 ;

我查询

INSERT INTO `b_sale_order_props_value` 
    SET `ORDER_ID`=316, 
        `ORDER_PROPS_ID`=30, 
        `NAME`='ADDRES', 
        `VALUE`='Test addres' 
    ON DUPLICATE KEY UPDATE `NAME`='ADDRES',`VALUE`='Test addres';

返回错误

Duplicate entry '316-30' for key 'IX_SOPV_ORD_PROP_UNI'

如何解决这个问题?

4

1 回答 1

1

太大而无法作为评论发布 - 您的查询在这里有效:

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> CREATE TABLE IF NOT EXISTS `b_sale_order_props_value` (
    ->   `ID` int(11) NOT NULL AUTO_INCREMENT,
    ->   `ORDER_ID` int(11) NOT NULL,
    ->   `ORDER_PROPS_ID` int(11) DEFAULT NULL,
    ->   `NAME` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    ->   `VALUE` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
    ->   `CODE` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
    ->   PRIMARY KEY (`ID`),
    ->   UNIQUE KEY `IX_SOPV_ORD_PROP_UNI` (`ORDER_ID`,`ORDER_PROPS_ID`)
    -> ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1921 ;
Query OK, 0 rows affected (0.11 sec)

mysql> insert into b_sale_order_props_value (ORDER_ID, ORDER_PROPS_ID, NAME, VALUE, `CODE`) values (316, 30, 'replaceme', 'replaceme', 'replaceme');
Query OK, 1 row affected (0.42 sec)

mysql> INSERT INTO `b_sale_order_props_value`
    ->     SET `ORDER_ID`=316,
    ->         `ORDER_PROPS_ID`=30,
    ->         `NAME`='ADDRES',
    ->         `VALUE`='Test addres'
    ->     ON DUPLICATE KEY UPDATE `NAME`='ADDRES',`VALUE`='Test addres';
Query OK, 2 rows affected (0.04 sec)

mysql> select * from b_sale_order_props_value;
+------+----------+----------------+--------+-------------+-----------+
| ID   | ORDER_ID | ORDER_PROPS_ID | NAME   | VALUE       | CODE      |
+------+----------+----------------+--------+-------------+-----------+
| 1921 |      316 |             30 | ADDRES | Test addres | replaceme |
+------+----------+----------------+--------+-------------+-----------+
1 row in set (0.00 sec)
于 2012-08-01T14:17:54.487 回答