我在名为websites
的列中有一条记录,其中包含以下文字:
["https://twitter.com/use312", "https://youtube.com/bel", "http://keepthiswebsite"]
我想删除包含youtube.com
和twitter.com
的所有网站,即"https://twitter.com/use312"
和"https://youtube.com/bel"
,但保留所有其他子串("http://keepthiswebsite"
)
因此上面记录中mysql语句的结果应为["http://keepthiswebsite"]
。
我真的很想用mysql做这个,没有python或其他一些脚本语言。我怎么能这样做?
答案 0 :(得分:1)
这是你要找的东西,但它不是很好,你必须在每一行都有一个唯一的id。
SELECT url ,CONCAT('[', result_url,']') as result_url
FROM (
SELECT u.url,
@r:= TRIM(BOTH ' ' FROM REPLACE(REPLACE(REVERSE(SUBSTRING_INDEX(REVERSE(SUBSTRING_INDEX(CONCAT(url,',,,,,,,,,,'), ',', ids.id)),',',1)),'[',''),']','')),
@r:= IF(@r = '' , NULL, @r),
@r:= IF(INSTR( @r ,'youtube.com') > 0 , NULL, @r) ,
@r:= IF(INSTR( @r ,'twitter.com') > 0 , NULL, @r) ,
-- Here you can add your own removes. only duplicate last line
GROUP_CONCAT( @r SEPARATOR ', ' ) as result_url
FROM (
SELECT 1 as id UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5
UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 ) as ids
CROSS JOIN `urlstable` u
CROSS JOIN ( SELECT @r:='') as init
GROUP BY u.id
ORDER BY u.id,ids.id
) as remove;
<强>样品强>
mysql> SELECT * FROM urlstable;
+----+--------------------------------------------------------------------------------------+
| id | url |
+----+--------------------------------------------------------------------------------------+
| 1 | ["https://twitter.com/use312", "https://youtube.com/bel", "http://keepthiswebsite"] |
| 2 | ["https://twitterr.com/use312", "https://youtube.com/bel", "http://keepthiswebsite"] |
| 3 | ["https://twitterr.com/use312", "https://google.com/bel", "http://keepthiswebsite"] |
| 4 | ["https://mydomain.de/use312", "https://youtube.com/bel", "http://keepthiswebsite"] |
+----+--------------------------------------------------------------------------------------+
4 rows in set (0,00 sec)
mysql> SELECT url ,CONCAT('[', result_url,']') as result_url
-> FROM (
-> SELECT u.url,
-> @r:= TRIM(BOTH ' ' FROM REPLACE(REPLACE(REVERSE(SUBSTRING_INDEX(REVERSE(SUBSTRING_INDEX(CONCAT(url,',,,,,,,,,,'), ',', ids.id)),',',1)),'[',''),']','')),
-> @r:= IF(@r = '' , NULL, @r),
-> @r:= IF(INSTR( @r ,'youtube.com') > 0 , NULL, @r) ,
-> @r:= IF(INSTR( @r ,'twitter.com') > 0 , NULL, @r) ,
-> -- Here you can add your own removes. only duplicate last line
-> GROUP_CONCAT( @r SEPARATOR ', ' ) as result_url
-> FROM (
-> SELECT 1 as id UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5
-> UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10 ) as ids
-> CROSS JOIN `urlstable` u
-> CROSS JOIN ( SELECT @r:='') as init
-> GROUP BY u.id
-> ORDER BY u.id,ids.id
-> ) as remove;
+--------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------+
| url | result_url |
+--------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------+
| ["https://twitter.com/use312", "https://youtube.com/bel", "http://keepthiswebsite"] | ["http://keepthiswebsite"] |
| ["https://twitterr.com/use312", "https://youtube.com/bel", "http://keepthiswebsite"] | ["https://twitterr.com/use312", "http://keepthiswebsite"] |
| ["https://twitterr.com/use312", "https://google.com/bel", "http://keepthiswebsite"] | ["https://twitterr.com/use312", "https://google.com/bel", "http://keepthiswebsite"] |
| ["https://mydomain.de/use312", "https://youtube.com/bel", "http://keepthiswebsite"] | ["https://mydomain.de/use312", "http://keepthiswebsite"] |
+--------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------+
4 rows in set (0,00 sec)
mysql>