How to get related rows between two tables
P粉976737101
P粉976737101 2023-09-14 23:53:48
0
2
606

My form:

表格1
练习

| primaryMuscleGroup | motionName           |
| ------------------ | -------------- ------|
| 胸部               | 双杠臂屈伸           |
| 胸部               | 卧推                 |
| 胸部               | 俯卧撑               |
| 胸部               | 飞鸟                 |
| 腿部               | 深蹲                 |
| 腿部               | 弓步                 |
| 背部               | 硬拉                 |

表格2
健身记录

| name               | motionName           |
| ------------------ | -------------- ------|
| 约翰·史密斯         | 双杠臂屈伸           |
| 莎莉               | 深蹲                 |
| 华莱士             | 弓步                 |
| 克里斯托夫         | 硬拉                 |

The query should return all exercises for muscle groups that a person has not done. For example, if we run the query for the customer "John Smith" we should return:

| primaryMuscleGroup | motionName           |
| 腿部               | 深蹲                 |
| 腿部               | 弓步                 |
| 背部               | 硬拉                 |

If we run the query for customer "Sally" we should return:

| primaryMuscleGroup | motionName           |
| ------------------ | -------------- ------|
| 胸部               | 双杠臂屈伸           |
| 胸部               | 卧推                 |
| 胸部               | 俯卧撑               |
| 胸部               | 飞鸟                 |
| 背部               | 硬拉                 |

P粉976737101
P粉976737101

reply all(2)
P粉642919823

You can use outer joins or use not exists to see if the following query meets your needs:

select * 
from exercises e
where not exists (
  select * from exercises x 
    where exists (
      select * from fitnessRecords fr
      where fr.name = 'john smith' and fr.motionName = x.motionName
    ) and x.primaryMuscleGroup = e.primaryMuscleGroup
)
P粉388945432
SELECT *
FROM excercises t1
WHERE NOT EXISTS ( SELECT NULL 
                   FROM fitnessRecords t2
                   JOIN excercises t3 USING (motionName)
                   WHERE t2.name = '给定的名字'
                     AND t1.primaryMuscleGroup = t3.primaryMuscleGroup )

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=eb216b7579d5fcd0c0ab628717f3d676

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template