[SQL ] 請問if() insert else update
請問如果我想把一個人(id=8888)加入資料庫裡面
但是我不知道是不是以前已經有過他的資料
所以我先查一下是否有這人select count(*)
如果沒有的話(@result=0)
就insert
如果已經有就update
代碼如下,mysql
-------------------------------------------
set @result=(select count(*) from user_profile where id='8888');
if @result= 0
then (insert user_profile (p_length,p_weight) values ('180','70'));
else (update user_profile set p_length='180' where p_weight='70');
end if
-------------------------------------------
但是一直出現錯誤 Error Code : 1064
語法錯誤,但是找遍書籍和網路,找不到解法。
請問有人知道錯在哪裡嗎?
----
剛爬文之後成功。
insert user_profile set p_length='180',p_weight='70',id='8888'
ON DUPLICATE KEY UPDATE p_length='180',p_weight='70';
但是if...else到底錯在哪裡??
想不通。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 117.32.174.185
※ 編輯: imntu 來自: 117.32.174.185 (08/18 01:18)
討論串 (同標題文章)