[英]Remove everything after a plus in MySQL
I have a query in MySQL 我在MySQL中有一个查询
select various(functions(here(...))) foo, count(*) ct
from table
group by foo
having ct > 1;
which looks for duplicate data. 查找重复数据。 I would like to change the query to remove plus signs and anything following them from foo, so that if
various(functions(here(...)))
yields foo+bar
I get just foo
. 我想更改查询以从foo中删除加号和其后的所有内容,以便如果
various(functions(here(...)))
产生foo+bar
我得到的只是foo
。 (If a plus sign does not occur, it stays unchanged.) (如果没有出现加号,它将保持不变。)
What's the best way to do this? 最好的方法是什么? I can use
replace
我可以用
replace
select if(locate("+", various(functions(here(...))))>0, left(various(functions(here(...))), locate("+", various(functions(here(...)))) - 1), various(functions(here(...)))) foo, count(*) ct
from table
where conditions
group by foo
having ct > 1;
but this seems like 'obviously' the wrong thing. 但这似乎“显然”是错误的事情。 Regex would be nice but they don't exist in MySQL as far as I know.
正则表达式会很好,但据我所知它们在MySQL中不存在。 A subquery makes this slightly less unwieldy
子查询使这个不太麻烦
select if(locate("+", bar)>0, left(bar, locate("+", bar)-1), bar) foo
from table
left join (
select pkey, various(functions(here(...))) bar
from table
where conditions
) subtable using(pkey)
group by foo
having ct > 1
;
but as the table is large I'd like to know if there's a more efficient or more maintainable solution. 但是由于桌子很大,我想知道是否有更有效或更可维护的解决方案。
Use substring_index()
: 使用
substring_index()
:
select substring_index(various(functions(here(...))), '+', 1) as foo, count(*) ct
from table
group by foo
having ct > 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.