怎么做这个查询?

时间:2011-03-26 06:36:41

标签: mysql sql

假设我有一个包含3个字段的表a,b,c:

a b c
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
4 5 6
4 6 5
5 4 6
5 6 4
...

如何只为每组排列获得一个委托(只有一个查询语句)。 结果应该是:

1 2 3
4 5 6

编辑:这是无符号数字[0-9]。

其他例子:

a b
1 2
2 1
2 3
3 2
3 4
4 3

结果应为:

1 2
2 3
3 4

4 个答案:

答案 0 :(得分:2)

SELECT * 
FROM  `table_test` 
WHERE a < b   
AND b < c

答案 1 :(得分:2)

如果您知道表格中包含所有排列,则查询相对简单

SQL> with x as (
  2    select 1 a, 2 b, 3 c from dual
  3    union all
  4    select 1, 3, 2 from dual
  5    union all
  6    select 2, 1, 3 from dual
  7    union all
  8    select 2, 3, 1 from dual
  9    union all
 10    select 3, 1, 2 from dual
 11    union all
 12    select 3, 2, 1 from dual
 13    union all
 14    select 4, 6, 5 from dual
 15    union all
 16    select 4, 5, 6 from dual
 17    union all
 18    select 5, 4, 6 from dual
 19    union all
 20    select 5, 6, 4 from dual
 21  )
 22  select a, b, c
 23    from x
 24   where a < b
 25     and b < c;

         A          B          C
---------- ---------- ----------
         1          2          3
         4          5          6

如果你不知道你将拥有表格中的所有排列,那么生活就会变得更加复杂。但是这样的事情应该有效。

SQL> ed
Wrote file afiedt.buf

  1  with x as (
  2    select 1 a, 2 b, 3 c from dual
  3    union all
  4    select 1, 3, 2 from dual
  5    union all
  6    select 2, 1, 3 from dual
  7    union all
  8    select 2, 3, 1 from dual
  9    union all
 10    select 3, 1, 2 from dual
 11    union all
 12    select 3, 2, 1 from dual
 13    union all
 14    select 4, 6, 5 from dual
 15    union all
 16    select 4, 5, 6 from dual
 17    union all
 18    select 5, 4, 6 from dual
 19    union all
 20    select 5, 6, 4 from dual
 21  )
 22  select a,
 23         b,
 24         c
 25    from (
 26      select a,
 27             b,
 28             c,
 29             min_abc,
 30             middle_abc,
 31             max_abc,
 32             row_number() over( partition by min_abc,
 33                                             middle_abc,
 34                                             max_abc
 35                                    order by a,
 36                                             b,
 37                                             c ) rn
 38        from (
 39          select (case when a not in (min_abc, max_abc)
 40                       then a
 41                       when b not in (min_abc, max_abc)
 42                       then b
 43                       else c
 44                    end) middle_abc,
 45                 min_abc,
 46                 max_abc,
 47                 a,
 48                 b,
 49                 c
 50            from (
 51              select least(a, b, c) min_abc,
 52                     greatest(a,b,c) max_abc,
 53                     a,
 54                     b,
 55                     c
 56                from x ) get_min_max
 57          ) get_middle
 58    ) get_rn
 59*  where rn = 1
SQL> /

         A          B          C
---------- ---------- ----------
         1          2          3
         4          5          6

答案 2 :(得分:1)

接受的解决方案是错误的。试试这个:

select distinct
       least(a,b,c) as min,
       case least(a,b,c)
       when a then least(b,c)
       when b then least(a,c)
       else least(a,b)
       end as mid,
       greatest(a,b,c) as max
from foo;

答案 3 :(得分:0)

请记住,一组数字的总和不依赖于排序:

SELECT a, b, c
FROM foo
GROUP BY SUM(a, b, c)