PHP MYSQL 更新(如果存在)或插入(如果不存在)?
2022-08-30 08:34:32
我不知道这是否是正确的。我有一个类,如果字段当前存在,我想更新数据库,如果字段不存在,则插入。复杂的是,我正在连接3个表(set_colors,school_art,基本图像)
任何帮助都会很棒。
这是我所拥有的:
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;
}