简体   繁体   English

带有多个分隔符的SUBSTRING_INDEX

[英]SUBSTRING_INDEX with multiple delimiters

I am trying to remove some information off urls that are placed into my database. 我试图删除放入我的数据库的URL中的一些信息。 I have this query that I am using: 我有这个查询,我正在使用:

Select substring_index(refurl,'?gclid',1) as refurl, Count(*)
from leads
group by substring_index(refurl,'?gclid',1)

But for the delimiter I really need to take off both of these: 但对于分隔符,我真的需要取消这两个:

?gclid or &gclid 

Is this possible by doing an OR statement within the substring or is it something completely different to get this done? 这是通过在子字符串中执行OR语句还是完全不同来实现这一点吗?

使用IF

SELECT SUBSTRING_INDEX(refurl, IF(LOCATE('?gclid', refurl), '?gclid', '&gclid'), 1) AS refurl, ...

Just in case, I just needed to group strings before a certain number, ex: 为了以防万一,我只需要在一定数量之前对字符串进行分组,例如:

hcu
----------------------
AQP Casma 01 HCU 200
AQP Casma 10 HCU 1500
AQP Casma 11 HCU 1500
Cusco 1 HCU 1500
Cusco 2 HCU 200-3
Cusco 3 HCU 200-1
Cusco 12 HCU 200

to obtain only this: 只获得这个:

AQP Casma
Cusco

I couldn't use IF because the multiple delimiters, so the query that I've used is: 我无法使用IF因为多个分隔符,所以我使用的查询是:

SELECT 
 CASE 
    WHEN LOCATE(' 0',hcu )>0 THEN SUBSTRING_INDEX(hcu, '0', 1)
    WHEN LOCATE(' 1',hcu )>0 THEN SUBSTRING_INDEX(hcu, '1', 1)  
END hub  
FROM npi_hist 
GROUP BY hub 

And the result was the expected... I don't know why is grouping correctly all the Cusco registers, but It's working as I needed. 结果是预期的...我不知道为什么正确分组所有库斯科寄存器,但它正在我需要的工作。

Hope it could help. 希望它可以帮助。

Bye. 再见。

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

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