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

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.

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

SQL OR statement in Wildcard

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?"

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.

how to search several columns in a sql query using concat and UPPER

So I have a DB and a web page where I want to display the result of my db search
I have try a couple ways both work but they are incomplete for what I want to accomplish
I want to be able to search my table columns id,Id_Nombre,Pais,Estado,Ciudad,website all of them with a word or several words from my search text.
this code works but I have to type exactly the word it's case sensitive:
$query = "SELECT * FROM Medios_table WHERE concat(id,Id_Nombre,Pais,Estado,Ciudad,website) LIKE '%$Busqueda%'";
so as a result i I type a word like "People" in my search box and in my data base that word is type in any of those columns as "people" it wont find it.
second code I use works but only using one column
$query = "SELECT * FROM Medios_table WHERE UPPER(Id_Nombre) LIKE UPPER('%$Busqueda%')";
the result for this code is great since it will find it no matter the case used, but I need to extend this type of search to all the other columns, but so far everithing I use does not work.
I have tried:
$query = "SELECT * FROM Medios_table WHERE UPPER(id,Id_Nombre,Pais,Estado,Ciudad,website) LIKE UPPER('%$Busqueda%')";
$query = "SELECT * FROM Medios_table WHERE UPPER(concat(id,Id_Nombre,Pais,Estado,Ciudad,website)) LIKE UPPER('%$Busqueda%')";
$query = "SELECT * FROM Medios_table WHERE concat(UPPER(id,Id_Nombre,Pais,Estado,Ciudad,website)) LIKE UPPER('%$Busqueda%')";
etc.
any help is greatly appreciated, thanks.
Did you try UPPER around each column name? Like this:
$query = "SELECT * FROM Medios_table WHERE concat(UPPER(id),UPPER(Id_Nombre),UPPER(Pais),UPPER(Estado),UPPER(Ciudad),UPPER(website)) LIKE UPPER('%$Busqueda%')";
Your upper(concat(...)) version should work. If you had a problem with that, it's probably just a typo, like you left out a parenthesis or something.
You can't say upper(x,y,z) because upper takes only one parameter. You must do the concat first, then do the upper, i.e. upper(concat(x,y,z)).
That said, this query will be very slow on a big database, because the db engine has to read every record in the table, and then for each one search it character by character. If the table is small or this is done infrequently, that might be acceptable. If the table is big and/or this query will be executed often, you really need a totally different approach.
Update
If you really need to search against any text in a field, where you cannot make any assumptions about the text being searched in or the text being searched for in advance, you should investigate fulltext searches. http://dev.mysql.com/doc/refman/5.0/en/fulltext-natural-language.html
If you will be doing these sort of searches all the time, you might want to build a dictionary, that is, build a list of all the word with all the records that that word occurs in. I think that's basically what fulltext does, so this may or may not gain you anything.
But if you have some foreknowledge, that is, if it's not really that you want to search for arbitrary text occurring anywhere in arbitrary text, than break out the things you want to search for into separate fields.
To take a simple example, suppose you have a field that contains customer full name, like "Fred Smith", "Mary Jones", etc. You want to search for someone by last name. You could search for
where full_name like '%Smith%'
But this would require reading every record in the table. If, instead, you broke the field into first name and last name, then you could search for
where last_name='Smith'
If you have an index on last_name this would be a very fast search.
If you're just trying to look for an entered value in any of several fields, it is much, much faster to do
where estado='Toledo' or ciudad='Toledo' or pais='Toledo'
(assuming that you have indexes on estado, ciudad, and pais), then
where concat(estado, ciudad, pais) like '%Toledo%'
(where no index will do you any good).
If you want to do case-insensitive searches, create an index on upper(estado) instead of on estado, etc. Then "where upper(estado)='TOLEDO'" can use the index.
Also, I'm not sure about MySql, but some database engines can use an index if the LIKE does not begin with a wildcard. That is, "somefield like '%x%'" must read every record in the table. But on some db's, "somefield like 'x%'" can use can index to get to the records where the field starts with "x" and then just process those.
You need full-text search
This link may be useful Mysql full-text search

How to write mysql query to search database?

I'm own a wallpaper website and I'm trying to write a search feature that will search the database for the terms the user is searching for. I have 2 fields in the database I'm searching against TAGS and NAME
The current way I'm doing it is I take the search term divide it up into multiple words and then search the database using those terms. So if a user searches for "New York" my query will look like this
SELECT * FROM wallpapers
WHERE tags LIKE '%New%' OR name LIKE '%new%'
or tags LIKE '%York%' OR name LIKE '%York%'
The issue with that of course is that anything with the term new in it will be pulled up also like say "new car" etc. If I replace the query above with the following code then it's too vague and only like 2 wallpapers will show up
SELECT * FROM wallpapers
WHERE tags LIKE '%New York%' OR name LIKE '%New York%'
Does anyone have a better way to write a search query?
Looks like you want to introduce the concept of relevance.
Try:
select * from (
SELECT 1 as relevance, * FROM wallpapers
WHERE tags LIKE '%New York%' OR name LIKE '%New York%'
union
select 10 as relevance, * FROM wallpapers
WHERE (tags LIKE '%New%' OR name LIKE '%new%')
and (tags LIKE '%York%' OR name LIKE '%York%')
union
select 100 as relevance, * FROM wallpapers
WHERE tags LIKE '%New%' OR name LIKE '%new%'
union
select 100 as relevance, * FROM wallpapers
WHERE tags LIKE '%York%' OR name LIKE '%York%'
)
order by relevance asc
By the way, this will perform very, very poorly if your database grows too large - you want to be formatting your columns consistently so they're all upper case (or all lower case), and you want to avoid wildcards in your where clauses if you possibly can.
Once this becomes a problem, look at full text searching.
Perhaps this is a really dumb question, but could be following possibly what you want?
SELECT * FROM wallpapers
WHERE ( tags LIKE '%New%' OR name LIKE '%new%' )
and ( tags LIKE '%York%' OR name LIKE '%York%' )
This searches for wallpapers which must have both words but anywhere.
Warning Beware of SQL injection this way, when searching for "words" like new'york or new%york. Perhaps the most easy way is to treat all nonalpha/nonnumeric characters as spaces when splitting, such that new#york and similar becomes new and york.
Notes about searching:
Searching this way in databases is plain overkill (full table scan). As long as you only have a few wallpapers this is not a bigger problem. Nearly all current cheap hardware should be able to search through a million wallpapers within a second or so, as long as the database fits into memory.
However with bigger sites where the tags and name information exceeds available RAM, you certainly get a problem. Then it is time to try some other way to search. However what to do heavily depends on the expected use pattern, so to answer that more information is needed.
You can use your first query to do a first selection and after that your can rank the results : a wallpaper with both keywords "New" and "York" will be ranked higher than a wallpaper with only one of the keywords.
If the second query only returns 2 wallpaper, can you put some example of the tags/name of the wallpaper not returned by the query but that you would like to have ?
Is it a problem of uppercase letters ? Spaces ?
I believe you may benefit from Full text indexes. Read up on mysql full text search. You will need to be using MyISAM engine for your tables.
http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html