仅从某些行中选择列的最大值

时间:2017-08-09 15:05:17

标签: mysql sql select subquery inner-join

enter image description here

 select name from myschema.table1 where 
 COL1 = 'A'and 
 COL2= 'B' and 
 LEVEL = (select max(LEVEL) from myschema.table1 where USERTYPE='C')

我知道正在查询表中的最大级别,而不是使用userType“c”的行中的最大级别。我只需要查询具有该usertype的那些。

1 个答案:

答案 0 :(得分:2)

你非常接近。您需要一个关联子句:

select t.name
from myschema.table1 t
where COL1 = 'A'and COL2= 'B' and 
      LEVEL = (select max(t2.LEVEL)
               from myschema.table1 t2
               where t2.col1 = t.col1 and g2.col2 = t.col2 and t2.USERTYPE = 'C'
              );