简体   繁体   English

MySQL查询问题-匹配联接表上的多个ID

[英]MySQL query issue - matching multiple id's on a joined table

I've got 3 tables: 我有3张桌子:

activities , tools and as these share an:m relation a table activities_tools . 活动工具 ,以及这些活动工具共享一个表activity_tools :m关系。

Users on my website can select available tools on a filter. 我网站上的用户可以在过滤器上选择可用的工具。 I'm now trying to get all activities, where the according tools are available. 我现在正在尝试进行所有活动,在这些活动中可以使用相应的工具。

So eg if I'm selecting tools with the id's 1 and 2 as available, I should be getting activities requiring either no tools, tool 1, tool 2 as well as activities that require both. 因此,例如,如果我选择ID分别为1和2的工具,则应该获得不需要任何工具的活动,工具1,工具2以及同时需要两者的活动。 Activities requiring other tools should be therefore excluded. 因此,应排除需要其他工具的活动。

My query I have tried with so far looks like this: 到目前为止,我尝试过的查询如下:

SELECT activities.*, GROUP_CONCAT(tool) AS tools

FROM activities

LEFT JOIN activities_tools ON activities_tools.activity = activities.id

WHERE ISNULL(tool) OR tool IN (1,2)

GROUP BY activities.id

The (1,2) is the list of id's selected by the user. (1,2)是用户选择的ID的列表。

The ISNULL part solves the problem of getting activities which have no tools applied. ISNULL部分解决了获取未应用工具的活动的问题。 My issue now it though, that eg a tool having tools 1 AND 2 as required is getting returned when only one of them is available. 但是现在我的问题是,当只有一个工具1和2可用时,会返回该工具。

How can I check it against multiple values at once? 如何同时针对多个值检查它? I have tried fiddling with GROUP_CONCAT + HAVING + FIND_IN_SET and more.. but I can't come up with a solution. 我尝试摆弄GROUP_CONCAT + HAVING + FIND_IN_SET等等。但是我无法提出解决方案。

Any help is appreciated. 任何帮助表示赞赏。

I think you can use an extra NOT IN check: 我认为您可以使用额外的NOT IN检查:

SELECT activities.*, GROUP_CONCAT(tool) AS tools

FROM activities

LEFT JOIN activities_tools ON activities_tools.activity = activities.id

WHERE ISNULL(tool) OR (tool IN (1,2) AND activities.id NOT IN (SELECT activity FROM activities_tools WHERE tool NOT IN (1,2)))

GROUP BY activities.id

After trying some more things out and considering the input from here I've found my solution :) 在尝试了更多事情并考虑了此处的输入后,我找到了解决方案:)

SELECT activities.*, GROUP_CONCAT(tool) AS tools
FROM activities
LEFT JOIN activities_tools ON activities_tools.activity = activities.id
GROUP BY activities.id
HAVING ISNULL(tool) OR FIND_IN_SET(1, tools) = 0 OR FIND_IN_SET(2, tools) = 0 etc.

The "OR FIND_IN_SET(1, tools) = 1" parts are the id's to exclude. “ OR FIND_IN_SET(1,tools)= 1”部分是要排除的ID。 This part of the query is generated dynamically in PHP. 这部分查询是用PHP动态生成的。

EDIT: 编辑:

After reading a super cool trick here: 在阅读了一个很棒的技巧之后:

MySQL find_in_set with multiple search string MySQL find_in_set与多个搜索字符串

I have adjusted my solution to this: 我已经针对此问题调整了解决方案:

SELECT activities.*, GROUP_CONCAT(tool) AS tools
FROM activities
LEFT JOIN activities_tools ON activities_tools.activity = activities.id
GROUP BY activities.id
HAVING CONCAT(",", toys, ",") NOT REGEXP ",(x|y|z)," 

Where x , y and z equal to the exclusion id's. 其中x,y和z等于排除ID。 That way the query is way shorter. 这样查询就更短了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM