如何 select 列与 mysql 中另一列的子集?
How to select columns with subset of another column in mysql?
我要处理这个问题的复杂版本 (How to select columns with same set of values in mysql?)。
在关系R(A,B,C)中,问题是求出"A's with 4 or more common B's"。仅供参考:"AB" 是候选键。
我能做的就是这个
查询:
select * from
(select A, group_concat (B separator ', ') all_b's from R group by A having
(count(B))>3) p1
join
(select A, group_concat (B separator ', ') all_b's from R group by A having
(count(B))>3) p2
on p1.all_b's = p2.all_b's and p1.A <> p2.A;
输出:
Null Set
但是,答案应该是别的东西。
知道如何处理这个吗?
示例数据:
A B C
a1 b1 asdas
a1 b2 sdvsd
a1 b3 sdfs
a1 b4 evevr
a2 b1 jdjd
a2 b2 dkjlfnv
a2 b3 sdfs
a2 b4 evevr
a2 b5 adfgaf
a3 b1 sdfsdf
预期输出
A A count
a1 a2 4
应该是这样的:
SELECT
first.A AS first_A,
second.A AS second_A,
COUNT(*) AS countSameBs
FROM
R first
JOIN
R second ON
first.B = second.B AND
first.A != second.A
GROUP BY
first_A, second_A
HAVING
countSameBs >= 4 AND
first_A < second_A
我要处理这个问题的复杂版本 (How to select columns with same set of values in mysql?)。
在关系R(A,B,C)中,问题是求出"A's with 4 or more common B's"。仅供参考:"AB" 是候选键。
我能做的就是这个
查询:
select * from
(select A, group_concat (B separator ', ') all_b's from R group by A having
(count(B))>3) p1
join
(select A, group_concat (B separator ', ') all_b's from R group by A having
(count(B))>3) p2
on p1.all_b's = p2.all_b's and p1.A <> p2.A;
输出:
Null Set
但是,答案应该是别的东西。 知道如何处理这个吗?
示例数据:
A B C
a1 b1 asdas
a1 b2 sdvsd
a1 b3 sdfs
a1 b4 evevr
a2 b1 jdjd
a2 b2 dkjlfnv
a2 b3 sdfs
a2 b4 evevr
a2 b5 adfgaf
a3 b1 sdfsdf
预期输出
A A count
a1 a2 4
应该是这样的:
SELECT
first.A AS first_A,
second.A AS second_A,
COUNT(*) AS countSameBs
FROM
R first
JOIN
R second ON
first.B = second.B AND
first.A != second.A
GROUP BY
first_A, second_A
HAVING
countSameBs >= 4 AND
first_A < second_A