I want to find usernames with reduplicated words in my database
Below is my SQL statement:
SELECT * FROM users WHERE BINARY `name` regexp 'a{2}|b{2}|c{2}|d{2}|e{2}|f{2}|g{2}|h{2}|i{2}|j{2}|k{2}|l{2}|m{2}|n{2}|o{2}|p{2}|q{2}|r{2}|s{2}|t{2}|u{2}|v{2}|w{2}|x{2}|y{2}|z{2}'
I guess there should be an easier way, but I don't know how to do it.
I also want to find usernames like 'ABAA' or 'AABA', but I won't write this SQL or say this regex
As MySQL's regex engine does not support backreferences, your approach is reasonable. To simplify your query, you could maintain a table of double letters:
table: letters (val)
aa
bb
...
zz
Now your query can be simplified to:
SELECT *
FROM users u
WHERE EXISTS (
SELECT 1
FROM letters l
WHERS INSTR(u.name, l.val) > 0
);
Try:
create table users(name varchar(100));
insert into users (name) values
("abaa"),
("bba"),
("abc"),
("aCCa");
SELECT * FROM users WHERE LOWER(name) REGEXP 'aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr|ss|tt|uu|vv|ww|xx|yy|zz';
Prints:
abaa
bba
aCCa
i think i found the answer,I upgraded my MySQL database to MariaDB.
this is my sql statement:
SELECT * FROM users WHERE `name` REGEXP '([a-z])\\1'
This is the sql statement to query like 'aaba':
SELECT * FROM users WHERE `name` REGEXP '([a-z])\\1((?!\\1)[a-z])\\1'
Note: Because MariaDB uses the C escape syntax in strings (for example, "\n" to represent the newline character), you must double any "" that you use in your REGEXP strings.
In MySQL 8.0
WHERE name REGEXP '([a-z])\\1'
Let the collation take care of ignore case.
Related
In 'plain' Regex this works for me:
\w+[A-Z][a-z]\w+
as per this example: https://regex101.com/r/VqsTXL/1
However doing the equivalent in Regexp fails to find CamelCase:
[[:<:]][A-Z][a-z][[:>:]]
as per this example: http://sqlfiddle.com/#!9/ad35d8/1
In MySQL version before 8, you can "emulate" \w using [[:alnum:]_]:
Select * from PatternTester where Binary Name regexp '[[:alnum:]_]+[A-Z][a-z][[:alnum:]_]+'
See the regex demo.
SQL:
Create Table PatternTester
(Name varchar(100));
Insert into PatternTester (Name)
values ('CamelCase'),
('Camelcase'),
('CAMELCASE'),
('CamelCase')
Select * from PatternTester where Binary Name regexp '[[:alnum:]_]+[A-Z][a-z][[:alnum:]_]+'
See SQL demo.
When I run the following on MySql:
SELECT * FROM Customers WHERE City LIKE '[acs]%';
I get the expected results.
However, the same query on mariadb returns an empty set. Am I doing something wrong?
I've looked at the docs and it seems like they want it to be more like
SELECT * FROM Customers WHERE City RLIKE '(a|c|s)'.
Is there a command that will work on both? Should I use REGEXP?
SELECT * FROM Customers WHERE City REGEXP 'a|c|s'
Yo can you the REGEXP function:
SELECT * FROM Customers WHERE City REGEXP '[acs]'
It is highly unlikely that this query returns results on MySQL:
SELECT *
FROM Customers
WHERE City LIKE '[acs]%';
Why? No city names that I know of have the character '['. And, if they did, it seems even less likely that they would have the character ']'.
You may be confusing MySQL with SQL Server. The latter has extended LIKE patterns to include character classes. The equivalent logic in MySQL is:
WHERE City REGEXP '^[acs]'
Or, if you prefer:
WHERE LEFT(City, 1) IN ('a', 'c', 's')
I want to to create a regex to find all columns that only have a single character ([A-Z]) as name, like N or M but not NM.
I've tried:
SELECT * FROM 'table' WHERE Name REGEXP '^[A-Z]'
But it's not displaying the expected result.
Try ^[A-Z]$.
You then state that this character is first and also last character of the value.
The regex functions in Oracle work only on one column. So, to search for just one character in a column, you would do the following:
select * from yourTable where REGEXP_LIKE (col1, '^[A-z]$');
Now, to search all the char/varchar columns on your table, you'll need to chain the regex expressions together, like so:
select * from yourTable where REGEXP_LIKE (col1, '^[A-z]$') or REGEXP_LIKE (col3, '^[A-z]$');
SQL solution:
where name in ('N','M')
I have a table in MySql, the table has 3 fields (name, date, number). The table connected to my app via PHP.
What I need is to make MySql to accept only English numbers. I don't want to do it via PHP, is there any way to do it in MySql to restrict it from accepting such as (Arabic, Parisian).
If there's any options can I do it from MySql.
Thanks
You can verify that easily with a regex,
SELECT * FROM yourTable WHERE number REGEXP '^[0-9]*$';
Edit : If your goal is only to block Arabic text, then here a regex that would select only ASCII character from the space to the tild(0-127).
SELECT * FROM yourTable where number REGEXP '^[ -~]*$'
I have a table, such as
create table table1(
name varchar(32),
);
And there's some data in it. When I select like this:
select * from table1 where name like 'Jack2%';
there will be Jack2.
But if I select like this:
select * from table1 where name like 'Jack[0-9]%';
there will be nothing;
And I also tried regexp to subsitute like, but it also didn't work!
What's wrong?
You've confused two different pattern-matching mechanisms. SQL LIKE uses % to match anything and _ to match any single character; it does not have anything like [0-9] to match a digit. That looks like a character class from a regular expression.
Standard SQL has no support for regular expressions at all, but MySQL does - you just have to use RLIKE (or REGEXP, but that doesn't read as nicely IMO) instead of LIKE. But that means that you have to replace the % with the regular-expression equivalent .*, too.
SELECT * FROM table1 WHERE name RLIKE 'Jack[0-9].*';
Fiddle
MySQL REGEX
select * from Table1 where `name` REGEXP 'Jack[0-9]'
You can use RLIKE instead
SELECT * FROM table1 WHERE name RLIKE 'Jack[0-9].*';
And please note the the '%' operator won't work with RLIKE, you have to use a regular expression pattern like '.*' instead.