I have a table like this:
// routes
+----+-----------------------------+
| id | route |
+----+-----------------------------+
| 1 | /tracking_code/expire/{id} |
| 2 | /tracking_code/list |
+----+-----------------------------+
{} means a dynamic value. And I need to match the first row for this entry value: /tracking_code/expire/2. I guess I need to use regexp. Any idea how can I do that?
My current workaround is using LIKE clause like this:
SELECT * FROM routes WHERE route LIKE :entry%
Also I should remove that number of the end of entry like /tracking_code/expire/.
Sadly my approach won't work for complicated routes like this: /tracking_code/expire/{id}/temp. Anyway, how should I use regexp for this?
You can use RLIKE like this
SELECT * FROM routes WHERE route RLIKE '^.*[0-9]+.*$'
to replace using regex i don't think this is possible, but there are some sneaky way, read this post here How to do a regular expression replace in MySQL?
MySql demo
Regex demo
Related
I am using Like to find something pattern from table
sample table:
id | title |
=============
1 | f550 |
-------------
2 | f550 |
-------------
3 | f-550 |
-------------
4 | f 550 |
I am using LIke query to check my receords, so lets say if I search for f550 it is bringing only 2 records that is correct technically but I want all records having any pattern such as (f550,f-550,f 550)
Is there any way besides REGEX I can do?
My query fires like this
SELECT * FROM `qd_posts` WHERE title LIKE '%f550%';
I tried using different combinations such like this but didn't worked.
SELECT * FROM `qd_posts` WHERE title LIKE '%f%-%550%';
I have even tried using RLIKE but still not got the result
You could try using '%f%550%'
SELECT *
FROM `qd_posts`
WHERE title LIKE '%f%550%';
or
SELECT *
FROM `qd_posts`
WHERE title LIKE 'f%' AND LIKE '%550';
For the specific list given (f550,f-550,f 550), use this:
REGEXP '^f[- ]?550$'
Yes, LIKE 'f%550' matches the same things, but lots more.
Is there a built in way in mysql to query columns that contains specific word(s) and the word(s) also start with the input.
For instance, input is "EF"
#1 ABCD EFGH <- Want this one
#2 BCDE FGHA
#3 CDEF GHAB
#4 EFGH ABCD <- And this one
If I query %EF%, it'll give me the #1, #3 and #4, if I query EF%, it's only going to give #4.
So essentially, I want to know if mysql can firstly do a preg_split like in PHP, then query EF%, or something along the line of that.
Use a regular expression. In MySQL, the regexp pattern [[:<:]] matches the beginning of a word, so you can do:
WHERE column REGEXP '[[:<:]]EF'
Documentation
Answering my own question here. Just came up with a solution, not pretty but it works.
I just changed my parameter into this
((Column LIKE '%...%' AND Column LIKE '% ...%') Or (Column LIKE '...%')) AND ...
Going to leave this here for a day and see if anyone has a better solution.
If your column has two words, then you can use SUBSTR() and LOCATE().
+--------------------------------+
| MyColumn |
+--------------------------------+
| Apple Effect |
| Effortless Orange |
+--------------------------------+
.
SELECT *
FROM (
SELECT
*,
SUBSTR(#values,1,LOCATE(' ',MyColumn)-1) AS FirstWord
SUBSTR(#values,LOCATE(' ',#values)+1) AS SecondWord
FROM MyTable
) AS X
WHERE FirstWord LIKE 'EF%'
OR SecondWord LIKE 'EF%'
If your column has more than two words, then things are a little more complex, but you can use SUBSTRING_INDEX() to find the Nth instance.
+--------------------------------+
| MyColumn |
+--------------------------------+
| The Perfect Apple Effect |
| The Special Effortless Orange |
+--------------------------------+
.
SUBSTRING_INDEX(SUBSTRING_INDEX(#values,' ',4),' ',-1) = Gets the fourth word.
More examples and information can be found here.
Im going to make a search field, where the user types the item he/she is looking for.
lets say helm in this example.
The database holds items, like:
id | name |
1 | helm of c
2 | helm of 9
3 | helm
4 | helmset
5 | helmapo
6 | haloween
how can i make the mysql query in this case return row 1-5, cause its all match?
You can use wildcards
SELECT * FROM table WHERE name LIKE '%helm%'
More information on wildcards: http://www.w3schools.com/sql/sql_wildcards.asp
You can try to make wildcard search like this:
select * from tablename where name like 'helm%'
Also if you the word helm can appear in the middle of the column value or any place then put it like
select * from tablename where name like '%helm%'
You can use sql wildcards to handle this use case
SELECT row FROM tablename WHERE field LIKE '%helm%'
If you encapsulate the search term with % on both sides the SQL will return any row containing the search term at any index position, however if you only want return rows that start with helm then you would use the term
'helm%'
I have a query I need to run on almost 2000 strings where it would be very helpful to be able to do a list like you can with the "IN" operator but using the LIKE comparison operation.
For example I want to check to see if pet_name is like any of these (but not exact): barfy, max, whiskers, champ, big-D, Big D, Sally
Using like it wouldn't be case sensitive and it can also have an underscore instead of a dash. Or a space. It will be a huge pain in the ass to write a large series of OR operators. I am running this on MySQL 5.1.
In my particular case I am looking for file names where the differences are usually a dash or an underscore where the opposite would be.
For this task I would suggest making use of RegExp capabilities in MySQL like this:
select * from EMP where name RLIKE 'jo|ith|der';
This is case insensitive match and will save from multiple like / OR conditions.
You could do something like this -
SELECT FIND_IN_SET(
'bigD',
REPLACE(REPLACE('barfy,max,whiskers,champ,big-D,Big D,Sally', '-', ''), ' ', '')
) has_petname;
+-------------+
| has_petname |
+-------------+
| 5 |
+-------------+
It will give a non-zero value (>0) if there is a pet_name we are looking for.
But I'd suggest you to create a table petnames and use SOUNDS LIKE function to compare names, in this case 'bigD' will be equal to 'big-D', e.g.:
SELECT 'bigD' SOUNDS LIKE 'big-D';
+---------------------------+
| 'bigD'SOUNDS LIKE 'big-D' |
+---------------------------+
| 1 |
+---------------------------+
Example:
CREATE TABLE petnames(name VARCHAR(40));
INSERT INTO petnames VALUES
('barfy'),('max'),('whiskers'),('champ'),('big-D'),('Big D'),('Sally');
SELECT name FROM petnames WHERE 'bigD' SOUNDS LIKE name;
+-------+
| name |
+-------+
| big-D |
| Big D |
+-------+
As first step put all static values in any temporary table, this would be lookup dictionary.
SELECT * FROM Table t
WHERE EXISTS (
SELECT *
FROM LookupTable l
WHERE t.PetName LIKE '%' + l.Value + '%'
)
Configure the column containing those 2000 values for full-text searching. Then you can use MySQL's full-text search feature. Refer to their docs
You could use REGEXP instead. It worked like a charm for me
pet_name regexp 'barfy|max|whiskers|champ|you name it'
I have a table that has a comma separated list of ids in one of the fields (not my doing). I want to know if I can use LIKE to match a number in this string? The problem is I don't want to get similar numbers. Is there a way to match a number with no numeric charcters on either side?
SELECT * FROM table WHERE activitiesids LIKE 6
| activitiesids |
---+---------------+---
| 3,16,8,6 |
---+---------------+---
| 6 |
---+---------------+---
| 7,560 |
---+---------------+---
Haven't tested but you can try something like this:
SELECT * FROM table WHERE activitiesids REGEXP '[[:<:]][0-9]+[[:>:]]';
Something like that:
WHERE ids LIKE '%,16,%' OR ids LIKE '%,16' OR ids LIKE '16,%';
Postgresql even has pattern matching - I don't know for mysql:
WHERE ids ~ '^(.*,)?16(,.*)?$';