我有一张单词表。假设它们是“苹果”、“橘子”和“梨”。数据库中的行如下:
------------------------------------------------
|author_id | content |
------------------------------------------------
| 54 | I ate an apple for breakfast. |
| 63 | Going to the store. |
| 12 | Should I wear the orange shirt? |
------------------------------------------------
我正在查找一个innodb表的查询,该表将返回第一行和第三行,因为
content
列包含我列表中的一个或多个单词。我知道我可以为列表中的每个单词查询一次表,并使用like和%通配符,但我想知道是否有一个查询方法来处理这种情况? 最佳答案
编辑:
像这样:
SELECT * FROM yourtable WHERE content LIKE '%apple%' OR content LIKE '%orange%'
您可以通过循环语句来创建WHERE子句条件。
例如:
$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%"
关于sql - MySQL:列包含单词列表中的单词,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4688643/