2013-12-23 133 views
0

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

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

你想用like

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

但这一个字符串匹配,这样子可以干涉。如果要考虑分隔符(', '),然后执行:

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

请检查我的编辑 – coderex

+0

@coderex。 。 。这看起来是正确的逻辑。 –

+0

:)多数民众赞成在工作:D额外的“,”使错误...谢谢你 – coderex

1

可以使用in条款:

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