MySQL LIke statement - multiple words - mysql

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. 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 something like this.
SELECT *
FROM TABLE
WHERE SEARCH (LIKE %searchterm1%)
OR (LIKE %searchterm2%)
OR (LIKE %searchterm3%) ....

Try This. http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp
SELECT *
FROM TABLE
WHERE SEARCH
REGEXP 'searchterm1|searchterm2|searchterm3'

Here's an example of a SQL SELECT statement that uses the LIKE comparison operator
SELECT t.*
FROM mytable t
WHERE t.col LIKE CONCAT('%','cdef','%')
AND t.col LIKE CONCAT('%','hijk','%')
AND t.col LIKE CONCAT('%','mnop','%')
Only rows that have a value in the col column that contains all of the strings 'cdef', 'hijk', and 'mnop' will be returned.
You specifically asked about the LIKE comparison operator. There's also a REGEXP operator that matches regular expressions. And the Full-Text search feature may be a good fit your use case.

Related

Can anyone tell me in mysql How to display employee names whose name DO NOT start with alphabet A?

I am a beginner so please help me.
There are 2 things you need to combine in this case.
Because you didn't provide enough information in your question we have to guess what you mean by name. I'm going to assume that you have a single name column, but that would be unusual.
With strings, to match a character column that is not an exact match, you need to use LIKE which allows for wildcards.
You also need to negate the match, or in other words show things that are NOT (something).
First to match names that START with 'A'.
SELECT * FROM table_name WHERE name LIKE 'A%';
This should get you all the PEOPLE who have names that "Start with A".
Some databases are case sensitive. I'm not going to deal with that issue. If you were using MySQL that is not an issue. Case sensitivity is not universal. In some RDBMS like Oracle you have to take some steps to deal with mixed case in a column.
Now to deal with what you actually want, which is NOT (starting with A).
SELECT * FROM table_name WHERE name NOT LIKE 'A%';
your question should have more detail however you can use the substr function
SELECT name FROM yourtable
WHERE SUBSTR(name,1,1) <> 'A'
complete list of mysql string functions here
mysql docs
NOT REGXP operator
MySQL NOT REGXP is used to perform a pattern match of a string expression expr against a pattern pat. The pattern can be an extended regular expression.
Syntax:
expr NOT REGEXP pat
Query:
SELECT * FROM emp_table WHERE emp_name NOT REGEXP '^[a]';
or
SELECT * FROM emp_table WHERE emp_name NOT REGEXP '^a';

Match hyphenated word

I'm trying to SELECT all rows that have an hyphenated word in a certain column. This is what I have so far:
SELECT * FROM MyTable WHERE list RLIKE '\w-\w'
But it's only returning 1 row, when I know there are a lot more. Any ideas why?
Thank you
| List |
built-in
self-discipline
hang-up
....
EDIT: Not sure if it matters, but list is utf8_unicode_ci
MySQL regular expressions don't support \w (or any other escape sequences for character classes). You must write:
SELECT * FROM MyTable WHERE list RLIKE '[[:alnum:]_]-[[:alnum:]_]'
See the Documentation for details of MySQL regular expressions.
if you want to try and use it without regex then try the wildcard.. not sure if you are not able to use that or not, but that should work too
SELECT * FROM MyTable WHERE list LIKE '%-%'

Where statement with multiple 'Not Like'

I'm trying to write the following statement:
WHERE field LIKE 'Pandora' AND field Not Like 'radio', 'digital', 'internet';
Translation: Select where field is like Pandora and not like radio, digital, or internet.
Is there a way to write this statement without writing Not Like 3 times with ANDs in between?
Thank you
If "field" is not just single words, you would need to do something like this:
SELECT * FROM table WHERE field LIKE '%Pandora%' AND field NOT LIKE '%radio%' AND field NOT LIKE '%internet%' and field NOT LIKE '%digital%';
First of all, your query is redundant, in that if field is LIKE 'pandora', then the other conditions will by default return false.
There is no possible way that field can be equal to 'Pandora', 'radio', 'digital', and 'internet'.
As a result, you can simplify your query using the following example:
SELECT *
FROM example
WHERE field = 'Pandora';
If the two conditions represent two separate fields, then you can use the REGEXP operator to enforce the DRY principle while still allowing for further pattern matching:
SELECT *
FROM example
WHERE field_1 = 'Pandora'
AND field_2 NOT REGEXP '^(radio|digital|internet)$';
If you're searching for specific words, you can use NOT IN()
WHERE field LIKE 'Pandora' AND field NOT IN('radio', 'digital', 'internet');
If you need the wildcard % in your search you'll need to use multiple LIKEs.

whole phrase priority searching

we are implementing a search application
we have implemented a exact word search by the following sql query
SELECT *
FROM jreviews_content
WHERE jr_produits REGEXP '[[:<:]]ryan[[:>:]]'
which works well now we have another requirement
If the user enter a word "Cabaret Mile-End" various result comes up which has Cabaret and Mile-End in it but the row that has whole phrase in it comes at the lastResult.So i want a whole phrase priority wise searching.
As my comment said:
Try using the LIKE structure as follows
SELECT *
FROM jreviews_content
WHERE jr_produits LIKE '%Cabaret Mile-End%'
UNION ALL
SELECT *
FROM jreviews_content
WHERE jr_produits REGEXP '[[:<:]]Cabaret Mile-End[[:>:]]'
I dont know what you put in the last query so I put the value just in between.

Undocumented MySQL usage of the LIKE operator for multiple words

Can't seem to find documentation on a particular formation of SQL using the LIKE operator. Using MySQL, a typical query for multiple words using the LIKE operator may look like this:
SELECT * from table AS t WHERE t.col LIKE '%word1%' AND t.col LIKE '%word2%'
Although the following statement also works, the rows returned will vary depending on the order of the words in the query. For example:
SELECT * from table WHERE col LIKE '%word1%' '%word2%'
executes without the AND boolean, but with different results from:
SELECT * from table WHERE col LIKE '%word2%' '%word1%'
My question is, what is actually happening when using this formation of the query instead of using boolean?
From the manual:
Quoted strings placed next to each other are concatenated to a single
string. The following lines are equivalent:
'a string'
'a' ' ' 'string'
So, what's happening is that '%word1%' '%word2%' is being interpreted as '%word1%%word2%'