MYSQL Search/Wildcards - mysql

I trying to search a MySQL database using PHP, The search works fine but I'm looking for a little help with wildcards:
The data in the field (Model) I am searching is: "A4" (215 results)
My search string is:
SELECT * FROM `temp_comp`.`mvl` WHERE `Model` LIKE '%A4 Avant%'
Is there a way I can still search 'A4 Avant' but this will return any fields that contain 'A4' or 'Avant'
The search term is taken from a csv file so I wanted to try and do this without having to split the two words first and search for 'A4' and/or 'Avant', I have tried the following but get no results:
SELECT * FROM `temp_comp`.`mvl` WHERE `Model` LIKE '%A4%Avant%'
As you may have guessed this is not my normal field so any help would be very much appreciated.

SELECT * FROM temp_comp.mvl WHERE (Model LIKE '%A4%') OR (Model LIKE '%Avant%')
If you want to avoid splitting up the test you can use a regexp:
SELECT * FROM temp_comp.mvl WHERE Model REGEXP 'A4|Avant' <<-- Either '%A4% or %Avant%
SELECT * FROM temp_comp.mvl WHERE Model REGEXP 'A4*Avant' <<-- '%A4%Avant%
See the reference

MySQL doesn't have a built-in way to split strings in a query. Splitting them beforehand and then adding n predicates to the where clause is trivial.
SELECT * FROM `temp_comp`.`mvl`
WHERE `Model` LIKE '%A4%' OR `Model` LIKE '%Avant%';

Here's how you can achieve this.
Modify your query like this:
SELECT * FROM `temp_comp`.`mvl` WHERE `Model` LIKE '%A4%' AND Model LIKE '%Avant%'
The above example will look for elements of the search term in the query in such a manner that they should all be present in the record. And if you want to search for records with either of the search terms, you can replace the word AND with OR and it does the job.
And ideally, you would follow these steps to prepare for the query:
Take the input string to search.
Split it by spaces so you have an array of words
Loop through the array and build your search string.
Use the search string in your query.

Related

How to make mysql LIKE search for exactly single words only

I have a query like this:
SELECT * FROM mytable where description like %STRING%
The problem is: When I search for JAVAit returns me even the records with JAVAscript.
But, JAVA != JavaScript, right ? How can I work around it ?
MySQL's LIKE operator isn't really suitable to detect an exact single word inside a string. But REGEXP, which supports regular expressions, can handle this. Consider the following query:
SELECT * FROM mytable WHERE description REGEXP '[[:<:]]Java[[:>:]]';
This corresponds to matching the pattern \bJava\b, i.e. the word Java by itself.
Demo
Edit:
If you are trying to execute this query using Laravel, then whereRaw should come in handy:
$results = DB::table('mytable')
->whereRaw('description REGEXP ?', ['[[:<:]]Java[[:>:]]'])
->get();

FIND_IN_SET for part of string in Mysql

I need to make a query to a table in my database. Until now I was using FIND_IN_SET because I have strings like this: "twitter,bycicle,car".
Then, I could search by FIND_IN_SET("twitter", nameOfColumn)
But now, I need to search just part of each "set", for example: "twitter>10,bycicle,car"
It still works fine for bycicle and car but if I need to search for twitter, I cannot find it. What is the correct way to do this?
The following query would give you what you want, however use it with caution with respect to the data you have:
SELECT *
FROM table1
WHERE col1 RLIKE 'twitter'
Working Fiddle: http://sqlfiddle.com/#!2/66f538/1
Use LIKE operator:
SELECT *
FROM table1
WHERE nameOfColumn LIKE '%twitter%';

MYSQL LIKE keyword error when selecting with patterns

I need to select all the urls with a string pattern (With out using RLIKE). String pattern is
url needs to be ending "%news.html" SO when i use the following query to select
SELECT * FROM `search_news` WHERE `url` LIKE '%news.html'
this also gives following results which are incorrect
news01.html
news8098.html
Why does the LIKE keyword is behaving like this ? What is the best way to do this without using REGEX patterns ?
Probably you are doing something wrong. Tried your scenario, Its working fine.
create table search_news (url varchar(30));
insert into search_news (url) values
('news8098.html'),
('news01.html'),
('news.html');
SELECT * FROM `search_news` WHERE `url` LIKE '%news.html'
And the output is correct as expected
news.html

How to use prefix wildcards like '*abc' with match-against

I have the following query :
SELECT * FROM `user`
WHERE MATCH (user_login) AGAINST ('supriya*' IN BOOLEAN MODE)
Which outputs all the records starting with 'supriya'.
Now I want something that will find all the records ending with e.g. 'abc'.
I know that * cannot be preappended and it doesn't work either and I have searched a lot but couldn't find anything regarding this.
If I give query the string priya ..it should return all records ending with priya.
How do I do this?
Match doesn't work with starting wildcards, so matching with *abc* won't work. You will have to use LIKE to achieve this:
SELECT * FROM user WHERE user_login LIKE '%abc';
This will be very slow however.
If you really need to match for the ending of the string, and you have to do this often while the performance is killing you, a solution would be to create a separate column in which you reverse the strings, so you got:
user_login user_login_rev
xyzabc cbazyx
Then, instead of looking for '%abc', you can look for 'cba%' which is much faster if the column is indexed. And you can again use MATCH if you like to search for 'cba*'. You will just have to reverse the search string as well.
I believe the selection of FULL-TEXT Searching isn't relevant here. If you are interested in searching some fields based on wildcards like:
%word% ( word anywhere in the string)
word% ( starting with word)
%word ( ending with word)
best option is to use LIKE clause as GolezTrol has mentioned.
However, if you are interested in advanced/text based searching, FULL-TEXT search is the option.
Limitations with LIKE:
There are some limitations with this clause. Let suppose you use something like '%good' (anything ending with good). It may return irrelevant results like goods, goody.
So make sure you understand what you are doing and what is required.

Search prob in mysql query

Is there any way to search like below criteria in mysql?
If any the pl reply.
if i search with "murray's" then it then it will return fine data for "murray's" but it should also return data for "murrays" means same word without apostrophy(').
same way if i search without apostrophy('), the it will search also with apostrophy(').
at last
if search query is "murray's", the it will return "murray's" and "murrays" also.
and
if search query is "murrays", the it will return "murrays" and "murray's" also.
Thanks in advance
Your best bet is to create a Full Text Index on your table.
You can then query it as:
select *
from restaurants
where match(name) against ('murrays')
Barring that, you can use SOUNDEX or SOUNDS LIKE in your query. The following two queries are exactly identical:
select *
from restaurants
where soundex(name) = soundex('murrays')
select *
from restaurants
where name sounds like 'murrays'
Also note that MySQL uses the original SOUNDEX algorithm, not the more recent one. Therefore, it will return arbitrary length strings. If you've used SOUNDEX before, just make sure you take that into account.