mysql string contained within a search string - mysql

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.

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

SQL Query like that: "some string" LIKE "%"+column+"%"

I've got a table full of substrings, and I need to select all substrings, which are in the search string.
Basically it would be the mirrored form of a where-condition:
WHERE "SearchString" LIKE "%"+ currentColumnValue +"%"
I know, that this is not possible, but for performance reasons I don't want to iterate every single database entry.
Maybe you have got an idea how to solve this kind of problem?
You can do
where 'SearchString' like concat('%', columnValue, '%');
This will be very slow as it would do a table scan and a like-compare on each line, but the result is correct.
http://sqlfiddle.com/#!2/e2b066/1
You can try this
$value = //your value
' select * from tb_name where col_name like %".$value."% '

Mysql searching for string in text

I can't help but think my syntax is wrong.
SELECT * FROM `rework` WHERE rw_pd LIKE 'FIB'
The SQL executes with a result of 0 rows. I KNOW there are rows with that string in that field. Anybody see any stupid mistakes?
You have to specify the wildcard if your have strings which contains FIB chained to other text:
SELECT * FROM `rework` WHERE rw_pd LIKE '%FIB%'
Check also this link for various wildcards usage.
Have you tried adding %, which is a wildcard.
SELECT * FROM rework WHERE rw_pd LIKE '%FIB%'

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.