mysql - 从两列中查找出现次数

时间:2014-08-18 08:43:00

标签: mysql

问题示例:

source | target
apple  |   dog
dog    |   cat
door   |   cat
dog    |   apple
cat    |   dog

结果:

apple dog 2
dog   cat 2
door  cat 1

以下是我的问题,例如:

我正在尝试计算来自源和目标的appledog事件。该 数是2,即; apple dogdog cat

以同样的方式; dog catcat dog,它们会出现2次。

如何使用mysql

执行此操作

数据非常大,所以这只是一个简单的例子。

3 个答案:

答案 0 :(得分:1)

正如我所看到的,您的问题是:计算您的值,使其与列中的顺序无关。因此,对<'foo', 'bar'>应计为<'bar', 'foo'>。为此您可以使用:

SELECT 
  *, 
  COUNT(*) 
FROM 
  test 
GROUP BY 
  LEAST(source, target),
  GREATEST(source, target)

请注意:

  • 将非组列与组功能混合仅适用于MySQL。它是extension,因此服务器可以自由选择任何一行。

答案 1 :(得分:1)

假设SourceTarget加入了ID,我会这样做:

SELECT
    FirstValue,
    SecondValue,
    COUNT(*) As MyCount
FROM
(SELECT
    SourceTable.Value FirstValue,
    TargetTable.Value SecondValue
FROM
    SourceTable 
    INNER JOIN TargetTable ON SourceTable.IDValue = TargetTable.IDValue
UNION ALL
SELECT
    TargetTable.Value FirstValue,
    SourceTable.Value SecondValue
FROM
    TargetTable 
    INNER JOIN SourceTable ON TargetTable.IDValue = SourceTable.IDValue) 
GROUP BY
    FirstValue,
    SecondValue

再次阅读问题我不确定这些是否是同一个表中的两列。如果是,那么查询可以简化为:

SELECT
    FirstValue,
    SecondValue,
    COUNT(*) As MyCount
FROM
(SELECT
    SourceColumn FirstValue,
    TargetColumn SecondValue
FROM
    MyTable
UNION ALL
SELECT
    TargetColumn FirstValue,
    SourceColumn SecondValue
FROM
    MyTable) 
GROUP BY
    FirstValue,
    SecondValue

答案 2 :(得分:1)

 DROP TABLE IF EXISTS my_table;

 CREATE TABLE my_table
 (source VARCHAR(12) NOT NULL
 ,target VARCHAR(12) NOT NULL
 ,PRIMARY KEY(source,target)
 );

 INSERT INTO my_table VALUES
 ('apple','dog'),
 ('dog','cat'),
 ('door','cat'),
 ('dog','apple'),
 ('cat','dog');

 SELECT * FROM my_table;
 +--------+--------+
 | source | target |
 +--------+--------+
 | apple  | dog    |
 | cat    | dog    |
 | dog    | apple  |
 | dog    | cat    |
 | door   | cat    |
 +--------+--------+


 SELECT GREATEST(source,target),LEAST(source,target),COUNT(*) FROM my_table GROUP BY GREATEST(source,target),LEAST(source,target);
 +-------------------------+----------------------+----------+
 | GREATEST(source,target) | LEAST(source,target) | COUNT(*) |
 +-------------------------+----------------------+----------+
 | dog                     | apple                |        2 |
 | dog                     | cat                  |        2 |
 | door                    | cat                  |        1 |
 +-------------------------+----------------------+----------+