I have this table notes.data of my table note and i want to select last notes for each subject(matiere) and for each students(id_etudiant). please help me with my sql code. In short i want result like this result
-
2Most people here want formatted text, not images (or links to images.) – jarlh Jan 19 '18 at 08:38
-
Also show us your current query attempt. – jarlh Jan 19 '18 at 08:38
-
SO is not a code writing service. Please show your efforts (and read [ask]) – HoneyBadger Jan 19 '18 at 08:38
-
1Possible duplicate of [Get top n records for each group of grouped results](https://stackoverflow.com/questions/12113699/get-top-n-records-for-each-group-of-grouped-results) – Perfect Square Jan 19 '18 at 08:43
3 Answers
You can try query like this
SELECT * FROM table_name GROUP BY id_etudiant,id_matiere ORDER BY cod_note desc

- 441
- 5
- 18
-
not working. i want the last (code_note) for each subject and for each students.. my solution now is :select n1.* from notes n1 left join notes n2 ON (n1.id_etudiant= n2.id_etudiant and n1.id_matiere=n2.id_matiere and n1.code_note< n2.code_note) where n2.code_note is null; – Haritiana Andrianarizaka Jan 19 '18 at 12:08
i will explain. i have a table "notes" like this:
code id id_sub id_unit notes session year
1 BEE12 1 1 10.00 normale 2017
2 ABHA 1 1 9.00 normale 2017
3 BEE12 2 1 13.00 normale 2017
4 ABHA 2 1 10.00 normale 2017
5 ABHA 1 1 19.00 rattrapage 2017
but i need a query who return this:
code id id_sub id_unit notes session year
1 BEE12 1 1 10.00 normale 2017
3 BEE12 2 1 13.00 normale 2017
4 ABHA 2 1 10.00 normale 2017
5 ABHA 1 1 19.00 rattrapage 2017
I use this query now:
select n1.* from notes n1 left join notes n2 ON (n1.id= n2.id and n1.id_sub=n2.id_sub
and n1.code < n2.code) where n2.code is null;
it's very simple. select is used to display content, So whatever you want to be displayed first we can alter that by using order by with that coloumn and sort data in ascending or descending order.
try query:
select * from table_name order by id DESC