我有一個這樣的表在MySQL:計數獨特領域的MySQL
dancer1 | dancer2
------------------
jeff | Julia
john | Megan
jeff | Vanessa
我應該很容易,但我怎麼算獨特的舞者的數量(在這種情況下5)這個問題看起來像在這裏questionned :MySQL Counting Distinct Values on Multiple Columns,但並不完全
我有一個這樣的表在MySQL:計數獨特領域的MySQL
dancer1 | dancer2
------------------
jeff | Julia
john | Megan
jeff | Vanessa
我應該很容易,但我怎麼算獨特的舞者的數量(在這種情況下5)這個問題看起來像在這裏questionned :MySQL Counting Distinct Values on Multiple Columns,但並不完全
一個簡單的聯盟會給你一套所有的舞者:
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
嘗試此查詢
select count(distinct dancer1,dancer2 ) from table
SELECT COUNT(DISTINCT dancer)
FROM
(SELECT dancer1 dancer
FROM my_table
UNION
SELECT dancer2
FROM my_table
) x;
您需要的數量爲5此處給出..? –