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';
Related
I'm using this query in my autocomplete feature:
SELECT description FROM my_table WHERE LIKE "%keyword%"
But this query returns the entire content of the field which is sometimes too long.
Is it possible to limit the number of characters before and after "keyword" ?
I suggest using MySQL's REGEXP operator here. For example, to accept a maximum of 10 characters before and after keyword, you could use:
SELECT description
FROM my_table
WHERE col REGEXP '^.{0,10}keyword.{0,10}$';
Note that if you intend to match keyword as a standalone word, you may want to surround it by word boundaries in the regex pattern:
SELECT description
FROM my_table
WHERE col REGEXP '^.{0,10}\\bkeyword\\b.{0,10}$';
To show for example 5 characters before and after you word you can do it using RIGHT, LEFT and SUBSTRING_INDEX
select description, concat(RIGHT(SUBSTRING_INDEX(description, 'keyword', 1),5), 'keyword', LEFT(SUBSTRING_INDEX(description, 'keyword', -1),5) ) as snippet
from my_table
where description like "%keyword%";
Check it here : https://dbfiddle.uk/MZcVJgEL
I've got a search function which creates query. My goal is to search for exact word, so if the phrase is 'hello' it should return only results with 'hello' (not with 'xhello', 'helloxx' etc). My code looks like:
SELECT (...) WHERE x RLIKE '[[:<:]]word[[:>:]]'
And it works for most of the cases, BUT
the problem starts when the phrase is f.e. '$hello', or 'helloĊ' etc - the special chars ruin the functionality.
Is there a way to handle it ?
Try
SELECT * FROM table WHERE x RLIKE '(^|[[:space:]])Hello([[:space:]]|$)'
or
SELECT * FROM table WHERE x RLIKE '(^| )Hello( |$)'
or
SELECT * FROM table WHERE x REGEXP '(^|[[:space:]])Hello([[:space:]]|$)'
or
SELECT * FROM test WHERE name REGEXP '(^| )Hello( |$)'
I use MySql REGEXP:
SELECT * FROM myTable
WHERE title REGEXP "dog|cat|mouse";
The dataset is small, so I am not concerned about performance. And I prefer this over LIKE notation, because I do not have to concatenate a bunch of "LIKE" statements.
However, the above notation uses a logical OR operator. Is there a logical AND operator, so that only rows containing all of the keywords are matched?
(I am using InnoDB so fulltext search not an option)
There's really no nice solution except concatenating ANDs:
SELECT * FROM myTable
WHERE title REGEXP "dog"
AND title REGEXP "cat"
AND title REGEXP "mouse"
The regular expression would otherwise look like this:
SELECT * FROM myTable
WHERE title REGEXP "(dog.*cat.*mouse)|(dog.*mouse.*cat)|(mouse.*dog.*cat)|(mouse.*cat.*dog)|(cat.*dog.*mouse)|(cat.*mouse.*dog)"
You can add several conditions with AND between them:
SELECT * FROM myTable
WHERE title REGEXP "dog" AND title REGEXP "cat" AND title REGEXP "mouse";
Maybe REGEXP is not necessary here and you may use INSTR instead (regular are usually slower):
SELECT * FROM myTable
WHERE INSTR(title, "dog") AND INSTR(title, "cat") AND INSTR(title, "mouse");
AND operation may be only accessible by the mysql-AND:
WHERE title REGEXP 'dog' AND title REGEXP 'cat' AND title REGEXP 'mouse'
this will only show those entries where all the keywords are in the title field. like
title = "this is about mouse, cat and dog"
You can use full text search in boolean mode:
SELECT * FROM table WHERE MATCH(colmun_name) AGAINST('+dogs +cat' IN BOOLEAN MODE);
You must index your table first:
ALTER TABLE table ADD FULLTEXT(column_name);
If your title contains all the search terms, for example:
Anything mouse anything cat anything dog anything
Then you will have all the search terms in a three times repeated title in any order (for example dog, cat and mouse).
Anything mouse anything cat anything DOG anything
Anything mouse anything CAT anything dog anything
Anything MOUSE anything cat anything dog anything
So you can do this without concatenating ANDs with:
SELECT * FROM myTable WHERE REPEAT(title,3) RLIKE '.*dog.*cat.*mouse.*';
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'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%'