简体   繁体   English

MySQL选择匹配相关表中多行的行

[英]MySQL Select rows that match multiple rows in related table

The following tables are much larger, but have been downsized for ease of the question下面的表格要大得多,但为了便于提问而缩小了尺寸

Table 1 - exercise_rolladex表 1 - Exercise_rolladex

Exercise_ID | Exercise_Name
---------------------------
1            Pushups
2            Turkish Get Ups
3            Squats
4            Ice Skater

Table 2 - exercise_planes表 2 - Exercise_planes

Exercise_Plane_ID | Exercise_Plane
----------------------------------
1                  Sagittal
2                  Frontal
3                  Transverse

Table 3 - exercise_has_planes表 3 - Exercise_has_planes

Exercise_ID | Exercise_Plane_ID
-------------------------------
1             1
2             1
2             2
2             3
3             1
4             2
4             3

My question is: How can I structure a Query where I can find the Exercise_ID of each exercise which has Exercise_Plane_ID=1 AND Exercise_Plane_ID=2.我的问题是:我如何构建一个查询,我可以在其中找到每个练习的 Exercise_ID,其中有 Exercise_Plane_ID=1 AND Exercise_Plane_ID=2。 In other words, find the exercises that have both Sagittal AND Frontal planes of motion.换句话说,找到同时具有矢状面和额状面运动平面的练习。

The Correct Query正确的查询

   SELECT e.Exercise_Name, p.Exercise_Plane 
   FROM exercise_rolladex e
   INNER JOIN exercise_has_planes h ON h.Exercise_ID=e.Exercise_ID
   INNER JOIN exercise_planes p ON p.Exercise_Plane_ID=h.Exercise_Plane_ID
   WHERE p.Exercise_Plane_ID IN(2,1)
   GROUP BY e.Exercise_ID
   HAVING COUNT(DISTINCT h.Exercise_Plane_ID ) >= 2

UPDATE FOLLOW UP QUESTION How then would I include an exclusion?更新后续问题那么我将如何包含排除项? for example, find the exercises with plane_id 2 and 3, but exclude exercises with plane_id 1 (The correct result being "Ice Skater")例如,找到带有plane_id 2和3的练习,但排除带有plane_id 1的练习(正确的结果是“溜冰者”)

I went ahead and answered my own question:我继续回答我自己的问题:

   SELECT e.Exercise_Name, p.Exercise_Plane 
   FROM exercise_rolladex e
   INNER JOIN exercise_has_planes h ON h.Exercise_ID=e.Exercise_ID
   INNER JOIN exercise_planes p ON p.Exercise_Plane_ID=h.Exercise_Plane_ID
   WHERE p.Exercise_Plane_ID IN(2,3)
       AND e.Exercise_ID NOT IN
       (SELECT Exercise_ID FROM exercise_has_planes WHERE Exercise_Plane_ID='1')
   GROUP BY e.Exercise_ID
   HAVING COUNT(DISTINCT h.Exercise_Plane_ID ) >= 2

Thanks to Mr. Brownstones answer from a different question.感谢 Brownstones 先生从一个不同的问题回答。 SQL query to exclude items on the basis of one value 基于一个值排除项目的 SQL 查询

You can do something like this,this will check the plan id with your given input ids and filter out there count in each exercise group if count returns more than one then it means exercise has planes,having clause will fulfill the scenario of having both planes in exercise你可以做这样的事情,这将用你给定的输入 id 检查计划 id 并过滤掉每个练习组中的计数,如果计数返回超过 1,则意味着练习有平面,have 子句将满足拥有两个平面的场景在运动中

SELECT e.Exercise_Name, 
p.Exercise_Plane 
FROM exercise_rolladex e
INNER JOIN exercise_has_planes h ON h.Exercise_ID=e.Exercise_ID
INNER JOIN exercise_planes p ON p.Exercise_Plane_ID=h.Exercise_Plane_ID
WHERE p.Exercise_Plane_ID IN(2,1)
GROUP BY e.Exercise_ID
HAVING COUNT(DISTINCT h.Exercise_Plane_ID ) >= 2

Demo演示

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

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