What would be the right SQL statement so that when I search two words, like for example 'text field' in a text box, it will return all results that has 'text' and 'field' in it using the LIKE statement?
I cant find the right terms to make a search.
EDIT : If possible, I want to make it dynamic. Like if a user search 5 words, all 5 words would be in the Like statement. I am trying to achieve a statement
SELECT * FROM table WHERE search (LIKE %searchterm1%) OR (LIKE %searchterm2%) OR (LIKE %searchterm3%) ....
The the words are unordered use a standard logical conjunction (aka AND)
LIKE '%word1%' AND LIKE '%word2%'
If the words are ordered use an implicit conjunction in the search term itself
LIKE '%word1%word2%'
Modify the like wildcards (and quotes) as needed; also consider if a full-text search might be more appropriate.
The correct syntax is;
SELECT * FROM table WHERE (column1 LIKE '%text%' AND column1 LIKE '%field%')
To allow the user to input multiple words, firstly take a look at the problems of SQL injection, but assuming you're using PHP you can explode an input string and implode the resulting array, like this;
$values = explode(" ", $input); // delimiter is a space
$query = "SELECT * FROM table WHERE (column1 LIKE '%" . implode("%' AND column1 LIKE '%",$values) . "%')";
Hope this helps!
Related
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();
When I searched for citroen in search page like that I get result but if I search blackn roll I dont get result because it's written like black'n roll in the table. Some user may also wanna search blackn roll but doesnt get result. How can I fix it? And also rows like v-hr and speacial characters like "&/(). I want the mysql to ignore them.
$sql = "SELECT * FROM arac
INNER JOIN suv_marka ON arac.marka = suv_marka.id
WHERE match(suv_marka.marka) against('citroen')";
If you know the characters (to be replaced) already then, you can use MySQL's REPLACE function to replace them with % and perform LIKE comparison, e.g.:
create table test(value varchar(100));
insert into test values ('black''n roll');
SELECT value
FROM test
WHERE 'blackn roll' LIKE CONCAT('%', REPLACE(value, '''', '%'), '%');
You can replace 'blackn roll' with your input string and use nested REPLACE functions if you need to replace more than one character.
The back-slash is MySQL's escape character. You can try the following... Full text indexing gets a little weird because of word terminators.
$sql = "SELECT * FROM arac
INNER JOIN suv_marka ON arac.marka = suv_marka.id
WHERE match(suv_marka.marka) against ('black\'n roll' IN BOOLEAN MODE);"
If that doesn't work, then try looking for the words individually. Word length is also a factor, small words (less than 4 characters by default) are not indexed.
$sql = "SELECT * FROM arac
INNER JOIN suv_marka ON arac.marka = suv_marka.id
WHERE match(suv_marka.marka) against ('black' + 'roll' IN BOOLEAN MODE);"
let's say I have a string in which the words are separated by 1 or more spaces and I want to use that string in and SQL LIKE condition. How do I make my SQL and tell it to match 1 or more blank space character in my string? Is there an SQL wildcard that I can use to do that?
Let me know
If you're just looking to get anything with atleast one blank / whitespace then you can do something like the following WHERE myField LIKE '% %'
If your dialect allows it, use SIMILAR TO, which allows for more flexible matching, including the normal regular expression quantifiers '?', '*' and '+', with grouping indicated by '()'
where entry SIMILAR TO 'hello +there'
will match 'hello there' with any number of spaces between the two words.
I guess in MySQL this is
where entry RLIKE 'hello +there'
I know this is late, but I never found a solution to this in relation to a LIKE question.
There is no way to do what you're wanting within a SQL LIKE. What you would have to do is use REGEXP and [[:space:]] inside your expression.
So to find one or more spaces between two words..
WHERE col REGEXP 'firstword[[:space:]]+secondword'
Another way to match for one or more space would be to use [].
It's done like this.
LIKE '%[ ]%'
This will match one or more spaces.
you can't do this using LIKE but what you can do, if you know this condition can exist in your data, is as you're inserting the data into the table, use regular expression matching to detect it up front and set a flag in a different column created for this purpose.
I just replace the whitespace chars with '%'. Lets say I want to do a LIKE query on a string like this 'I want to query this string with a LIKE'
#search_string = 'I want to query this string with a LIKE'
#search_string = ("%"+#search_string+"%").tr(" ", "%")
#my_query = MyTable.find(:all, :conditions => ['my_column LIKE ?', #search_string])
first I add the '%' to the start and end of string with
("%"+#search_string+"%")
and then replace other remaining whitespace chars with '%' like so
.tr(" ", "%")
http://www.techonthenet.com/sql/like.php
The patterns that you can choose from are:
% allows you to match any string of any length (including zero length)
_ allows you to match on a single character
I think that the question is not asking to match any spaces but to match two strings one a pattern and the other with wrong number of spaces because of typos.
In my case I have to check two fields from different tables one preloaded and the other filled typed by users so sometimes they don't respect 100% the pattern.
The solution was to use LIKE in the join
Select table1.field
from table1
left join table2 on table1.field like('%' + replace(table2.field,' ','%')+'%')
if the condition:
WHERE myField LIKE '%Hello world%'
doesn't work try
WHERE myField LIKE '%Hello%'
and
WHERE myField LIKE '%world%'
this approach is helpful in a few specific use cases, hope this helps.
MySQL like clause lets wildcard searches like '%keyword%' where keyword is sandwiched within the column value.
For pattern matching where the keyword contains a part of the column value and when %keyword% will not work, we can use INSTR function to do the search.
Example:
Column="Apple"
$keyword = "An Apple a day"
Here, we cannot do Column like '%$keyword%' to make a match but (INSTR('$Keyword', Column)>0 would do the match.
What alternatives (regex?, fulltext search?, lucene?) do we have other than INSTR for pattern-matching such cases (examples?) ?
Update:
Column = "Golden Apple"
$keyword = "An Apple a day"
Even for samples like this, I would like to be able to match the $keyword with column as they have a common term "Apple".
In this case, you can still use LIKE:
'%$keyword%' LIKE CONCAT('%', Column, '%')
Or regular expression:
'%$keyword%' REGEXP Column
I don't understand clearly what exactly you want to do, but try this link to find the help about pattern matching in mysql
Are you writing a procedure,a trigger, or simply a select?
Also consider SOUNDEX and Natural Language (using full text searches and relevance) searches.
I am looking to update the field 'prefix' in my course table to just have the FIRST letter capitalized and not the whole prefix.
Is there any easy way to do this is SQL?? Sample Output could look like 'Aadm' in the database for 'prefix'.
My table looks like:
Sample rows look like:
I have SQL that looks like:
WHERE CONCAT(prefix,code) LIKE '%". $keywords . "%'");
Is it possible to user LOWER on prefix here?
select prefix,
concat(upper(substring(prefix,1,1)),substring(lower(prefix) from 2)) as initcap
from course
try it in select form before update your field
if you're looking for prefixes where all chars are upper case use a regexp
where binary(prefix) regexp '^[A-Z]+$'
EDIT. Update query
update course
set prefix =
concat(upper(substring(prefix,1,1)),substring(lower(prefix) from 2))