2014-12-26 65 views
1

我想在mysql中刪除我的行的一部分。像這樣:從數據庫中刪除一部分行(數據庫行中的一組值)

我行:1,2,3,4

我想讓它:1,2,3

我該怎麼辦呢?

我知道這可以用這段代碼完成,但有沒有更好的方法?

UPDATE Table SET message = REPLACE(message, ",4", "") WHERE id = 1;

+0

值1,2,3,4在一列中對嗎? –

+0

這會很好,如果你更新列例如UPDATE表SET消息=「1,2,3」WHERE id = 1; – Trushali

+0

'UPDATE Table SET message =「1,2,3」WHERE id = 1;' 試試這個...... –

回答

3

所以這將工作。

UPDATE Table SET message = CASE 
WHEN message LIKE '4,%' 
THEN // enter code here to replace '4,' in message with '' 
WHEN message LIKE '%,4,%' 
THEN // enter code here to replace ',4,' in message with ',' 
WHEN message LIKE '%,4' 
THEN // enter code here to replace ',4' in message with '' 
ELSE // this means all other occurances of 4 like 14,41,44,etc do nothing here or skip this else condition 
END; 
0

出於安全fistly您在測試服務器上測試代碼。

,如果你要換,4,MySQL將更換40太

必須更改字符串

,1,2,3,4,

,你必須更換,4,

+0

這就是爲什麼他在WHERE條件下使用ID的原因:) –