MySQL Select String

时间:2013-12-23 15:37:52

标签: mysql sql database database-design mysqli

我有一个字符串“Hello Help Me, Stackover flow, Google users, Google Plus,”。 在我的数据库表中我有

ID    TITLE
--    -----

 1.   Stackover flow
 2.   Google Plus
 3.   Help Me
 4.   Another Title
 5.   Hey World

我需要使用MySQL查询获取ID 1,2,3,因为该字符串包含这些单词。

在MySQL中可以实现

修改

select ID, post_title, 
from wp_posts 
where 'Hello Help Me, Stackover flow, Google users, Google Plus,' like concat('%', post_title, '%') and post_type = 'mediawords';

2 个答案:

答案 0 :(得分:2)

您想使用like

select t.id
from table t
where @string like concat('%', title, '%');

这是字符串匹配,因此子字符串可以干扰。如果您想考虑分隔符(', '),请执行:

select t.id
from table t
where concat(', ', @string) like concat('%, ', title, '%, ');

答案 1 :(得分:1)

您可以使用in子句:

select id 
from table
where title in ('Hello Help Me', 'Stackover flow', 'Google users', 'Google Plus')
相关问题