Using regexp with sql to find words that match specific characteristics - mysql

I want to write a select statement that gets all the columns where one of them match a certain regular expression.
I basically have this file_name column and I want to select all files that start with a certain word, have a certain word in the middle, and end with 'xlsx'.
I know for checking if if starts with a certain word, I can do
SELECT * FROM files WHERE file_name REGEXP '^company';
to find files that start with the word company, just using that as a place holder.
after this point it can have whatever other characters but at some point in the middle, i am looking for another word, lets say 'dog' and then ends with xlsx.
I know for finding file names that end with xlsx i can do:
SELECT * FROM files WHERE file_name REGEXP 'xlsx[[:>:]]';
My main confusion is how to combine all these 3 cases into one statement. so that it starts with company then at some point has the word dog then after at some point ends with xlsx.

You can combine all conditions into one select using:
SELECT * FROM files WHERE file_name REGEXP '^company.*dog.*xslx$';

If the "words" are not too complex, LIKE will be faster:
SELECT * FROM files WHERE file_name LIKE 'company%dog%xslx';
especially if you have INDEX(file_name).

Related

Select rows in SQL partially matching an input input

I would like to select rows in my table (I'm using Google Sheet for that purpose) which content is included in the string.
For example, rows included in table called Jobportal, column Test:
How to find work
Work permit
Jobs
Temporary jobs
I want to select all the rows that contain any word of my input, so if I write "i'm looking for a job", I need to select rows Jobs and Temporary jobs. If I write "where is my work?", I need to select How to find work and Work permit.
I've tried this query, but it's returning wrong/unexpected results.
select * from Jobportal where 'im looking for a job' LIKE CONCAT('%',Test,'%');
You can use regular expressions. Assuming that what the user types does not have special characters:
where test regexp replace('im looking for a job', ' ', '|')
That said, for performance you might want to consider using full text search capabilities.

Mysql search, match words if similar

I have a db with a table called sections. In that is a field called head that has a full text index with 3 entries each a string. 2 entries have the word motorcycle and one has motorcycles. I can't seem to find a way to return all 3 if the term "motorcycles" is search.
I have tried
SELECT * FROM sections
WHERE MATCH (head) AGAINST ('Motorcycles')
but it only returns the plural entry. I have also tried.
SELECT * FROM sections
WHERE head like '%motorcycles%'
but that also only returns the plural entry. Is there a way to return all three rows based on "motorcycles"?
Have you tried boolean mode?
where match (head) against ('+ Motorcycle*' in Boolean mode)
More information is here.
Your where clause has an extra "s":
SELECT * FROM sections WHERE head like '%motorcycle%'
Assuming your question is more general than the specific motorcylce example you've given...I'm not aware of a way that you can relax the contraints directly in the SQL (without a stored proc to pre process the input). I'd suggest pre processing your input with a regex to remove/replace the chars that make the word plural. Then use like in the way that you have shown on the singular version of the word.
If i have got your Questions correctly I think you want something like this:
if (SELECT count(1) FROM sections WHERE head like '%motorcycles%')>1
begin
select * FROM selections
WHERE head like '%motorcycle%'
end

Match group in MySQL query with regex containing some words but not others

I am querying against a database of songs, and want to separate out rows containing the song "Tube" (which may appear with any number of characters before and after it, e.g. "Track 01. Tube -> Another song"), but not match the songs "First Tube" or "Last Tube", which may have extraneous characters as well.
I've been doing this with this query:
SELECT * FROM `songs` WHERE `song` LIKE ('%Tube%') and song not like ('%first%') and song not like ('%last%');
But would prefer to use a regular expression so that I may use the same matching in other places (like within PHP code).
I tried something like this:
SELECT * FROM songs WHERE song REGEXP '.*(first|last){0}.*(tube).*';
But it didn't work obviously, it matches 'First Tube' songs too.
If you want to use regular expressions here, I think you might be looking for a regular expression more along these lines:
SELECT * FROM songs WHERE song REGEXP ".*Tube.*" and NOT REGEXP ".*first.*|.*last.*"
That's not far from the LIKE statements you have above, but it would give you a start.

Get all records between to alpha variables in alpha order mysql

I have a database of words for dictionary lookup purposes. What I need to be able to do with mysql is allow a user to input to variables (alpha) and my script will return every word that starts with both of those variables and everything in between.
Let's say the two variables are:
$letters1 = abor
$letters2 = accr
I want to get every word that starts with abor through accr. I need to return every word that would fit between those two starting points. So an example SQL statement that I know does not work but might help you understand what I am asking:
SELECT word from table1 WHERE word LIKE '%abor%' THROUGH '%accr%' ORDER BY word ASC
I know that THROUGH is not an operator but that's the general idea of what I need to accomplish.
If you merely want words that start with letters between the two variables, you can use MySQL's BETWEEN ... AND ... operator:
SELECT word FROM table1 WHERE word BETWEEN 'abor' AND 'accr' ORDER BY word

MySQL UNION query correct handling for 3 or more words

I've to ask your help to solve this problem.
My website has a search field, let's say user writes in "Korg X 50"
In my database in table "products" i have a filed "name" that holds "X50" and a field "brand" that hold "Korg". Is there a way to use the UNION option to get the correct record ?
And if the user enters "Korg X-50" ?
Thank you very much !
Matteo
May be you should use full-text search
SELECT brand, name, MATCH (brand,name) AGAINST ('Korg X 50') AS score
FROM products WHERE MATCH (brand,name) AGAINST ('Korg X 50')
As far as I understand you don't need UNION but something like
SELECT * FROM table1
WHERE CONCAT(field1, field2) LIKE '%your_string%'
On client side you get rid of all characters (like space, hyphen, etc) in your_string that appears in user input and cannot be in field1 or field2.
So, user input Korg X 50 as well as Korg X-50 becomes KorgX50.
you will need to get some form of searchable text.
either parse out the input for multiple key words and match each separately, or perhaps try to append them all together and match to the columns appended in the same way.
you will also need either a regex, or maybe a simpler search and replace to get rid of spaces and dashes after the append before the comparison.
in general, allowing users to search for open ended text strings is more complicated than 'what union do i use'... you will ideally also be worried about slight misspellings and capitalization, and keyword order.
you may consider pulling all keywords out from your normal record into a separate keyword list associated with each product, then use that list to perform your searches.
If you do not want to parse user input and use as it is, then you will need to use a query like this
select * from products where concat_ws(' ',brand,name) = user_input -- or
select * from products where concat_ws(' ',brand,name) like %user_input%
However, this query won't return result if user enters name "Korg X-50" and your table contains "Korg" and "X50", then you need to do some other thing to achive this. You may look at http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_soundex however it won't be a complete solution. Look for text indexing libraries for that ex: lucene