Can groupby solve this query? & nbsp; field & nbsp; a & nbsp; B & nbsp; c & nbsp; & nbsp; 1 & nbsp; 2 & nbsp; can the group by statement in & nbs solve this query?
Field a B c
1 2 3
2 3 2
2 1 5
5 3 4
3 5 3
3 4 7
4 3 6
Query out
1. fields a or B include three
2. select only the one with the largest c value for the combination of a and B.
The last thing you want to get is,
2 3 2
5 3 4
3 4 7
The statement is not very good. I hope you can understand it and help me solve it.
------ Solution --------------------
Yes, yes ......
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 < t.b, CONCAT( t.a, ',', t.b ) , CONCAT( t.b, ',', t.a ) )
)
Execution result
Abc
232
347
534