I need to make a search in database-table. If i enter "a" in search box it should suggest "antony", "Thomas Antony" , "George John Ani".How To write the search query for such a result.
It's quite simple:
Contains a:
SELECT *
FROM profile
WHERE name LIKE CONCAT('%', `a` , '%');
Starts with a:
SELECT *
FROM profile
WHERE name LIKE CONCAT(`a` , '%');
//answer edited
for the purpose you need to use REGEXP,SQLite3 supports the REGEXP operator, check this out
SELECT * FROM `profile` WHERE `name` REGEXP '[[:<:]]A'
Hope this will guide you.
Related
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.
ll've looked into the like function, as well as other solutions but I think what I'm trying to do can be accomplished with one query. Here is what I have so far.
"SELECT name FROM tblname where name like '%jons%'"
i excepted answer fetch the name "jon" how can i do. please help me
SELECT `name`
FROM `tblname`
WHERE 'jons' LIKE CONCAT('%', `name`, '%');
Also you can use two LIKE methods with OR operator.
SELECT name
FROM tblname
WHERE name like 'jon%';
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'
The field table.name contains 'Stylus Photo 2100' and with the following query
SELECT `name` FROM `table` WHERE `name` LIKE '%Stylus 2100%'
I get no results. Of course i would if i searched
SELECT `name` FROM `table` WHERE `name` LIKE '%Photo 2100%'
How can I select the record by searching 'Stylus 2100' ?
Thanks
Well if you know the order of your words.. you can use:
SELECT `name` FROM `table` WHERE `name` REGEXP 'Stylus.+2100'
Also you can use:
SELECT `name` FROM `table` WHERE `name` LIKE '%Stylus%' AND `name` LIKE '%2100%'
I think that the best solution would be to use Regular expressions. It's cleanest and probably the most effective. Regular Expressions are supported in all commonly used DB engines.
In MySql there is RLIKE operator so your query would be something like:
SELECT * FROM buckets WHERE bucketname RLIKE 'Stylus|2100'
I'm not very strong in regexp so I hope the expression is ok.
Edit
The RegExp should rather be:
SELECT * FROM buckets WHERE bucketname RLIKE '(?=.*Stylus)(?=.*2100)'
More on MySql regexp support:
http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp
You can just replace each space with %
SELECT `name` FROM `table` WHERE `name` LIKE '%Stylus%2100%'
The correct solution is a FullText Search (if you can use it) https://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html
This nearly does what you want:
SELECT * FROM buckets WHERE bucketname RLIKE '(Stylus|2100)+.*(Stylus|2100)+';
SELECT * FROM buckets WHERE bucketname RLIKE '(Stylus|2100|photo)+.*(Stylus|2100|photo)+.*(Stylus|2100|photo)+.*';
But this will also match "210021002100" which is not great.
you need to do something like this,
SELECT * FROM buckets WHERE bucketname RLIKE 'Stylus.*2100';
or
SELECT * FROM buckets WHERE bucketname RLIKE '(Stylus)+.*(2100)+';
Assuming that your search is stylus photo 2100. Try the following example is using RLIKE.
SELECT * FROM `buckets` WHERE `bucketname` RLIKE REPLACE('stylus photo 2100', ' ', '+.*');
EDIT
Another way is to use FULLTEXT index on bucketname and MATCH ... AGAINST syntax in your SELECT statement. So to re-write the above example...
SELECT * FROM `buckets` WHERE MATCH(`bucketname`) AGAINST (REPLACE('stylus photo 2100', ' ', ','));
SELECT `name` FROM `table` WHERE `name` LIKE '%Stylus % 2100%'
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%'