Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Column Contains Word From List of Words

Tags:

sql

mysql

innodb

I have a list of words. Lets say they are 'Apple', 'Orange', and 'Pear'. I have rows in the database like this:

------------------------------------------------
|author_id   |  content                        |
------------------------------------------------
| 54         | I ate an apple for breakfast.   |
| 63         | Going to the store.             |
| 12         | Should I wear the orange shirt? |
------------------------------------------------

I'm looking for a query on an InnoDB table that will return the 1st and 3rd row, because the content column contains one or more words from my list. I know I could query the table once for each word in my list, and use LIKE and the % wildcard character, but I'm wondering if there is a single query method for such a thing?

like image 220
mellowsoon Avatar asked Jan 14 '11 06:01

mellowsoon


People also ask

How do you check if a string contains a substring in MySQL?

MySQL query string contains using INSTRINSTR(str, substr) function returns the index of the first occurrence of the substring passed in as parameters. Here str is the string passed in as the first argument, and substr is the substring passed in as the second argument.

How do you check if a column contains a value in MySQL?

$query = "SELECT * FROM my_table WHERE categories LIKE '%2%'"; Instead you should consider the find_in_set mysql function which expects a comma separated list for the value. Show activity on this post. $query = "SELECT * FROM my_table WHERE categories LIKE '%2%'"; $rows = mysql_query($query);

How do I match a string in MySQL?

STRCMP() function in MySQL is used to compare two strings. If both of the strings are same then it returns 0, if the first argument is smaller than the second according to the defined order it returns -1 and it returns 1 when the second one is smaller the first one.

How do I find a character in a string MySQL?

MySQL POSITION() function MySQL POSITION() returns the position of a substring within a string. A string whose position within another string (str) is to be retrieved. Keyword. A string within which the position of the substring (substr) is to be retrieved.


2 Answers

MySQL (I believe the 5.0 version) added the ability to use regular expressions in your SQL.

Check out: http://www.brainbell.com/tutorials/MySQL/Using_MySQL_Regular_Expressions.htm

SELECT author_id, content
FROM AuthorTableName
WHERE content REGEXP 'Apple|Orange|Pear'
ORDER BY author_id;
like image 187
John K. Avatar answered Oct 13 '22 08:10

John K.


EDIT:

Something like this:

SELECT * FROM yourtable WHERE content LIKE '%apple%' OR content LIKE '%orange%'

You can loop your words to create WHERE clause conditions.

For Example:

$words = array( 'apple', 'orange' );
$whereClause = '';
foreach( $words as $word) {
   $whereClause .= ' content LIKE "%' . $word . '%" OR';
}

// Remove last 'OR'
$whereClause = substr($whereClause, 0, -2);

$sql = 'SELECT * FROM yourtable WHERE' . $whereClause;

echo $sql;

Output:

SELECT * FROM yourtable WHERE content LIKE "%apple%" OR content LIKE "%orange%" 
like image 24
Naveed Avatar answered Oct 13 '22 10:10

Naveed