I have data in column 'sub' in mysql database :
1=0|2=0|3=0, 3=0, 1=0, 2=0
How to filter '2=0' from 'sub' with regexp mysql ?
i was try
SELECT * FROM `men` WHERE `sub` REGEXP '2{0,1)=0'
but the result is more than 1. I want only '2=0' for my result.
please, is that any solution for me ?
You could use LIKE operator
For all rows containing 2=0
SELECT * FROM men WHERE sub LIKE '%2=0%'
For containing just 2=0
SELECT * FROM men WHERE sub = '2=0'
I want only '2=0' for my result
Use the beginning of the string ^ and the end of the string $ anchors.
REGEXP, in this case, would be good to find dynamic numbers:
SELECT * FROM men WHERE sub REGEXP '^[0-9]=0$'
for such simple case (I want only '2=0' for my result) it's achievable without regexp :
SELECT * FROM men WHERE sub = '2=0'
Related
I have a bunch url that has a string either has
hotel+4 digit number: hotel1234
or slash+4digit.html: /1234.html
Is there a regex to extract 4 digit number like 1234 either use python or mysql?
I'm thinking 'hotel'[0-9][0-9][0-9][0-9],sth like this
Thanks!
You can try the REGEXP
SELECT * FROM Table WHERE ColumnName REGEXP '^[0-9]{4}$'
or
SELECT * FROM Table WHERE ColumnName REGEXP '^[[:digit:]]{4}$';
The following is a stackoverflow.com link that might be useful showing
how to extract a substring from inside a string in Python?
Unfortunately, MySQL regexp simply returns true if the string exists. I have found substring_index useful if you know the text surrounding the target...
select case when ColumnName like 'hotel____' then substring_index(ColumnName,'hotel',-1)
when ColumnName like '/____.html' then substring_index(substring_index(ColumnName,'/',-1),'.html',1)
else ColumnName
end digit_extraction
from TableName
where ...;
The case statement above isn't necessary because of the way substring_index works (by returning the entire string if the search string isn't found).
select substring_index(substring_index(substring_index(ColumnName,'hotel',-1),'/',-1),'.html',1)
from TableName
where ...;
I have a string /path/to/project/subdirectory/ and need to find a row where path is /path/to/project/.
How can I find rows where my string starts with path?
It would be the opposite of:
SELECT * FROM projects WHERE path LIKE "/path/to/%"
Because I have too many characters, not too few.
Both of these return zero rows:
SELECT * FROM projects WHERE path LIKE "/path/to/project/subdirectory/%"
SELECT * FROM projects WHERE "/path/to/project/subdirectory/" LIKE (path+"%")
You were close with your second attempt, but MySQL uses CONCAT() to concatenate strings, not +.
SELECT *
FROM projects
WHERE "/path/to/project/subdirectory/" LIKE CONCAT(path, '%')
You can also use
WHERE LOCATE(path, "/path/to/project/subdirectory/") = 1
I am working on a database that has been made in a terrible way. I have to find an ID in a comma'd column.
The column values could be:
6154
5145,6154,4562
161545
My query is:
SELECT resource_id,filename,id FROM image WHERE other_vendors = '".$vendor_id."'
$vendor_id will be 6154
Now how do I match with a LIKE REGULAR EXPRESSION in a SQL QUERY where I get all the values which have a 6154 or 6154 in a comma'd string.
Just use build in function find_in_set, which search string in comma separated list:
SELECT resource_id,filename,id FROM image WHERE find_in_set('6154',other_vendors)
I think this is the easiest method:
SELECT resource_id,filename,id FROM image WHERE (','+other_vendors +',') like '%,$vendor_id,%'
Try something like:
SELECT resource_id,filename,id FROM image WHERE other_vendors REGEXP '.*(6154){1}.*'
SELECT resource_id,filename,id FROM image WHERE other_vendors = '%6154%'
I need a method that will go through database and return appropriate results. in this case its searching for books by author, title, publishing date or ISBN code. I decided to use where() method but i encountered two problems:
1) i have trouble searching by multiple fields. its easy looking for a title:
def self.browse(query)
if query.nil?
nil
else
self.where("title REGEXP :query", query: query)
end
end
but i dont know how to set it to look for title OR author OR isbn etc. tried
self.where("(title OR author OR publishing_date OR isbn) REGEXP :query", query: query)
but it doesnt work
and second, i want my query to match only a beginning or the end of the word. in mysql Workbench its pretty easy but i have a hard time doing it in Rails. here's what i've tried so far (and failed):
self.where("title REGEXP :query", query: /^(query)*$/)
self.where("title REGEXP /^:query/", query: query)
self.where("title REGEXP :query", query: $"query"^)
Needless to say, on the internet i found many different docs or tutorials, one saying "^" should be at the end, the other it should be at the beginning...
1) You will want to use parentheses and both AND and OR clauses in your where sql:
(title IS NOT NULL AND title REGEXP :id_query) OR (name IS NOT NULL AND name REGEXP :name_query)
2) You will want to use both ^ (beginning of line) and $ (end of line), like this.
(^something|something$)
Here is an example of the whole thing that I matched against my own code. Replace id and name with your own columns, and put extra OR's in there to match against more columns
Charity.where("(id IS NOT NULL AND id REGEXP :id_query) OR (name IS NOT NULL AND name REGEXP :name_query)", id_query:'1', name_query:'(^a|a$)')
Here is the to_sql output of the above:
Charity.where("(id IS NOT NULL AND id REGEXP :id_query) OR (name IS NOT NULL AND name REGEXP :name_query)", id_query:'1', name_query:'(^a|a$)').to_sql
=> "SELECT `charities`.* FROM `charities` WHERE ((id IS NOT NULL AND id REGEXP '1') OR (name IS NOT NULL AND name REGEXP '(^a|a$)'))"
This should do it:
self.where("title REGEXP ? OR author REGEXP ? OR publishing_date REGEXP ? OR isbn REGEXP ?", query, query, query, query)
The "?" will be subbed in order by the included variables. If you want to use the same regexp for each column, then just plug the code in as-is
As for the second part, you may want to check out the LIKE operator.
To match a column which starts with a given string you'd do:
self.where("title LIKE ?", (query + "%"))
And to match a column that ends in a particular string:
self.where("title LIKE ?", ("%" + query))
create your sql query and pass into ActiveRecord execute method,it will excute sql query and do not need to change in ActiveRecord query
sql query = "your sql query"
ActiveRecord::Base.connection.execute(sql query)
You can use or:
class MyARModel < ActiveRecord::BAse
scope :search, ->(rgx) do
where('title REGEXP ?', rgx)
.or('author REGEXP ?' rgx)
.or('publishing_date REGEXP ?' rgx)
.or('isbn REGEXP ?' rgx)
end
#...
I have the following strings in the following pattern in a table in my db:
this_is_my_string_tester1
this_is_my_string_mystring2
this_is_my_string_greatstring
I am trying to match all strings that start with a specific pattern split by underscores i.e.this_is_my_string_ and then a wildcard final section
Unfortunately there is an added complication where some strings like the following:
this_is_my_string_tester1_yet_more_text
this_is_my_string_mystring2_more_text
this_is_my_string_greatstring_more
Therefore taking the following as examples:
this_is_my_string_tester1
this_is_my_string_mystring2
this_is_my_string_greatstring
this_is_my_string_tester1_yet_more_text
this_is_my_string_mystring2_more_text
this_is_my_string_greatstring_more
I am trying to have returned:
this_is_my_string_tester1
this_is_my_string_mystring2
this_is_my_string_greatstring
I have no idea how to do this with a like statement. Is this possible if so how?
EDIT
There is one final complication:
this_is_my_string
needs to be supplied as a list i.e in
(this_is_my_string, this_is_my_amazing_string, this_is_another_amazing_string)
SELECT * FROM atable WHERE afield REGEXP 'this_is_my_string_[a-z]+'
It might be faster if you have an index on afield and do
SELECT * FROM atable WHERE afield REGEXP 'this_is_my_string_[a-z]+'
AND afield LIKE 'this_is_my_string_%'
After edit of question:
Either
SELECT * FROM atable
WHERE afield REGEXP '(this_is_my_string|this_is_my_amazing_string)_[a-z]+'
or maybe you want something like having a table with the prefixes:
SELECT *
FROM atable AS t,
prefixes AS p
WHERE afield REGEXP CONCAT(p.prefix, '_[a-z]+')
As by the reference documentation this should not be possible, as a pattern (string literal) is required. Give it a try nevertheless.
There the answer of #KayNelson with LIKE (?) and INSTR might do instead of REGEXP.
try this
SELECT * FROM db.table WHERE strings LIKE 'this_is_my_string_%' AND instr(Replace(strings,"this_is_my_string_",""),"_") = 0;
It checks if more _ occurs after replacing the standard "this_is_my_string_"