简体   繁体   English

MySQL-根据VARCHAR字段中的模式计数出现次数?

[英]MySQL - Count the number of occurrences according to pattern in a VARCHAR field?

I have a table like this: 我有一张这样的桌子:

> URL          
> ------------------------------------------------ 
> http://www.example.com 
> http://www.example.com/one
> http://www.example.com/one/two/abc.txt 
> http://www.example.com/three/four
> http://www.example.com/five/six/seven/def.txt

I wonder if I can use a single SQL query which could count the occurrences of the URL pattern and output the result as below: 我想知道是否可以使用单个SQL查询来计算URL模式的出现并输出如下结果:

> URL PATTERN                                   |   COUNT
> -------------------------------------------------------------------------- 
> http://www.example.com/                       |     5
> http://www.example.com/one/                   |     2
> http://www.example.com/one/two/               |     1 
> http://www.example.com/three/                 |     1
> http://www.example.com/three/four/            |     1
> http://www.example.com/five/                  |     1
> http://www.example.com/five/six/              |     1
> http://www.example.com/five/six/seven/        |     1

Is it possible to write it in SQL (MySQL)? 可以用SQL(MySQL)编写吗?

Thanks. 谢谢。

for example : 例如 :

update table_b set 
    count = (select count(url) 
    from table_a where url like "http://www.example.com/%") 
    where url_pattern = "http://www.example.com/"

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

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