0

我正在尝试从数据表中获取所有数据行,其中一个单词与我的“故事”列中的文本中的一个单词匹配。

表结构:

post_id | user_id | story
----------------------------------
1       | 1       | Hello World What's up?
2       | 4       | Hello guys!
3       | 7       | Working on shareit.me! 

例如:

我想获取所有包含单词 hello 的帖子(我正在寻找不区分大小写的)。

我怎样才能做到这一点?

这是我到目前为止所做的:

// this will be the keyword! so use the variable $filter_tag in the query!
$filter_tag= $_GET['tag'];

//query for getting the users' posts containing the select tag 
$query_posts= "SELECT * FROM posts WHERE user_id= '$user_id' ORDER BY post_id 
DESC";

$result_posts= mysqli_query($connect, $query_posts); 

谢谢!

4

7 回答 7

3
 $query_posts= "SELECT * FROM posts WHERE user_id= '$user_id' AND story LIKE '%$filter_tag%' ORDER BY post_id 
    DESC";
于 2013-05-03T13:03:07.587 回答
1
SELECT * FROM posts WHERE ... AND LOWER(story) LIKE '%hello%'

或者

SELECT * FROM posts WHERE ... 
AND story COLLATE latin1_general_ci_ai LIKE '%hello%'
于 2013-05-03T13:07:34.447 回答
0

我的回答是一样的,我做了一个sqlfiddle:

#supose the tag is Hello

SELECT * FROM posts
where story like "%Hello%";

PS:http ://sqlfiddle.com/#!2/6bfcc1/5

于 2013-05-03T13:12:49.390 回答
0

您可以使用LIKE运算符:

$query_posts = "SELECT * FROM posts WHERE user_id = $user_id AND story LIKE '%yourword%' ORDER BY post_id";

%字符就像通配符。%表示匹配任意数量的字符,其中_(下划线)只匹配一个。

注意:我也从您的user_id列检查中删除了单引号 - 这是一个整数,不想在引号中 - 它们用于字符串。

于 2013-05-03T13:06:32.580 回答
0

不用说不要忘记转义输入。

$query_posts= "SELECT * FROM posts WHERE user_id = '$user_id' AND story LIKE '%".mysql_real_escape_string($filter_tag)."%'";
于 2013-05-03T13:07:17.430 回答
0

这将是:

SELECT * FROM posts WHERE ... AND story LIKE '%hello%'.

通常,“%”是通配符(如 '*')。所以你可以使用 'hello%' '%hello%' 等等。

于 2013-05-03T13:03:05.067 回答
0

最好的解决方案是将 MATCH() AGAINST() 与 FULLTEXT 索引一起使用。

于 2015-07-26T08:59:54.557 回答