1

I'm trying to pull results where 1 row from tableA (profiles.category) matches 1 row from tableB (projects.categorysecond) however I'm not getting results.

*IMPORTANT projects.categorysecond will vary between having only 1 category to several category deliminated by ; EXAMPLE: OTTAWA OR OTTAWA;TORONTO;MONTREAL OR OTTAWA;MONTREAL OR TORONTO;MONTREAL

profiles.category will always only have 1 category, never deliminated.

I need to make sure that regardless if I have OTTAWA OR OTTAWA;TORONTO;MONTREAL in profiles.category it PULLS results as long as 1 word matches.

I'm currently trying the following query:

SELECT p.*, up.* FROM users_profiles up INNER JOIN projects p ON find_in_set(up.category, p.categorysecond) > 0
4

1 回答 1

1

FIND_IN_SET() 仅将逗号理解为分隔符。阅读https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_find-in-set

因此,您可以替换;,然后进行比较:

SELECT p.*, up.* 
FROM users_profiles up 
INNER JOIN projects p 
  ON FIND_IN_SET(up.category, REPLACE(p.categorysecond, ';', ',')) > 0

我不得不评论说,如果你想编写表达式来查找分号分隔的字符串中的单个单词,这不是存储数据的好方法。请参阅我对在数据库列中存储分隔列表真的那么糟糕吗?

project_categories您应该在表格中每行存储一个项目类别。那么您的查询会更容易:

SELECT p.*, up.* 
FROM users_profiles up 
INNER JOIN project_categories pc
  ON up.category = pc.category
INNER JOIN projects p 
  ON p.project = pc.project;

使用复合索引project_categories(category,project),这个查询应该得到很好的优化。

于 2017-02-16T02:39:56.473 回答