繁体   English   中英

如何在 MySQL 中删除逗号分隔的记录字符串?

[英]How to remove comma separated string of records in MySQL?

如何删除逗号分隔的字符串“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

期待答案

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

认为您有 3 种情况:左侧、右侧和中间的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'进行类似的查询。

您也可以尝试使用此方法将其他位置的值替换为:

Values may be:
+------------------------------+
0000-00-00,2016-02-1,2016-1-15 
2016-02-1,0000-00-00,2016-1-15 
2016-02-1,2016-1-15,0000-00-00
0000-00-00
+------------------------------+

UPDATE YourTable
SET `Return Date` = TRIM(BOTH ',' FROM
      REPLACE(
        REPLACE(CONCAT(',',REPLACE(`Return Date`, ',', ',,'), ','),',0000-00-00,', ''), ',,', ',')
    )
WHERE FIND_IN_SET('0000-00-00', `Return Date`)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM