搜索查询将字符串中的特定单词与mysql中另一个字符串的值进行比较

时间:2013-05-15 08:30:57

标签: mysql

我正在制作一个搜索模块,其中我需要搜索与其他用户的详细信息匹配的用户的详细信息,以便我需要比较用户详细信息。有关agegender等详细信息,我已使用like '%search_term%'并且能够获得我想要的结果,但我无法查询用户1发言的详细信息{{1因此它存储在数据库中,就像用逗号分隔的单词作为相同的标记。并且假设其他用户只有一种共同语言english,french,spanish,即共同法语,那么如何根据用户1的详细信息过滤该用户,因此他/她仍然有共同点。以下查询适用于正常值,例如age

latin,hindi,french

如何使用select * from users_profile where age like '%$search_term%' order by created_time DESC 在此版本中进行查询并获得结果?

1 个答案:

答案 0 :(得分:3)

您可以进行自我连接,然后用管道替换其中一个逗号,然后将该正则表达式与rlike一起使用。表现可能很糟糕。

select users_profile.*, users_profile2.id as other_id, 
  users_profile2.language as other_lang from users_profile
  inner join users_profile as users_profile2 on
    users_profile.language rlike 
      replace(users_profile2.language,",","|") 
    and users_profile.id != users_profile2.id;

你可以亲眼看到: http://sqlfiddle.com/#!2/9549f/6