巧妙的SQL四(T-SQL)

id      name    age
1 a 33
1 b 34
1 c 43
1 d 44
2 aa 22
2 bb 33
3 aaa 23
3 bbb 24
-------------
要得到結果
1 d 44
2 bb 33
3 bbb 24
================================

SQL語句:
1、select id,name,age from table1 a group by id,name,age having age=(select max(age) from table1 where id=a.id)
2、select * from table1 a where age=(select max(age) from table1 where id=a.id)
3、select * from table1 a where not exists(select 1 from table1 where a.id=id and a.age<age) 

注:父select中的字段也可以和子select中的字段聯繫

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章