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

时间:2015-01-26 08:34:16

标签: mysql

我有一张这样的表:

> 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

我想知道我是否可以使用单个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

是否可以在SQL(MySQL)中编写它?

感谢。

1 个答案:

答案 0 :(得分:0)

例如:

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