I'm using this query to select data:
mysql_query("SELECT * FROM products WHERE product_name LIKE '%".$search."%'");
The only problem is, that it sometimes selects more, than I would like.
For example, I would like to select product "BLA", but my query select product "BLABLA" as well. To be clear, if i wanted to select "Product 1", I don't want the query to select "Product 11".
Does anybody know how to manage that?
Thanks.
Do you just want to search on word boundaries? If so a crude version might be:
SELECT * FROM products WHERE product_name LIKE "% foo %";
Or you could be a bit cleverer and look for word boundaries with the following REGEXP
SELECT * FROM products WHERE product_name RLIKE "[[:<:]]foo[[:>:]]";
Found this question on Google, so I figure that some people may still stumble upon this so here's my pretty inelegant attempt:
SELECT * FROM products
WHERE product_name LIKE 'BLA %' #First word proceeded by more words
OR WHERE product_name LIKE '% BLA' #Last word preceded by other words
OR WHERE product_name LIKE '% BLA %' #Word in between other words
OR WHERE product_name = 'BLA'; #Just the word itself
Not sure about the efficiency or if this covers all cases, so feel free to downvote if this is really inefficient or too inelegant.
Try using regular expressions:
SELECT
*
FROM
`products`
WHERE
product_name regexp '(^|[[:space:]])BLA([[:space:]]|$)';
SELECT *
FROM products
WHERE product_name = 'BLA'
will select exact BLA
SELECT *
FROM products
WHERE product_name LIKE 'BLA%'
will select BLADDER and BLACKBERRY but not REBLAND
To select BLA as the first word of the string, use:
SELECT *
FROM products
WHERE product_name RLIKE '^Bla[[:>::]]'
AND product_name LIKE 'Bla%'
The second condition may improve your query performance if you have an index on product_name.
Remove LIKE keyword and use = for exact match
EDIT
do not forgot to escape user input using mysql_real_escape_string otherwise your query will fail if some one enter quotes inside the input box.
$search=mysql_real_escape_string($search);
mysql_query("SELECT * FROM products WHERE product_name='".$search."'");
Then don't use LIKE, but search for equality.
ie.
mysql_query("SELECT * FROM products WHERE product_name = '".$search."'");
BTW I hope you sanitize/escape $search before using it in a query.
You can just cover all the possible options.
SELECT
*
FROM
`products`
WHERE
`product_name` like 'BLA' -- Column cointains just that word
OR `product_name` like 'BLA %' -- The word appears at the beginning
OR `product_name` like '% BLA' -- The word appears at the end
OR `product_name` like '% BLA %'; -- The word appears in the middle
Use equals (=)?
mysql_query("SELECT * FROM products WHERE product_name = '".$search."'");
If you are looking to match EXACT words don't use LIKE.
EDIT: That clears things up a bit then. Just add a space after the search term. Or even add the hyphen (-) if that is always in the search term.
mysql_query("SELECT * FROM products WHERE product_name LIKE '".$search." -%'");
try to use regular expression in query
mysql_query("SELECT * FROM products WHERE product_name regexp '".$search."'");
If you want to search exact word matching from MySql using LIKE then you use:
SELECT * FROM tableName WHERE columnName LIKE 'your_query' ;
In Laravel Eloquent you can do this like below.
Category::where('name', 'RLIKE ', "[[:<:]]"$words"[[:>:]]");
In raw query, you can search it like this.
SELECT * FROM categories WHERE name RLIKE "[[:<:]]categoryNameHere[[:>:]]";
you can use select query like this ,i also use in cakePHP and it's helpful.
Select * from `users` where username COLLATE latin1_general_cs LIKE '%$email%'
Related
It is possibile to search for rows that does not contain a white space in it?
I have try this but return an error
SELECT * FROM `users` WHERE `name`
NOT LIKE '% %'
in mysql 8 and newer you can use REGEXP_LIKE for more complicated expression too.
SELECT *
FROM users
WHERE REGEXP_LIKE(name, '[[:blank]]')
REGEXP_LIKE
I want to search exact word from string like
id Description
1 This is nice pen looking good
2 This is nice pendrive looking good
Search String : pen
My Current query
SELECT * FROM `table` WHERE Description like '%pen%';
Above Query return both record but I want Only first record. Because pen word exact match with my search string.
Expected Output
1 This is nice pen looking good
Demo
Try using regular expressions:
SELECT
*
FROM
`table`
WHERE
Description regexp '(^|[[:space:]])pen([[:space:]]|$)';
Demo
Or using word boundaries:
SELECT
*
FROM
`table`
WHERE
Description regexp '[[:<:]]pen[[:>:]]';
You can use REGEXP and the [[:<:]] and [[:>:]] word boundary markers:
SELECT
*
FROM
`table`
WHERE
Description REGEXP '[[:<:]]pen[[:>:]]';
SQL Fiddle Demo
you can use fulltext match like as follow
$query="SELECT * FROM table_name WHERE MATCH (column_name) AGAINST
('search_word' IN NATURAL LANGUAGE MODE)" ;
you can select boolean mode also,it return exact word in search result
Try this to get exact word results
SELECT * FROM `table` WHERE Description ='pen';
I want to write a query that title start with A or B
is this correct?
I dont want use OR
I want use it in mysql ,
select * from table where title like `[AB]%`
Use REGEXP instead of LIKE:
SELECT * FROM table
WHERE title REGEXP '^[AB]'
DEMO
Or just use a substring:
SELECT * FROM table
WHERE LEFT(title, 1) IN ('A', 'B');
you can do it like
select * from table where title like `A%` OR title like `B%`
another way is to use regular expression
select * from table where title REGEXP '^(A|B)';
This is correct way:
SELECT * FROM table WHERE title LIKE 'A%' or title LIKE 'B%';
What you wrote should work. or you can use,
select *
from table_name
where title LIKE 'A%' OR title LIKE 'B%'
Unfortunately, the LIKE operator in SQL only supports a limited syntax. It doesn't support a regex style syntax.
You have to do divide it into two expressions:
select * from table where (title like 'A%' or title like 'B%')
Note:
In this case the parenthesis are superfluous, but since OR has a lower precedence than AND I think it is good practice to routinely add parenthesis around ORexpressions in SQL.
Here is what I was want to Optimize this query
SELECT *
FROM users
where `username` like "%abc%"
or `name` like "%abc%"
or `email` like "%abc%"
This search get result from users where any of username , name , email like %abc%
So I tried to use in with like But
I was looking for mysql Like in and I have found this answer stackoverflow :Mysql like in
So the solution was regex
But I want to make the regex not use OR
I want to be like this
SELECT * from users where abc REGEXP 'username|name|email';
So the answer
Do you mean something like this? >>
SELECT * from users where CONCAT(username, ',', name, ',', email) REGEXP 'abc'
I have this MySQL query.
I have database fields with this contents
sports,shopping,pool,pc,games
shopping,pool,pc,games
sports,pub,swimming, pool, pc, games
Why does this like query does not work?
I need the fields with either sports or pub or both?
SELECT * FROM table WHERE interests LIKE ('%sports%', '%pub%')
Faster way of doing this:
WHERE interests LIKE '%sports%' OR interests LIKE '%pub%'
is this:
WHERE interests REGEXP 'sports|pub'
Found this solution here: http://forums.mysql.com/read.php?10,392332,392950#msg-392950
More about REGEXP here: http://www.tutorialspoint.com/mysql/mysql-regexps.htm
The (a,b,c) list only works with in. For like, you have to use or:
WHERE interests LIKE '%sports%' OR interests LIKE '%pub%'
Why not you try REGEXP. Try it like this:
SELECT * FROM table WHERE interests REGEXP 'sports|pub'
You can also use REGEXP's synonym RLIKE as well.
For example:
SELECT *
FROM TABLE_NAME
WHERE COLNAME RLIKE 'REGEX1|REGEX2|REGEX3'
Don't forget to use parenthesis if you use this function after an AND parameter
Like this:
WHERE id=123 and(interests LIKE '%sports%' OR interests LIKE '%pub%')
Or if you need to match only the beginning of words:
WHERE interests LIKE 'sports%' OR interests LIKE 'pub%'
you can use the regexp caret matches:
WHERE interests REGEXP '^sports|^pub'
https://www.regular-expressions.info/anchors.html
Your query should be SELECT * FROM `table` WHERE find_in_set(interests, "sports,pub")>0
What I understand is that you store the interests in one field of your table, which is a misconception. You should definitively have an "interest" table.
Like #Alexis Dufrenoy proposed, the query could be:
SELECT * FROM `table` WHERE find_in_set('sports', interests)>0 OR find_in_set('pub', interests)>0
More information in the manual.
More work examples:
SELECT COUNT(email) as count FROM table1 t1
JOIN (
SELECT company_domains as emailext FROM table2 WHERE company = 'DELL'
) t2
ON t1.email LIKE CONCAT('%', emailext) WHERE t1.event='PC Global Conference';
Task was count participants at an event(s) with filter if email extension equal to multiple company domains.