简体   繁体   English

MySQL REGEXP完全相同的字符串

[英]Mysql REGEXP exact same string

I am trying to fetch all those results with same exact string, but the query below is returning results matching all strings having Cars, but I need only results which match exact to Cars 我正在尝试使用完全相同的字符串获取所有这些结果,但是下面的查询返回的结果与所有具有Cars的字符串匹配,但我只需要与Cars完全匹配的结果

SELECT i.name
FROM table_name i
WHERE i.name REGEXP '[[:<:]]Cars[[:>:]]'

I thought [[:<:]] [[:>:]] would restrict the result to having only exact value 我以为[[:<:]] [[:>:]]会将结果限制为具有精确值

EDIT: 编辑:

MY bad I didnt mention, the above is just an example, Instead of just cars there will be multiple strings, like 我没有提到我的不好,上面只是一个例子,不仅仅是汽车,还会有多个字符串,例如

SELECT i.name
    FROM table_name i
    WHERE i.name REGEXP '[[:<:]]Cars[[:>:]].[[:<:]]Movies[[:>:]].[[:<:]]Camping[[:>:]]''

There will be multiple strings to match depending on the conditions. 将根据条件匹配多个字符串。 I am trying Regexp because else I will have to explicitly declare LIKE without % for all strings. 我正在尝试Regexp,因为否则我将必须为所有字符串显式声明LIKE,而无需%。

The Only reason I am using REGEXP is because I can pass my strings at once by concatenating, which is not possible using LIKE as much as I know 我使用REGEXP的唯一原因是因为我可以通过串联一次传递我的字符串,就我所知,使用LIKE是不可能的

My table

**ColumnName**
Cars
Racing Cars
Movies
Romantic Movies

By using the above query I get all results related to cars and movies, instead I only need the exact match 通过使用上面的查询,我可以获得与汽车和电影有关的所有结果,相反,我只需要完全匹配

Cars & Movies only 仅限汽车和电影

Why to use Regex when you want the exact match. 当您需要完全匹配时,为什么要使用正则表达式。 Simply try this 只需尝试一下

SELECT i.name
FROM table_name i
WHERE i.name = 'CARS'

or 要么

SELECT i.name
FROM table_name i
WHERE i.name like '%CARS%'

Remove the left or right wild character % as per your string and pattern 根据您的字符串和样式删除左或右百搭字符%

EDIT:- 编辑:-

So as per your edit, if you want to match only Cars and Movies then you can write like this: 因此,根据您的修改,如果只想匹配汽车和电影,则可以这样编写:

SELECT i.name
FROM table_name i
WHERE i.name IN ('CARS','MOVIES')

@Deepanshu, I see that you use REGEXP, perhaps because your implementation limits you in a way that you give as a parameter to a function the whole string that will be used in a REGEXP expression. @Deepanshu,我看到您使用了REGEXP,也许是因为您的实现限制了您将REGEXP表达式中使用的整个字符串作为参数提供给函数的方式。 [[:<:]] and [[:>:]] match the beginning and the end of a certain matching word and not the whole string. [[:<:]][[:>:]]匹配某个匹配单词的开头和结尾,而不匹配整个字符串。 So, you want to use ^ and $ respectively and the exact query you will use is : 因此,您要分别使用^$ ,将使用的确切查询是:

SELECT i.name
FROM table_name i
WHERE i.name REGEXP '(^Cars$)|(^Movies$)|(^Camping$)'

However, since I notice a lot of people keep on using mySQL REGEXP for straight equality comparison, this is not a good practice and I suggest changing your implementation so that it uses LIKE ,equality or something similar. 但是,由于我注意到很多人一直在使用mySQL REGEXP进行直接相等比较,所以这不是一个好习惯,因此我建议更改您的实现,使其使用LIKE ,equality或类似的东西。 You could also produced dynamically a LIKE statement using a programming language. 您还可以使用编程语言动态生成LIKE语句。

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

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