计算mysql的唯一字段

时间:2016-08-31 12:38:40

标签: mysql

我在mysql中有一个这样的表:

dancer1 | dancer2
------------------
jeff    | Julia
john    | Megan
jeff    | Vanessa

我应该很容易,但我如何计算独特舞者的数量(在这种情况下为5)这个问题在这里有问题:MySQL Counting Distinct Values on Multiple Columns,但不完全

3 个答案:

答案 0 :(得分:2)

一个简单的联盟会给你一组所有的舞者:

select DISTINCT dancer1 as dancer from table
union 
select DISTINCT dancer2 as dancer from table

如果你想要数:

select count(all_dancers.dancer) from (    
     select DISTINCT dancer1 as dancer from table
     union 
     select DISTINCT dancer2 as dancer from table
) all_dancers

答案 1 :(得分:0)

尝试此查询

select  count( distinct dancer1,dancer2  ) from table 

答案 2 :(得分:0)

SELECT COUNT(DISTINCT dancer) 
  FROM 
     ( SELECT dancer1 dancer 
         FROM my_table 
        UNION 
       SELECT dancer2 
         FROM my_table
     ) x;