简体   繁体   中英

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:

> 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)?

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/"

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.

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