Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same character 3 times condition in mysql

Tags:

regex

mysql

I want a query (in msyql) to get records with same character 3 times:

aa => false
aaa => true
baaa => true
aaab => true
aaaaaab => true
baaab => true
babababa => false

For any character, not only 'a' and 'b'.

The following sentence doesn't work:

SELECT field1
FROM mytable
WHERE field1 REGEXP '(.)\1\1';
like image 413
barroco Avatar asked Aug 22 '11 20:08

barroco


People also ask

How do I get the last 3 characters of a string in SQL?

It could be this using the SUBSTR function in MySQL: SELECT `name` FROM `students` WHERE `marks` > 75 ORDER BY SUBSTR(`name`, -3), ID ASC; SUBSTR(name, -3) will select the last three characters in the name column of the student table.

Can MySQL use RegEx?

MySQL supports another type of pattern matching operation based on the regular expressions and the REGEXP operator. It provide a powerful and flexible pattern match that can help us implement power search utilities for our database systems.

How can use like operator in MySQL for multiple values?

SELECT COUNT(email) as count FROM table1 t1 JOIN ( SELECT company_domains as emailext FROM table2 WHERE company = 'DELL' ) t2 ON t1. email LIKE CONCAT('%', emailext) WHERE t1. event='PC Global Conference"; Task was count participants at an event(s) with filter if email extension equal to multiple company domains.


1 Answers

May be this can help you.

select * from test1 where name REGEXP  '[a]{3}|[b]{3}|[c]{3}|[d]{3}|[e]{3}|[f]{3}|[g]{3}|[h]{3}|[i]{3}|[j]{3}|[k]{3}|[l]{3}|[m]{3}|[n]{3}|[o]{3}|[p]{3}|[q]{3}|[r]{3}|[s]{3}|[t]{3}|[u]{3}|[v]{3}|[w]{3}|[x]{3}|[y]{3}|[z]{3}';  

It returns records for 3 same and consecutive letters.

like image 154
Nishu Tayal Avatar answered Oct 11 '22 01:10

Nishu Tayal