Multiple Column REGEX search in MySQL - mysql

I am trying to search multiple columns in my Db using a regex. It works but using many and/or statments. I was wondering if it was possible to use something like this;
SELECT * FROM table REGEXP 'regex' IN (col1, col2, col3,.....)
This doesn't work, it was a guess at the syntax because I can't find anything similar by searching online. Is this a stupid idea or am I missing something very simple?

If you want to regexp search a value in multiple columns then you can do:
SELECT * FROM table where CONCAT(col1, col2, col3) REGEXP 'search-pattern';

The syntax for MySQL REGEX comparison is
expr REGEXP pattern_string
You cannot use it with IN. You would have to do:
SELECT * FROM `table` WHERE
col1 REGEXP 'regex'
OR col2 REGEXP 'regex'
OR col3 REGEXP 'regex'
You could also use RLIKE -- they are synonyms.

Related

Oracle SQL - Regex to search columns with only 1 letter

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')

like clause doesn't work as it said in MySQL SQL

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.

MySQL string to set and get intersection

I have a bad-projected database which have a ID sets in text columns like "1,2,5,10". I need to get an intersection of two columns which are set by same way.
I don't like to do it using PHP or another scripting language, I also not like MySQL custom functions.
Is there any way to get an intersection of two sets given by comma-delimeter strings?
Actually I don't need to have full intersection, I just need to know is there same numbers in two sets. If yes, I need to have "1", if no same number, I need to have "0".
Thank you.
You may be able to use REGEXP to do this with a bit of clever replacing.
Think this should do it (disclaimer: haven't tested it extensively):
SELECT col1,
col2,
CONCAT('(', REPLACE(col2, ',', '(\\,|$)|'), '(\\,|$))') AS regex,
col1 REGEXP CONCAT('(', REPLACE(col2, ',', '(\\,|$)|'), '(\\,|$))') AS intersect
FROM tbl
See http://sqlfiddle.com/#!2/7b86f/3
To explain: This converts col2 into a regular expression for matching against col1. The (\,|$) bit matches against either a comma or the end of the string. Hope this helps...
The code from Steve does not work in all cases.
For e.x it does not work when a number can be found in another number. INSERT INTO tbl (col1, col2)
VALUES ('60,61,64,68,73', '14,16,17,18,1');
With a little tweak it can work:
SELECT col1,
col2,
CONCAT('((\\,)', REPLACE(col2,',', '(\\,)|(\\,)'), '(\\,))') AS regex,
CONCAT(',',col1,',') REGEXP CONCAT('((\\,)', REPLACE(col2,',', '(\\,)|(\\,)'), '(\\,))') AS intersect
FROM tbl

MySQL and word boundary in regex

How can I use regex word boundary in a MySQL query?
For instance, I want to match either 'product' or 'newsletter',
It works fine with OR,
IF(? = 'product' OR ? = 'newsletter', ... , ...)
But how about a regex? I assume it would be something like this below?
IF(? REGEXP '^('product'||'newsletter')+$', ..., ... )
SELECT *
FROM tbl
WHERE description REGEXP '[[:<:]]red[[:>:]]|[[:<:]]blue[[:>:]]'
or, as a select expression:
SELECT IF(description REGEXP '[[:<:]]red[[:>:]]|[[:<:]]blue[[:>:]]', 1, 0) AS is_matched, tbl.*
FROM description
You can use RLIKE as a synonym but REGEXP seems to be more popular [citation needed].
Case Sensitivity
From the MySQL manual for REGEXP:
REGEXP is not case sensitive, except when used with binary strings.
mysql> SELECT 'a' REGEXP 'A', 'a' REGEXP BINARY 'A';
-> 1 0
Use REGEXP or RLIKE.
Regular expressions in MySQL
In your case, you could use MATCH() ... AGAINST fulltext search with MyISAM storage engine.
Or you could use IN() as #Wiseguy mentioned

Search MySQL table for a kind of results

How can I search within a MySQL table for results ending in anything except ".jpg"?
Thanks.
You don't need to involve regular expressions, you can just do:
SELECT my_fields
FROM my_table
WHERE my_field NOT LIKE '%.jpg'
SELECT *
FROM mytable
WHERE myfield NOT RLIKE '\\.jpg$'