Home  >  Article  >  Backend Development  >  一句group by可以解决这个查询吗解决思路

一句group by可以解决这个查询吗解决思路

WBOY
WBOYOriginal
2016-06-13 13:08:20851browse

一句group by可以解决这个查询吗
字段 a b c
  1 2 3
  2 3 2
  2 1 5
  5 3 4
  3 5 3
  3 4 7
  4 3 6
查询出 
1、a或b字段 包括3的 
2、a和b的组合只选择一条c值最大的

最后想得到的就是,
  2 3 2
  5 3 4
  3 4 7
表述的不太好,希望能看懂的帮忙解答一下



------解决方案--------------------
可以是可以的……

SQL code

SELECT a, b, MAX( c ) AS c
FROM (
SELECT 1 a, 2 b, 3 c
UNION ALL SELECT 2 , 3, 2
UNION ALL SELECT 2 , 1, 5
UNION ALL SELECT 5 , 3, 4
UNION ALL SELECT 3 , 5, 3
UNION ALL SELECT 3 , 4, 7
UNION ALL SELECT 4 , 3, 6
)t
WHERE t.a =3
OR t.b =3
GROUP BY (
IF( t.a 
                 
              
              
        
            
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn