有条件的mysqli ON DUPLICATE KEY UPDATE问题

时间:2016-10-27 14:09:02

标签: mysql sql

有人告诉我为什么ts_contact_scraped没有使用以下查询进行更新, 我想如果$ sqlContactNumber已经存在,并且如果$ sqlContactName不等于表中的相应名称,则更新s_contact_name和ts_contact_scraped。

latlng

1 个答案:

答案 0 :(得分:0)

对我来说这很简单但很奇怪,

刚刚互换

ts_contact_scraped = CASE WHEN s_contact_name <> VALUES(s_contact_name) 
THEN VALUES(ts_contact_scraped) ELSE ts_contact_scraped END

s_contact_name = CASE WHEN s_contact_name <> VALUES(s_contact_name) 
THEN VALUES(s_contact_name) ELSE s_contact_name END

最终代码是

$sqlTimestamp = date('c');
$sql="INSERT INTO `" . DB_TABLE . "` (`s_contact_name`, `s_contact_number`, `ts_contact_scraped`)
VALUES ('$sqlContactName','$sqlContactNumber','$sqlTimestamp') 
ON DUPLICATE KEY UPDATE 
ts_contact_scraped = CASE WHEN s_contact_name <> VALUES(s_contact_name) 
THEN VALUES(ts_contact_scraped) ELSE ts_contact_scraped END,
s_contact_name = CASE WHEN s_contact_name <> VALUES(s_contact_name) 
THEN VALUES(s_contact_name) ELSE s_contact_name END";
相关问题