如下所示的表
-----------------------
num1    num2
a        b
a        c
a        b
b        a
c        a
a        c
-----------------------
将(a,b)和(b,a)视作相同
统计出例如(a,b)或者(b,a)出现的频率?
比如上表中(a,b)出现2次,(b,a)出现1次,即(a,b)或者(b,a)次数为3次请教了

解决方案 »

  1.   

    mysql> select * from tx;
    +------+------+
    | num1 | num2 |
    +------+------+
    | a    | b    |
    | a    | c    |
    | a    | b    |
    | b    | a    |
    | c    | a    |
    | a    | c    |
    +------+------+
    6 rows in set (0.00 sec)mysql> select t.num1,t.num2,count(*)
        -> from (
        ->  select num1,num2 from tx
        ->  union all
        ->  select num2,num1 from tx
        -> ) t
        -> group by t.num1,t.num2;
    +------+------+----------+
    | num1 | num2 | count(*) |
    +------+------+----------+
    | a    | b    |        3 |
    | a    | c    |        3 |
    | b    | a    |        3 |
    | c    | a    |        3 |
    +------+------+----------+
    4 rows in set (0.00 sec)mysql>
      

  2.   

    如果不需要重复的显示。mysql> select t.num1,t.num2,count(*)
        -> from (
        ->  select num1,num2 from tx
        ->  union all
        ->  select num2,num1 from tx
        -> ) t
        -> where t.num1<t.num2
        -> group by t.num1,t.num2;
    +------+------+----------+
    | num1 | num2 | count(*) |
    +------+------+----------+
    | a    | b    |        3 |
    | a    | c    |        3 |
    +------+------+----------+
    2 rows in set (0.00 sec)mysql>
      

  3.   

    当您的问题得到解答后请及时结贴.
    http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
      

  4.   


    初次提问,还不晓得要结贴
    再请教一下
    还是上表
    num1    num2
    a       b
    a       b
    a       c
    c       a如何选出(a,c)(c,a)这一类有交叉的
    数据
      

  5.   

    SELECT a.* from tta2 a left join tta2 b on a.num1=b.num2 and a.num2=b.num1 where b.num1 is not null