gpt4 book ai didi

mysql - 高效的短SQL查询

转载 作者:行者123 更新时间:2023-11-29 17:26:42 27 4
gpt4 key购买 nike

我有这张表称为论文

|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新手,尝试了很多次但一直失败。

最佳答案

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

关于mysql - 高效的短SQL查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50916173/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com