SQL OR statement in Wildcard - mysql

My current MySql wild card is LIKE '%A%B%'. This can return values that contain A and B.
Can anyone suggest how can I alter the wildcard statement to return values that contain either A or B.
Thanks in advance.

You can add as many like operator you want within the parenthesis with OR condition like below
select * from tablename where (column_name like '%test%' or same_column_name like '%test1%' or
same_column_name like '%test2%' or same_column_name like '%test3%')
For more info have a look at the below link.
SQL Server using wildcard within IN
Hope that helps you

You can use REGEXP
select * from Table1 where some_column REGEXP '[AB]'
there are lots of different ways in writing this as a regular expression, the above basically means containing A or B.
Generally you want to avoid using REGEXP and LIKE '%something' because the do not use indexes. Thus for large tables these operations would be unusable. When you want to do a search of this kind it's always best to stop and ask: "Have I got the best database design?", "Can I use full text search instead?"

Related

How to make a good sql search with like

For years when I want my user to search some field in my database where he can type anything he wants I use an algorithm to break the words and search each word separetely... a mess.
For example, if the user types in the search box "aaa bbb ccc" I dont like using:
SELECT id
FROM table
WHERE description LIKE '%aaa bbb ccc%'
Cause sometimes the user types things out of order and the query above wouldng find. What I usually do is breaking the string and concatenating it with PHP so the result becomes:
SELECT id
FROM table
WHERE description LIKE '%aaa%'
AND description LIKE '%bbb%'
AND description LIKE '%ccc%'
But today after talk to a friend I was wondering if there is some native way to do this faster using MY SQL?
What you want to do is called full text search and most relational databases support it nowadays, including mysql.
I think you can use REGEXP. For example:
Select * from table where description REGEXP 'aaa|bbb|ccc'
FULLTEXT Searches are really fast.
INSTR or locate works better than REGEXP. But it depends on various factors.
More comparison here
SELECT * from table where INSTR(description, 'aaa') >0
SELECT * from table where LOCATE(description, 'aaa') >0

Query with multiple LIKE with AND condition using REGEXP

I want to search the same field but in multiple values using REGEXP.
Is it possible?
SELECT
*
FROM
posts
WHERE keyword LIKE '%ipad%'
AND keyword LIKE '%iphone%'
AND keyword LIKE '%nokia%' ;
this query is use for OR , i want to convert it to AND condition
SELECT * FROM posts WHERE keyword REGEXP 'ipad|iphone|nokia';
Can your keyword field contain multiple values (devices)? Like ipad;iphone;nokia?
If so, this is bad practice. It violates First Normal Form, and it's a symptom of bad table design. Still, the way you did it with multiple LIKE and AND clauses should work.
Converting this to REGEXP syntax is not easy in MySQL, and definitely not worth the trouble. See this post for more details.

MySQL searching using many 'like' operators: is there a better way?

I have a page that gets all rows from a table in a database, then displays the rows in an HTML table.
That works great, but now I want to implement a 'search' feature. There is a searchbox, and search-terms are separated by a space. I am going to make it search three fields for the search terms, 'make' 'model' and 'type.' These three fields are VARCHAR(30).
Currently if I wanted to search using 3 terms (say 'cool' 'abc' and '123') my query would look something like this.
SELECT * FROM table WHERE make LIKE '%cool%' OR make LIKE '%abc%' OR make LIKE '%123%' OR model LIKE '%cool%' OR model LIKE '%abc%' OR model LIKE '%123%' OR type LIKE '%cool%' OR type LIKE '%abc%' OR type LIKE '%123%'
That looks really bad, and it will get even worse if there are more search terms or more fields to search.
My question to you: is there a better way to search? If so, what?
Use REGEXP instead of like
SELECT * FROM table WHERE make REGEXP 'cool|abc|123' OR model REGEXP 'cool|abc|123' OR type REGEXP 'cool|abc|123';
read more about REGEXP
http://dev.mysql.com/doc/refman/5.1/en/regexp.html
http://www.go4expert.com/forums/showthread.php?t=2337
You should use FULLTEXT. Search SO about it, it is very easy and useful.
You could use a JQuery Table Plugin, like data tables Features on-the-fly filtering which you could use instead of a lengthy sql command. And its free.

MYSQL / SQL 'Exclude' or 'Except'

I want to query everything, Like:
SELECT * FROM
but I want to exclude two columns because their not necessary, but there's too many columns I need to just type it all one by one. Is there an exclude keyword or except keyword or something in SQL or MYSQL?
No. It's better practice to type out all of the fields as opposed to SELECT * FROM ... anyway.
If you're going to be a programmer, you may as well get used to typing :)
Yes, you must type them all out.
Or, if there really are that many you can do it programatically.
SHOW COLUMNS FROM `tablename`
Will give you the list of columns. Use whatever language you are using to pull them out and build the query with them.
Of course, you have to make sure the column names are escaped (what if you have a column name called select?).

MySQL Like function only works on some tables

I have two tables within the same database.
On one I can use the MySQL function LIKE to locate a company name. On the other I always get returned zero results, even when I use a simple match like:
SELECT name
FROM table
WHERE name LIKE 'a%'
Is there a reason for this? Is there a setting or something that needs to be changed?
You could try seeing if you get anything by simply using
SELECT name FROM table WHERE name LIKE '%'
Maybe nothing starts with a?
Please provide an example of the table population and MySQL output. Perhaps you really don't have any names that start with 'a'.