高效的短sql查询

5uzkadbs  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(251)

这个问题在这里已经有答案了

sql仅选择列上具有最大值的行[重复](27个答案)
两年前关门了。
我有张table叫纸

|ID | Paper_Code | Subject_Code |
| 0 | 2018/Eng/01|     ENG      |
| 1 | 2018/Eng/02|     ENG      |
| 2 | 2018/CS/01 |     CS       |
| 3 | 2018/Sci/01|     Sci      |
| 4 | 2018/Eng/03|     ENG      |

我希望结果是

|ID | Paper_Code | Subject_Code |
| 4 | 2018/Eng/03|     ENG      |
| 2 | 2018/CS/01 |     CS       |
| 3 | 2018/Sci/01|     Sci      |

我试过了

$sql = "SELECT DISTINCT (MAX(Paper_Code) 
     FROM papers WHERE Subject_Code = (SELECT DISTINCT Subject_Code))";

我是sql新手,尝试了很多次,但都失败了。

mbzjlibv

mbzjlibv1#

select a.* from yourtable a join (
select subject_code, max(id)id
from yourtable
group by subject_code) b 
on a.subject_code=b.subject_code and a.id=b.id

相关问题