计数唯一字段 mysql
Count unique fields mysql
我在 mysql 中有这样一个 table:
dancer1 | dancer2
------------------
jeff | Julia
john | Megan
jeff | Vanessa
我应该很简单,但是我如何计算唯一舞者的数量(在本例中为 5)问题看起来像在这里被质疑: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;
我在 mysql 中有这样一个 table:
dancer1 | dancer2
------------------
jeff | Julia
john | Megan
jeff | Vanessa
我应该很简单,但是我如何计算唯一舞者的数量(在本例中为 5)问题看起来像在这里被质疑: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;