mySQL - 统计多个列的区别

时间:2022-01-12 23:16:33

I have a mySQL DB in which two columns may refer to the same Identifier. One column is PartyA and the other is PartyB (as in counter-parties to an exchange, or nodes in a graph).

我有一个mySQL数据库,其中两列可能引用相同的标识符。一列是PartyA,另一列是PartyB(如交换的对方或图中的节点)。

Curious if there is an easy mySQL query to find the unique count of Party Identifies?

好奇如果有一个简单的mySQL查询来查找Party Identifies的唯一计数?

Currently I pull PartyA, then PartyB, row-bind the two, then count distinct.

目前我拉PartyA,然后PartyB,行绑定两个,然后计数不同。


E.g.:

mySQL  - 统计多个列的区别

Clearly the distinct count of PartyA and PartyB is 9, even through neither PartyA nor PartyB have a distinct count of 9.

很明显,PartyA和PartyB的独特统计数字是9,即使PartyA和PartyB都没有明显的9。

1 个解决方案

#1


SELECT COUNT(1) 
FROM (
   SELECT PartyA FROM theTable 
   UNION 
   SELECT PartyB FROM theTable
) AS pQ;

#1


SELECT COUNT(1) 
FROM (
   SELECT PartyA FROM theTable 
   UNION 
   SELECT PartyB FROM theTable
) AS pQ;