how to replace a certain value of a column. for example I have three rows of values in a table:
id_tweet: 1
status_user: RT @username1: success rta: P
id_tweet: 2
status_user: RT @username2: happy rta: D
id_tweet: 3
status_user: amen
of the three records above I want to replace or remove retweet text in the status_user column to:
after replace:
id_tweet: 1
status_user: success rta: P
id_tweet: 2
status_user: happy rtx: D
id_tweet: 3
status_user: amen
but I have constraints. I think I can use the replace function query on mysql as follows:
UPDATE tes_tbl_status
SET status_user = (SELECT REPLACE (status_user, '?', '') FROM
tes_tbl_status WHERE status_user LIKE 'RT%:%')
WHERE status_user LIKE 'RT%:%';
information : '?' = RT @...:
query above by replacing a certain value in the status_user column with an empty string value, however I do not yet know how to retrieve the text value
RT @ ...:
which will be replaced with an empty string. anyone have any idea or solution for my problem?
If you want to remove always the RT @...:
parts, the SUBSTRING_INDEX
, CONCAT
and REPLACE
mysql functions are there for your help. Using SUBSTRING_INDEX
you can extract the RT @...
substring that you can CONCAT with a colon ( :
) and apply this result in the REPLACE
function as the from_str
parameter to be replaced by an empty string.
Here is the code what do the update for you:
UPDATE t
SET status_user = TRIM(REPLACE(status_user, CONCAT(SUBSTRING_INDEX(status_user, ':', 1),':'), ''))
WHERE status_user LIKE 'RT%';
Result :
+----------+----------------+
| id_tweet | status_user |
+----------+----------------+
| 1 | success rta: P |
| 2 | happy rta: D |
| 3 | amen |
+----------+----------------+
Note the TRIM
that i used to remove the leading and trailing spaces (if exist).
See sqlfiddle demo here .
DDL :
CREATE TABLE t (
id_tweet INT UNSIGNED NOT NULL,
status_user TEXT NOT NULL
);
INSERT INTO t (id_tweet, status_user) VALUES
(1, 'RT @username1: success rta: P'),
(2, 'RT @username2: happy rta: D'),
(3, 'amen');
Also there is a nice mysql udf library ( lib_mysqludf_preg ) that can equips your mysql with PCRE pattern matching functionalities.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.