sql查询重复记录并取对应最小值

时间:2020-12-31 19:22:40

原表(aa):

id   a                      b

1    22                    456

2    22                    256

3    22                    268

4    23                    236

5    23                    358  

要求查询 a字段重复记录取对应b中最小值

要显示结果:

22                    256

23                    236  

sql语句:SELECT min(b) as b1,a FROM aa group by a

要显示结果:

2     22                    256

4     23                    236  

sql语句:select ta.id,ta.a,ta.b from aa as ta where ta.b in (select min(b) from aa where a=ta.a)