I have these rows:
db666405.gallery
db666405.table1
db666405.table2
I want to capture the word after the dot.
How to do with regex in mysql?
I have tried with ^\. or [.], but I did not succeed.
SELECT *
FROM `table`
WHERE `column` REGEXP '^\\.'
MySQL regex doesn't "capture" anything. It's for matching only; there is no support for regex replacement in MySQL.
I assume you want to return the part after the dot:
select *, substring(`column`, instr(`column`, '.') + 1) as ext
from `table`
where `column` like '%.?%'
To select any columns that have the word Gallery, you can use a
SELECT *
FROM `table`
WHERE `column` LIKE '%gallery%' `
This will return all that have gallery
Related
I'm using this query in my autocomplete feature:
SELECT description FROM my_table WHERE LIKE "%keyword%"
But this query returns the entire content of the field which is sometimes too long.
Is it possible to limit the number of characters before and after "keyword" ?
I suggest using MySQL's REGEXP operator here. For example, to accept a maximum of 10 characters before and after keyword, you could use:
SELECT description
FROM my_table
WHERE col REGEXP '^.{0,10}keyword.{0,10}$';
Note that if you intend to match keyword as a standalone word, you may want to surround it by word boundaries in the regex pattern:
SELECT description
FROM my_table
WHERE col REGEXP '^.{0,10}\\bkeyword\\b.{0,10}$';
To show for example 5 characters before and after you word you can do it using RIGHT, LEFT and SUBSTRING_INDEX
select description, concat(RIGHT(SUBSTRING_INDEX(description, 'keyword', 1),5), 'keyword', LEFT(SUBSTRING_INDEX(description, 'keyword', -1),5) ) as snippet
from my_table
where description like "%keyword%";
Check it here : https://dbfiddle.uk/MZcVJgEL
In a perfect world for this type of setup, we would have an integer column that expects only numbers;
But what if you have a varchar column and you want to add a WHERE clause that said something like this:
WHERE <value> is NOT a number
In essence, you are selecting all rows that contain any characters that are NOT ONLY numbers.
This is for MySQL.
try this
SELECT * FROM myTable WHERE concat('',col1 * 1) != col1
demo here
REGEXP or RLIKE are your friends:
SELECT * FROM `MyTable` WHERE `Column` RLIKE '^[^0-9]*$';
UPDv1:
You may use different regexes to detect negative integers:
SELECT
'-2' RLIKE '^[^0-9]*$', -- fails
'-1' RLIKE '-[0-9]'; -- succeeds
For example:
SELECT * FROM `MyTable` WHERE `Column` RLIKE '-[0-9]' OR `Column` RLIKE '^[^0-9]*$';
Tested with this:
SELECT
*
FROM
(
SELECT 'abs 12 x' as `Column`
UNION ALL
SELECT 12
UNION ALL
SELECT -2
UNION ALL
SELECT '-x'
) as `sub`
WHERE
`Column` RLIKE '-[0-9]'
OR
`Column` RLIKE '^[^0-9]*$';
Output:
-2
-x
You should be able to use a regular expression in the where clause.
The following mysql documentation link provides details:
http://dev.mysql.com/doc/refman/5.1/en/regexp.html
This would approach somehow your goal:
SELECT * FROM MyTable WHERE NOT MyTable.Field1 REGEXP '^[[:digit:]]*$';
As Field1 is VARCHAR, this will select all rows that Field1 is not wholly numerical.
If you have floating-point value:
SELECT * FROM MyTable WHERE NOT MyTable.Field1 REGEXP '^[[:digit:]\.]*$';
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
The field table.name contains 'Stylus Photo 2100' and with the following query
SELECT `name` FROM `table` WHERE `name` LIKE '%Stylus 2100%'
I get no results. Of course i would if i searched
SELECT `name` FROM `table` WHERE `name` LIKE '%Photo 2100%'
How can I select the record by searching 'Stylus 2100' ?
Thanks
Well if you know the order of your words.. you can use:
SELECT `name` FROM `table` WHERE `name` REGEXP 'Stylus.+2100'
Also you can use:
SELECT `name` FROM `table` WHERE `name` LIKE '%Stylus%' AND `name` LIKE '%2100%'
I think that the best solution would be to use Regular expressions. It's cleanest and probably the most effective. Regular Expressions are supported in all commonly used DB engines.
In MySql there is RLIKE operator so your query would be something like:
SELECT * FROM buckets WHERE bucketname RLIKE 'Stylus|2100'
I'm not very strong in regexp so I hope the expression is ok.
Edit
The RegExp should rather be:
SELECT * FROM buckets WHERE bucketname RLIKE '(?=.*Stylus)(?=.*2100)'
More on MySql regexp support:
http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp
You can just replace each space with %
SELECT `name` FROM `table` WHERE `name` LIKE '%Stylus%2100%'
The correct solution is a FullText Search (if you can use it) https://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html
This nearly does what you want:
SELECT * FROM buckets WHERE bucketname RLIKE '(Stylus|2100)+.*(Stylus|2100)+';
SELECT * FROM buckets WHERE bucketname RLIKE '(Stylus|2100|photo)+.*(Stylus|2100|photo)+.*(Stylus|2100|photo)+.*';
But this will also match "210021002100" which is not great.
you need to do something like this,
SELECT * FROM buckets WHERE bucketname RLIKE 'Stylus.*2100';
or
SELECT * FROM buckets WHERE bucketname RLIKE '(Stylus)+.*(2100)+';
Assuming that your search is stylus photo 2100. Try the following example is using RLIKE.
SELECT * FROM `buckets` WHERE `bucketname` RLIKE REPLACE('stylus photo 2100', ' ', '+.*');
EDIT
Another way is to use FULLTEXT index on bucketname and MATCH ... AGAINST syntax in your SELECT statement. So to re-write the above example...
SELECT * FROM `buckets` WHERE MATCH(`bucketname`) AGAINST (REPLACE('stylus photo 2100', ' ', ','));
SELECT `name` FROM `table` WHERE `name` LIKE '%Stylus % 2100%'
I need a MySQL query w/ Regex to tell me if my string's first character is a number from 0 to 9.
The following query returns '1', since the REGEXP matches. You can adapt it for your purposes:
SELECT '123 this starts with a digit' REGEXP '^[[:digit:]]';
You can use it in a SELECT like this:
SELECT * FROM tbl WHERE field REGEXP '^[[:digit:]]';
Use this:
SELECT 'a12' REGEXP '^[0-9]';
=> 0
SELECT '4ab' REGEXP '^[0-9]';
=> 1