PHP MYSQL更新如果存在或插入,如果不是?
我不知道这是否是远程正确的。 我有一个类,我想更新数据库,如果字段目前存在或插入,如果他们不。 复杂的是,我正在做一个join3个表格(set_colors,school_art,baseimage)
任何帮助将是非常好的。
这是我有什么:
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;
}
提前致谢。
我相信你正在寻找下面的语法
INSERT INTO <table> (field1, field2, field3, ...) VALUES ('value1', 'value2','value3', ...) ON DUPLICATE KEY UPDATE field1='value1', field2='value2', field3='value3', ...
两个select:
MySQL手册:: INSERT INTO … ON DUPLICATE KEY UPDATE语法
要么
MySQL手册:: REPLACE INTO语法
两者都将允许您在一个查询中执行insert-or-update-if-not-exists。