简体   繁体   中英

Select unique entries of column and filter these by unique entries of another one

I have the following hypothetic table:

col_1 | col_2
a      1
b      1
c      1
d      2
e      2
a      3
b      3
c      3

What I want is the number of unique entries in column 1 (5 - a, b, c, d, e) filtered by the unique entries of column 2. The output should be 2 because

col_1 | col_2
a      1,3
b      1,3
c      1,3
d      2
e      2

is

col_1 | col_2
a,b,c   1,3
d,e     2

I put no value of the entries. I only want the number of output rows. What can I do? I tried something with DISTINCT and GROUP BY but it brought no success.

Thank you!

group_concat has the basic functionality you need, and also supports distinct -ness. You just need to rig together a couple of sub-queries:

SELECT   GROUP_CONCAT(DISTINCT col_1) AS col_1, col_2
FROM     (SElECT   col_1, GROUP_CONCAT(DISTINCT col2) AS col_2
          FROM     my_table
          GROUP BY col_1) t
GROUP BY col2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM