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

一句group by可以解决这个查询吗解决方法

WBOY
WBOYOriginal
2016-06-13 12:58:09864browse

一句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
表述的不太好,希望能看懂的帮忙解答一下


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

<br />
SELECT a, b, MAX( c ) AS c<br />
FROM (<br />
SELECT 1 a, 2 b, 3 c<br />
UNION ALL SELECT 2 , 3, 2<br />
UNION ALL SELECT 2 , 1, 5<br />
UNION ALL SELECT 5 , 3, 4<br />
UNION ALL SELECT 3 , 5, 3<br />
UNION ALL SELECT 3 , 4, 7<br />
UNION ALL SELECT 4 , 3, 6<br />
)t<br />
WHERE t.a =3<br />
OR t.b =3<br />
GROUP BY (<br />
IF( t.a < t.b, CONCAT( t.a,  ',', t.b ) , CONCAT( t.b,  ',', t.a ) )<br />
)<br />

执行结果
a b c
2 3 2
3 4 7
5 3 4

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