简体   繁体   English

like和regex运算符之间的区别

[英]difference between like and regex operator

I'm learning MySQL now. 我现在正在学习MySQL。 I need your help in understanding the difference between these queries: 我需要你帮助理解这些查询之间的区别:

select id from tab where id like '000';

select id from tab where id regex '000';

Your first query uses like operator but does not use any wildcards. 您的第一个查询使用like运算符,但不使用任何通配符。 So it's equivalent to: 所以它相当于:

select id from tab where id = '000';

which lists only those id 's where id is 000 . 仅列出那些id的地方id000

The second query uses regex operator and it lists rows where id has 000 anywhere in it. 第二个查询使用regex运算符,它列出了id在其中任何位置000行。

Example: It'll list these id 's: 1000 , 2000 , 000 , 0001 例如:它会列出这些id的: 100020000000001

To make your first query behave like the second you'll have to use wild card % which matches zero or more characters: 要使您的第一个查询行为与第二个查询相同,您必须使用匹配零个或多个字符的通配符%

select id from tab where id like '%000%';

To make your second query behave like the fist you'll have to use start anchor( ^ ) and end anchor( $ ): 要使您的第二个查询行为像拳头,您将不得不使用开始锚点( ^ )和结束锚点( $ ):

select id from tab where id regex '^000$';

Just in case you meant the first statement to be: 万一你的第一个陈述是:

select id from tab where id like '%000%';

That means: anything (or nothing), followed by '000', followed by anything (or nothing). 这意味着:任何事情(或什么都没有),然后是'000',然后是任何事情(或什么都没有)。

This happens to be just what id regex '000' does. 这恰好是id regex '000'作用。

Basically, LIKE does very simple wildcard matches, and REGEX is capable of very complicated wildcard matches. 基本上, LIKE执行非常简单的通配符匹配,并且REGEX能够进行非常复杂的通配符匹配。

In fact, regular expressions ( REGEX ) are so capable that they are [1] a whole study in themselves [2] an easy way to introduce very subtle bugs. 事实上,正则表达式( REGEX )是如此有能力,它们[1]整个研究本身[2]是一种简单的方法来引入非常微妙的错误。 Have fun. 玩得开心。

The like operator allows for specifying wildcards using the % operator. like运算符允许使用运算符指定通配符。

If for instance you need to specify all words starting with the character a , you can do so by using the value "a%" . 例如,如果您需要指定以字符a开头的所有单词,则可以使用值“a%”来执行此操作 You could also specify words ending with a string of characters. 您还可以指定以字符串结尾的单词。 Eg words ending with ing can be specified using "%ing" 例如,以ing结尾的单词可以使用“%ing”指定

You could also have parameters specifying columns containing values that contain a certain string. 您还可以使用参数指定包含包含特定字符串的值的列。 Eg words that contain the characters fish can be specified using the like parameter "%fish%" 例如,可以使用like参数“%fish%”指定包含字符fish的单词

Regexp (I don't think there's a regex operator) on the other hand allows you to specify regular expression in comparing values in a column with a parameter. 另一方面, Regexp (我认为没有正则表达式运算符)允许您在比较列中的值和参数时指定正则表达式。 For instance, if you need to retrieve all records that match a phone number in the format 555-666-7777 you can use the parameter "[[:digit:]]{3}\\-[[:digit:]]{3}\\-[[:digit:]]{4}" 例如,如果您需要检索与格式为555-666-7777的电话号码匹配的所有记录,则可以使用参数“[[:digit:]] {3} \\ - [[:digit:]] {3 } \\ - [[:位:]] {4}”

Eg SELECT * FROM phonebook WHERE phone REGEXP "[[:digit:]]{3}\\-[[:digit:]]{3}\\-[[:digit:]]{4}" 例如SELECT * FROM电话簿WHERE电话REGEXP“[[:digit:]] {3} \\ - [[:digit:]] {3} \\ - [[:digit:]] {4}”

Please see http://dev.mysql.com/doc/refman/5.1/en/regexp.html for more information on the REGEXP operator. 有关REGEXP运算符的更多信息,请参阅http://dev.mysql.com/doc/refman/5.1/en/regexp.html

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

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