答案 0 :(得分:0)
有了这张桌子
CREATE TABLE table1
(`student_id` int, `subject` varchar(10), `marks` int)
;
INSERT INTO table1
(`student_id`, `subject`, `marks`)
VALUES
(1, 'Hindi', 70),
(1, 'English', 80),
(1, 'Maths', 90),
(2, 'Hindi', 40),
(2, 'English', 50),
(2, 'Maths', 60),
(3, 'Hindi', 40),
(3, 'English', 60),
(3, 'Maths', 70)
;
和这个选择状态网
Select
t.student_id,
t.Subject,
t.marks
From
table1 t inner Join
(SELECT student_id,MAX(marks) mmarks FROM table1 Group by student_id) m1
on t.marks = m1.mmarks and t.student_id = m1.student_id;
您得到以下结果:
student_id Subject marks
1 Maths 90
2 Maths 60
3 Maths 70
使用mysql 5.5,您必须自行加入表