如果存在PHP MYSQL UPDATE或不存在INSERT?

我不知道这是否真的正确。我有一个班级,如果这些字段当前存在,我想在其中更新数据库,如果不存在则要插入。复杂的是,我正在联接3个表(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', ...

以上是 如果存在PHP MYSQL UPDATE或不存在INSERT? 的全部内容, 来源链接: utcz.com/qa/398061.html

回到顶部