Query to select var REGEX MANY columns - mysql

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'

Related

how do i write a sql query to select the names that may contain any one of the vowels?

I m trying to query a database with about 2000 entries. I want to select the entries in which the names may contain any one of the vowel.
I tried using the following query, but it gives me those entries that contain all the given characters in that order.
select * from myTable where name like '%a%e%i%';
How do I modify the above query to select those entries with names that may contain at least anyone of the vowels.
Try this for SQL Server:
SELECT * FROM myTable WHERE name LIKE '%[AEIOU]%';
I hope this helps you...
SELECT * FROM myTable WHERE name REGEXP 'a|e';
or.....
SELECT * FROM myTable WHERE name REGEXP 'a|e|i';
In SQL Server, you would do:
where name like '%[aeiou]%';
In MySQL, you would do something similar with a regular expression.
Use OR like this.
This will work for both SQL Server and MySql.
select * from myTable where name like '%a%' OR name like '%e%' OR name like '%i%';
Use LIKE and OR.
Query
select * from myTable
where name like '%a%'
or name like '%e%'
or name like '%i%'
or name like '%o%'
or name like '%u%'

mysql search similar to search in default android contacts app

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.

MySQL SELECT LIKE or REGEXP to match multiple words in one record

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%'

MySQL - How to search for exact word match using LIKE?

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%'

Is there a way to combine IN and LIKE in MySQL?

I'm currently running a query like this:
SELECT *
FROM email
WHERE email_address LIKE 'ajones#%'
OR email_address LIKE 'bsmith#%'
OR email_address LIKE 'cjohnson#%'
The large number of OR's bothers me. Is there a way to condense this up with something akin to an IN operator, e.g.:
SELECT *
FROM email
WHERE email_address LIKE ('ajones#%', 'bsmith#%', 'cjohnson#%')
Or is this just wishful thinking?
You can use RLIKE operator (synonym for REGEXP) as well.
SELECT *
FROM email
WHERE email_address RLIKE 'ajones#|bsmith#|cjohnson#'
There might be some performance penalty due to regex matching, but for simple patterns or small sets it should be not an issue.
For more on RLIKE see http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp
Here's what I recommend: Extract the part of the email address before the # and use that before IN:
SELECT * FROM `email`
WHERE LEFT(`email_address`, LOCATE('#', `email_address`) - 1)
IN ('ajones', 'bsmith', 'cjohnson')