LIKE MySQL search not functioning with CONCAT - mysql

I got a problem regarding SQL query. If single word SQL query can detect the string, but if I add multiple words inside the string, it won't show any results.
SELECT * FROM rules WHERE keyword LIKE CONCAT('%','$message','%')
Input 1 (show results)
$message = "ASK"
Input 2 (show no results)
$message = "I WANT TO ASK"

I'm assuming you want those rows where the value of the keyword column is part of the message. That's the typical use case for key words.
If that's right then you've get this result with the help of the function INSTR:
SELECT * FROM rules WHERE INSTR('$message', keyword) > 0;
While
SELECT * FROM rules WHERE '$message' LIKE CONCAT('%', keyword, '%');
would work most times too, it's got the restriction that the keyword musn't contain the wildcard characters for the LIKE operator: % and _. INSTR doesn't have this restriction.

Related

MySQL underscore with LIKE Operator

I have an SQL query which matches results using a LIKE :
_column_name_%
This will return results where the column name is:
_column_name_1
_column_name_2
The end number could be a really high number e.g. 32523, or even 523624366234.
If I use _column_name_%%%%% this would match 32523, but not 523624366234.
How would I get the LIKE to match without typing % repeatedly?
A Simple select query with the LIKE operator should look like this
You have to escape the underscore using "\" if you are having any.
instead of pretext_% use pretext\_%
Select * from mytable where field_1 like 'pretext\_%'
This will return pretext_1 as well as pretext_11

Apostrophe in FULL TEXT SEARCH mysql

When I searched for citroen in search page like that I get result but if I search blackn roll I dont get result because it's written like black'n roll in the table. Some user may also wanna search blackn roll but doesnt get result. How can I fix it? And also rows like v-hr and speacial characters like "&/(). I want the mysql to ignore them.
$sql = "SELECT * FROM arac
INNER JOIN suv_marka ON arac.marka = suv_marka.id
WHERE match(suv_marka.marka) against('citroen')";
If you know the characters (to be replaced) already then, you can use MySQL's REPLACE function to replace them with % and perform LIKE comparison, e.g.:
create table test(value varchar(100));
insert into test values ('black''n roll');
SELECT value
FROM test
WHERE 'blackn roll' LIKE CONCAT('%', REPLACE(value, '''', '%'), '%');
You can replace 'blackn roll' with your input string and use nested REPLACE functions if you need to replace more than one character.
The back-slash is MySQL's escape character. You can try the following... Full text indexing gets a little weird because of word terminators.
$sql = "SELECT * FROM arac
INNER JOIN suv_marka ON arac.marka = suv_marka.id
WHERE match(suv_marka.marka) against ('black\'n roll' IN BOOLEAN MODE);"
If that doesn't work, then try looking for the words individually. Word length is also a factor, small words (less than 4 characters by default) are not indexed.
$sql = "SELECT * FROM arac
INNER JOIN suv_marka ON arac.marka = suv_marka.id
WHERE match(suv_marka.marka) against ('black' + 'roll' IN BOOLEAN MODE);"

MySQL query LIKE %...% query returning other results?

So I'm trying to code a PHP script, but we'll just leave it at the SQL part of things since this is where the issue is arising. I've a SELECT * query which should only grab from the rows where the user ID matches, and the badge ID meets their userID followed by an underscore. Although, it's grabbing results that shouldn't be included?
Here's my SQL query:
SELECT *
FROM `user_badges`
WHERE `user_id` = 1
AND `badge_id` LIKE '%1_%'
That should only return badges that start/contain 1_, it is grabbing all the badges that do contain/start with 1_ but it's also grabbing it215. If I search a different user ID, for example my own, it will grab all the badges with 3_ AND it's also grabbing ACH_RoomDecoFurniCount31 which is confusing because it doesn't contain 3_. Maybe there's more to it? Could someone please point me in the right direction.
You need to escape the _ as it's a wildcard character. Your query would should be like this:
SELECT *
FROM `user_badges`
WHERE `user_id` = 1
AND `badge_id` LIKE '%1\_%'
_ is also a wildcard in SQL - A substitute for a single character
_ is also a wildcard character. It means "any single character" (whereas % is "any sequence of characters").
You could escape/quote that _ or use the LOCATE function instead of a pattern match.
WHERE badge_id LIKE '%1\_%'
WHERE locate('1_', badge_id) > 0
_ is a wildcard "_ matches exactly one character." so what you are saying is:
% : starts with anything(or nothing)
1: contains 1
_: has exactly 1 of % (or anything, or nothing)
http://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html

MySQL LIKE for two words

What would be the right SQL statement so that when I search two words, like for example 'text field' in a text box, it will return all results that has 'text' and 'field' in it using the LIKE statement?
I cant find the right terms to make a search.
EDIT : If possible, I want to make it dynamic. Like if a user search 5 words, all 5 words would be in the Like statement. I am trying to achieve a statement
SELECT * FROM table WHERE search (LIKE %searchterm1%) OR (LIKE %searchterm2%) OR (LIKE %searchterm3%) ....
The the words are unordered use a standard logical conjunction (aka AND)
LIKE '%word1%' AND LIKE '%word2%'
If the words are ordered use an implicit conjunction in the search term itself
LIKE '%word1%word2%'
Modify the like wildcards (and quotes) as needed; also consider if a full-text search might be more appropriate.
The correct syntax is;
SELECT * FROM table WHERE (column1 LIKE '%text%' AND column1 LIKE '%field%')
To allow the user to input multiple words, firstly take a look at the problems of SQL injection, but assuming you're using PHP you can explode an input string and implode the resulting array, like this;
$values = explode(" ", $input); // delimiter is a space
$query = "SELECT * FROM table WHERE (column1 LIKE '%" . implode("%' AND column1 LIKE '%",$values) . "%')";
Hope this helps!

How do I use WHERE LIKE properly in a mysql prepared statement?

I'm trying to puzzle out how to add a LIKE to this prepared statement.
SELECT convention_name FROM events_master
WHERE (convention_name = ?);
The goal is to see if the convention name is similar to something that already exists in convention_name. I do understand that it checks for an exact match already.
Thanks in advance.
SELECT convention_name FROM events_master
WHERE (convention_name LIKE 'partofname' + '%');
The % character is a wild char so if you put it in the back it will search for anything that begins with 'partofname' + blah appended to it.
if it's Like = '%partofname%' then partofname could have characters before or after it.
If SQL LIKE clause is used along with % characters, then it will work
like a meta character (*) in UNIX while listing out all the files or
directories at command prompt.
Without a % character, LIKE clause is very similar to equals sign
along with WHERE clause.
$sql = $dbh->prepare(SELECT convention_name FROM events_master
WHERE convention_name LIKE ?");
$sql->execute(array('%'.$yourSearchString.'%'));
For example:
String query = "SELECT convention_name FROM events_master WHERE convention_name like ?";
PreparedStatement stm = conn.prepareStatement(query);
stm.setString(1, "%car%");
Your argument may contain wildchar %, then it will find for example card, scart ..
If this substring match is not enough, then you can use soundex algorithm to find similar strings. See how to compute similarity between two strings in MYSQL for more options.