我有以下两个表,它们通过 ID 列作为主键相关联。我的目标是查询表 1 中“名称”列中的值,这些值对应于具有表 2 中最大和最小“分数”列值的 User_id。
Table 1:
| ID | Name |
|----|------|
| 1 | Foo |
| 2 | Bar |
| 3 | Zoo |
| 4 | Bar |
| 5 | Foo |
| 6 | Zar |
Table 2:
| ID | Score |
|----|-------|
| 1 | 98 |
| 2 | 67 |
| 3 | 86 |
| 4 | 59 |
| 5 | 75 |
| 6 | 73 |
最终的输出应该是这样的:
| Name | Score |
|------|-------|
| Foo | 98 |
| Bar | 59 |
最佳答案
您可以尝试以下方法 -
select name, score
from table1 t1 join table2 t2 on t1.id=t2.id
where
score=(select max(score) from t2)
or
score=(select min(score) from t2)
关于mysql - SQL根据另一个相关表中列的相应最小值/最大值(值)获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64075638/