我不知道這是否是遠程正確的。我有一個類,我想更新數據庫,如果字段存在或插入,如果他們不。複雜的是,我正在做一個加入3桌(set_colors,school_art,baseimage)PHP MYSQL更新如果存在或插入,如果不是?
任何幫助將是非常好的。
以下是我有:提前
public function set_layer_colors($value) {
global $db;
$result_array = mysql_query("
IF EXISTS(SELECT * FROM set_colors WHERE school_art_id = '{$value}')
UPDATE set_colors (school_art_id, baseimage_id, sub_folder, layer)
SELECT school_art.id, baseimage.id, baseimage.sub_folder, baseimage.layer
FROM school_art
JOIN baseimage ON baseimage.base_folder = school_art.series_code
WHERE baseimage.image_type = 'B' ORDER BY school_art.id
ELSE
INSERT INTO set_colors (school_art_id, baseimage_id, sub_folder, layer)
SELECT school_art.id, baseimage.id, baseimage.sub_folder, baseimage.layer
FROM school_art
JOIN baseimage ON baseimage.base_folder = school_art.series_code
WHERE baseimage.image_type = 'B' ORDER BY school_art.id
");
return $result_array;
}
感謝。
爲了更加正確,您不需要指示主鍵。因此,如果field1是密鑰,那麼 ...... 在重複密鑰更新 field2 ='value2',field3 ='value3',...(以field2開頭) – giacoder