FIND_IN_SET for part of string in Mysql - 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%';

Related

What is the best way to use MYSQL Search on term not working

What is the best way to search a database for a phrase such as "Almond Anise Cookie" and return the result?
If I
SELECT *
FROM recipes
WHERE name LIKE '%".$query."%'
and use the phrase "Almond Cookie", nothing is returned as expected. But if I search for "Anise Cookie" the result above is returned.
I've also tried
SELECT *
FROM recipes
WHERE name LIKE '%".$query."%'
OR name LIKE '".$query."%'
OR name LIKE '%".$query."'
with the same failed result.
Using MATCH AGAINST returns everything that contains "Almond" and everything that contains "Cookie" also not a good result. Is there a happy middle in returned results?
You can try using REPLACE. Something like this should work:
SELECT *
FROM recipes
WHERE NAME LIKE REPLACE(' ".$query." ',' ','%');
Note that I purposely add spaces between .$query. to ensure that the replace operation will make your term filled with the wildcard symbol. In the example above:
If $query='almond cookies' then REPLACE(' ".$query." ',' ','%') will become %almond%cookies%.
You can test the fiddle here : https://www.db-fiddle.com/f/kMzp99S8ENbTkYcW5FVdYN/0

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();

Search comma separated string using LIKE in mysql

I have a table with 3 columns something like below,
expert table
id - 1589
name - Jhonny
expert_in - 1,12,8 (Values similar like this)
The experts_in contains another table's foreign key
experts_in table
id - 1
expert_in - painting
I want search experts who are expert in some jobs while searching for experts
SELECT * FROM `experts` WHERE expert_in LIKE 1%
The above query brings all experts with 11,12,13...etc. I want only exact word. I know LIKE will bring all. Is there any way to achieve this without altering table. Thanks in advance.
You should use REGEXP. Try this query:
SELECT * FROM experts
WHERE expert_in REGEXP '[[:<:]]1[[:>:]]';
Output: See Live Demo on SQLFiddle
Note: You can adjust searching string based on your requirement above REGEXP is searching exact word.
if you can alter the data (not the table/schema) you should append and prepend another comma, so you can search with where col like "%,123,%", this will always fit on an exact value. Otherwise you have to use regex with something like ,?123,?

MYSQL Search/Wildcards

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.

mysql string contained within a search string

What I need seems simple, but I haven't been able to pull it off so far. Maybe it's just these late hours, or maybe it's not that simple after all, I don't know any more :)
So, here's the thing. I want to be able to check whether the search string from my site contains any of the fields from a particular column in my database. So, it would be the opposite of the usual one:
mysql_query("
SELECT *
FROM `table`
WHERE `column` LIKE '%{$search}%'
");
which looks for the fields with values where the search string is contained.
What would be the easiest way, using some regular expressions or...?
Thanks a bunch!
Just do it the other way around
SELECT *
FROM `table`
WHERE '{$search}' LIKE concat('%', `column`, '%')
bear with me for the proper syntax for variable escaping for SQL-injection.
your query is also good as you want.
but still you can try this.
You can search your string by using MATCH() AGAINST() statement in mysql.
mysql_query("SELECT * FROM table_name WHERE MATCH(field11,field12) AGAINST ('searchstring')");
Here your field must be having Fulltext datatype.
This may work good.
Thanks.