我有一個包含所有類型參數的表。 表的結構是:ID,OBJECT_ID,PARAM_NAME,param_value非重複鍵上的MySQL在非唯一字段
下面的代碼工作,但它不是追加更新它們的結果。 事實是,我不能使用ON DUPLICATE KEY,因爲我的領域是非的唯一身份(除了ID當然)
INSERT INTO `params_table` (`object_id`, `param_name`, `param_value`)
SELECT
A.id AS my_object_id,
'XYZ' AS my_param_name,
IF(TMP.abc IS NULL,0,1) AS my_param_value
FROM
ref_table AS A
LEFT JOIN tmp_table AS TMP ON TMP.abc = A.abc
ON DUPLICATE KEY
UPDATE `param_value` = IF(TMP.abc IS NULL,0,1);
您可以創建新的索引? – 2011-04-28 09:53:55
是的,我已經有一個索引object_id – Kounisher 2011-04-28 09:55:26
如果你想更新記錄而不是插入一個新的記錄,這意味着它在某種程度上是唯一的,也許你需要一對列(object_id,param_name)上的唯一鍵? – piotrm 2011-04-28 09:59:23