数据库中有如下表
要找出表中列2中最大的一行,如这一行:A 4 aaaaa 667,其他列1中A的行不要,
一次类推找出列1中B对应列2中最大的行,列1中C对应列2中最大的行,如何写select语句,请大神们帮忙,谢谢!
//以这个为准,上面个别逗号写成中文了,把table_name 替换成你自己的表名
select a.列1,a.列2,
(select 列3 from table_name where 列1=a.列1 and 列2=a.列2) 列3,
(select 列4 from table_name where 列1=a.列1 and 列2=a.列2) 列4
from
(select 列1,max(列2) 列2 from table_name group by 列1) a
select max(列2) from 表名 where 列1 <> A group by 列1 不是很懂意思,我理解的应该是这样了
select 列1,max(列2),列3,列4 from table group by 列1
//把table_name 替换成你自己的表名
select a.列1,a.列2,
(select 列3 from table_name where 列1=a.列1 and 列2=a.列2) 列3,
(select 列4 from table_name where 列1=a.列1 and 列2=a.列2) 列4
from
(select 列1,max(列2) 列2 from table_name group by 列1) a
select t1.* from (select 列1,max(列2) from 表名 t1 group by 列1) t2
left join t1 on t1.列1 = t2.列1
这个才对,漏了一个限制条件
select t1.* from (select 列1,max(列2) a from 表名 t1 group by 列1) t2
left join t1 on t1.列1 = t2.列1 and t1.列2 = t2.a