0

I Have This Table called papers

|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      |

I Want The Results to be

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

I Have tried

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

I'm new to sql, tried alot of times but kept failing.

Barmar
  • 669,327
  • 51
  • 454
  • 560

1 Answers1

0
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 
Daniel Marcus
  • 2,616
  • 1
  • 6
  • 13