Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 选择包含给定字符(SQL)特定数字的项_Mysql_Sql - Fatal编程技术网

Mysql 选择包含给定字符(SQL)特定数字的项

Mysql 选择包含给定字符(SQL)特定数字的项,mysql,sql,Mysql,Sql,对于这样的表: visits url 594 http://example.com/page1/stuff 302 http://example.com/page2 494 http://example.com 202 http://example.com/page3/stuff/more 我只想选择包含4个/字符的URL 在python中,我将使用len(url.split('/')==5。如何在SQL查询中实现这一点?这种搜索无法编制索引,因此无法对其进行

对于这样的表:

visits  url
594     http://example.com/page1/stuff
302     http://example.com/page2
494     http://example.com
202     http://example.com/page3/stuff/more
我只想选择包含4个
/
字符的URL


在python中,我将使用
len(url.split('/')==5
。如何在SQL查询中实现这一点?

这种搜索无法编制索引,因此无法对其进行优化。一种方法是使用正则表达式:

WHERE url REGEXP '^([^/]*/){4}[^/]*$'
说明:

  • ^
    =字符串的开头
  • [^/]*/
    =0个或多个非
    /
    字符,后跟
    /
  • (…){4}
    =括号内的图案正好重复4次
  • [^/]*
    =另一个非
    /
    字符序列
  • $
    =字符串结尾

它们也可以是连续的,中间没有字符吗?例如:
http://example.com///
当然可以-因此在您的示例中,我们将计算5长度(url)-len(替换(url,“/”,“)=4?这是有效的。这个表有O(10^8)行,所以速度在这里很重要,如果你能找到任何更快的东西,请分享。干杯(顺便说一句,我认为你的
leng
应该是
length
)没有快速的方法,它不能被索引。