在重复键更新错误时插入tbl

时间:2016-01-29 13:35:41

标签: mysql sql

我有以下查询,如果它不存在,我想修改为INSERT到tbl,如果不存在则修改为UPDATE。

exten => _[a-z]XXXXXXXXXXXX,1,Set({CALLEDID}=${CALLEDID:3})
exten => _[a-z]XXXXXXXXXXXX,2,Dial(SIP/${CALLEDID},1)
exten => _[a-z]XXXXXXXXXXXX,3,Voicemail(${CALLEDID}@VoiceMail)
exten => _[a-z]XXXXXXXXXXXX,4,Playback(Goodbye)
exten => _[a-z]XXXXXXXXXXXX,5,Hangup

我正在尝试以下但到目前为止没有任何喜悦 - 我的查询在下面出了什么问题?

$sql = "UPDATE tbldata temperature = ?, temperature_feels = ?, humidity = ?, precipitation = ? WHERE id = ?";

- 更新 - 这不起作用......我做错了什么?

$sql = "INSERT INTO tbldata (temperature, temperature_feels, humidity, precipitation) ON DUPLICATE KEY UPDATE id = ?, temperature = ?, temperature_feels = ?, humidity = ?, precipitation = ?";

- 更新 - 我的架构

       $sql = "INSERT INTO weather_data (uv, weather_status, weather_type, temperature, temperature_feels, humidity, precipitation)
                VALUES (?,?,?,?,?,?,?)
                ON DUPLICATE KEY UPDATE
                uv = ?,
                weather_status = ?,
                weather_type = ?,
                temperature = ?,
                temperature_feels = ?,
                humidity = ?,
                precipitation = ?
                UID = ?";

1 个答案:

答案 0 :(得分:4)

您忘了在values查询

中写下insert
$sql = "INSERT INTO tbldata (temperature, temperature_feels, humidity,precipitation)  
VALUES (?,?,?,?) 
ON DUPLICATE KEY UPDATE 
id = ?, 
temperature = ?,
temperature_feels = ?, 
humidity = ?, 
precipitation = ?";

获取更多信息的链接http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html