Query for exact word search - mysql

hope that you are doing fine
I am having very hard time writing a query
Here is my question explained
i have a database table say "jreviews_content" which has a field named "jr_produits"
In "jr_produits" the data is is the format *ryan-decosta*tom-gosling* so i want a search query that is exact word based i.e if the user type "rya" the mysql should not return anything
but if the user type ryan then it should return the row likewise if the user type "gos" the mysql should not return anything
but if the user type gosling then it should return the row where ryan and gosling are the exact words
the query that i am writing are
SELECT *
FROM `jreviews_content`
WHERE jr_produits LIKE '%*ryan-%' or jr_produits LIKE '%-ryan*%'
or jr_produits LIKE '%*ryan*%' or jr_produits LIKE '%-ryan-%';
I want that to be done in some other way that is more efficient(either by regular expression or any other method)
SELECT * FROM `jreviews_content` WHERE jr_produits REGEXP '^[*-]ryan[*-]$'
It doen't fetch anything
neither does
SELECT * FROM `jreviews_content` WHERE jr_produits like '%[*-]ryan[*-]%'
Please suggest something

Try the MySQL regex word boundary markers. They're documented about halfway down this page:
SELECT *
FROM jreviews_content
WHERE jr_produits REGEXP '[[:<:]]ryan[[:>:]]'
Note that I don't have MySQL access today, so this is untested.
Also heed what #user1032531 said. Records with values like *ryan-decosta*tom-gosling* almost always mean "bad design".

Related

transfer several parameters from a form into SQL query

i am working on a table that includes a filter function.
For the filter i use a form where i enter the parameters.
Those are added to a string which is my SQL query.
So far it works fine.
There is oine input field where multiple parameters canbe added.
The plan is to seperate them with ; .
For example 520;521;522
My plan was to use str_replace to convert this in to sql Code.
For example
$str = str_replace(";", "" OR ", "520;521;522");
Results in to:
SELECT * FROM MaschinenVorgangslisteMitHV WHERE (VorgangNr LIKE '%520%' or '%522%' or '%523%')
But some how this code does not show the expected results.
I only get results for '%520%'
How do i need to adjust this query in order to have the sql query working?
$str = str_replace(";", "" OR ", "520;521;522");
Results in to:
SELECT * FROM MaschinenVorgangslisteMitHV WHERE (VorgangNr LIKE '%520%' or '%522%' or '%523%')
In another input field i search for names.
Here the query looks like this...
SELECT * FROM MaschinenVorgangslisteMitHV WHERE (Bearbeiter LIKE '%Heine%' OR Bearbeiter LIKE '%Wolf%' OR Bearbeiter LIKE '%Maiwald%')
This works fine!
The multiple like should be written as,
SELECT *
FROM MaschinenVorgangslisteMitHV
WHERE VorgangNr REGEXP '520|522|523';
I believe you need to add VorgangNr LIKE after every OR.

Using PDO and LIKE not working, why?

I'm using PDO to connect to MySQL. Everything is working fine, except this doesn't work.
Does anyone knows why? And how should i do it?
SELECT * FROM flagIt WHERE :flagids LIKE CONCAT('%', flagIt.flagIt_id, '%')
:flagids is equivalent to a string like "ID1 ID2 ID3".
EDIT (just to compare)
SELECT * FROM flagIt WHERE 'ID1 ID2 ID3' LIKE CONCAT('%', flagIt.flagIt_id, '%)
If i use like this, it works fine, so...why it does not work with :flagids?
I hope you understand my problem.
Thank you very much.
EDIT
I tried:
"SELECT * FROM flagIt WHERE flagIt.flagIt_id IN(:flagids)"
and as Hobo Sapiens suggested
"SELECT * FROM flagIt WHERE FIND_IN_SET(flagIt.flagIt_id, :flagids)"
and nothing works!!!!!!!!!
This is the query you're submitting to PDO::prepare():
SELECT * FROM flagIt WHERE :flagids LIKE CONCAT('%', flagIt.flagIt_id, '%')
The process of preparing a statement doesn't just involve evaluating the contents of the placeholders, substituting them in a string and executing the resulting query.
A prepare asks the server to evaluate the query and prepare an execution plan that includes the table and indexes it will use. For that it needs to know which columns of which tables it must work with, which is why one cannot use a placeholder where you would need an identifier.
The problem with your query is that the server has no way to know at the time it prepares the statement whether the placeholder represents a string literal or a column identifier. Without that information, the preparation cannot be done, and your prepare will fail.
If you have some flexibility over the value you're using in :flagids you could use find_in_set():
SELECT * FROM flagIt WHERE find_in_set(flagIt_id, :flagids)
where a variable containing, for example, 'ID1,ID2,ID3' is bound to :flagids.
This will be fine for small lists, but will be slow for a large list.
MySQL reference for find_in_set()

SELECT * FROM games WHERE

I am having issues with my MySQL syntax. I would like to run a select query where either one of two options are true. However the following code does not work.
SELECT * FROM games WHERE genre="indie" OR title="indie"
I have been fooling around and look at other threads and have found out how to use OR to check the same column for multiple entries but not a way to check different columns for the same entries. When I do:
SELECT * FROM games WHERE genre="indie"
The query works fine. Any help would be greatly appreciated.
The only way I see this really would't work, is if you've mistyped the name of the column 'title' (if the second query you wrote works)
The assumptions about the case sensitivity are wrong, since the second query returns something, the first should return at least the same rows as the second one
In MySQL " " works just as ' ', so this assuption was wrong too.
If you post more information, it would be easier to help you
Maybe you ignoring the upper/lower case? Also use like
You can use this:
SELECT * FROM games WHERE (LOWER(genre) like 'indie') OR (LOWER(title) like 'indie')

Using HeidiSQL with mysql select statement with "not like" does not work

Ok guys, here's the story, I have a very simple query I'm trying to do and for the life of me I don't understand why it doesn't work.
I'm am using the streaming twitter feed to populate my database using keywords. Now I want to filter out the retweets by using this query
select * from earthquake
where earthquake.Text not like '%RT%'
order by earthquake.Text
It returns "0 rows affected, 0 rows found", and yes there are thousands of retweets, so I know they exist.
I do the same query with the '%#%' and it finds thousands as expected, it almost seems that everything except 'RT' works.
Did you try NOT LIKE '%RT #%'?
Is it possible that common word xxxrtxxx always contains in your Text field?
I don't know anything about mysql specifically, but is there a function like MSSQL's PatIndex() that you could use instead of not like?
Alternatively you could try something like this: (dont know if your db supports this syntax)
SELECT * FROM Earthquake
WHERE CASE WHEN Text LIKE '%RT%' THEN 1 ELSE 0 END = 0
ORDER BY Text
I'd also advise checking your logic of looking for "RT" somewhere in the Text column to identify the stuff you don't want, because I'm surprised that "Not Like [expression]" apparently doesnt work.

mysql regexp for search using alias

I am not very good with regexp so I really would like some help to achieve my goal.
When searching in my db I use an alias for specific keywords.
Here is an example
keyword tets alias test (someone have spell wrong then word test)
keyword b.m.w alias bmw (if someone write b.m.w instead of bmw)
etc.
So far if a user searches for "bmw 316" I use LIKE "%bmw%316%" to get the results.
Now if the user searches for "b.m.w 316" I must use
"%b.m.w%316%" OR
"%bmw%316%"
because b.m.w has alias bmw.
In the case of 6 words with 2-3 aliases there are too many combinations.
I am trying to achieve it with regexp.
In the scenario above it would be something like (bmw|b.m.w) 316.
How do I solve this problem?
You are not looking for REGEXP you are looking for a thing called levenshtein distance
MySQL does not (yet) have native support for this (wonderful) concept, but you can download a UDF here:
http://joshdrew.com/
And here's a list so you've got something to choose from:
http://blog.lolyco.com/sean/2008/08/27/damerau-levenshtein-algorithm-levenshtein-with-transpositions/
You can also write your own function in MySQL, so you don't have to install a UDF.
http://www.supermind.org/blog/927/working-mysql-5-1-levenshtein-stored-procedure
Finally this question might help you out as well:
Implementation of Levenshtein distance for mysql/fuzzy search?
A query for the closest match would look something like:
SELECT * FROM atable a ORDER BY levenshtein(a.field, '$search') ASC LIMIT 10