2016-07-27 58 views
0

如何刪除逗號分隔字符串 「0000-00-00」如何在MySQL中刪除逗號分隔的記錄字符串?

ID  Name Return Date 
    1  A  0000-00-00,2016-02-1,2016-1-15 
    2  B  0000-00-00,2016-04-1 
    3  c  0000-00-00,2016-04-4 

期待答案

ID  Name Return Date 
    1  A  2016-02-1,2016-1-15 
    2  B  2016-04-1 
    3  c  2016-04-4 
+0

他們是字符串。你使用標準的字符串操作/函數:http://dev.mysql.com/doc/refman/5.7/en/string-functions.html如果你的數據庫已經正確的標準化,你不需要問這個問題。它會是一個簡單的'delete .. .where date ='0000-00-00''查詢。 –

+0

'SUBSTRING(\'Return Date \'FROM 11);' –

+2

這裏真正的解決方案是不將多個值存儲在單個列中! –

回答

1

認爲你有三種情況:左0000-00-00串,右和中間:

+------+------+--------------------------------+ 
| ID | Name | Return Date     | 
+------+------+--------------------------------+ 
| 1 | A | 0000-00-00,2016-02-1,2016-1-15 | 
| 2 | B | 0000-00-00,2016-04-1   | 
| 3 | C | 0000-00-00,2016-04-4   | 
+------+------+--------------------------------+ 

使用REPLACE功能:

SELECT `Return Date`, REPLACE(`Return Date`,'0000-00-00,','') as replaced 
FROM YourTable; 

+--------------------------------+----------------------+ 
| Return Date     | replaced    | 
+--------------------------------+----------------------+ 
| 0000-00-00,2016-02-1,2016-1-15 | 2016-02-1,2016-1-15 | 
| 0000-00-00,2016-04-1   | 2016-04-1   | 
| 0000-00-00,2016-04-4   | 2016-04-4   | 
+--------------------------------+----------------------+ 

你更新一句話是:

UPDATE YourTable 
SET `Return Date` = REPLACE(`Return Date`,'0000-00-00,','') 
WHERE `Return Date` like '%0000-00-00,%'; 

您必須對其他情況下,像'0000-00-00'在中間或右側做類似的查詢。

相關問題