我有这个表(表名CH);
╔══════╦══════╦══════════╦══════════╗
║ ID_A ║ ID_B ║Category_A║Category_B║
╠══════╬══════╬══════════╬══════════╣
║ 1 ║ 1 ║ 1 ║ 5 ║
║ 1 ║ 2 ║ 1 ║ 5 ║
║ 1 ║ 3 ║ 1 ║ 5 ║
║ 1 ║ 1 ║ 2 ║ 5 ║
║ 1 ║ 3 ║ 2 ║ 5 ║
║ 1 ║ 1 ║ 3 ║ 5 ║
║ 1 ║ 2 ║ 3 ║ 5 ║
║ 2 ║ 1 ║ 1 ║ 4 ║
║ 2 ║ 2 ║ 1 ║ 3 ║
║ 2 ║ 2 ║ 2 ║ 2 ║
║ 2 ║ 2 ║ 3 ║ 1 ║
╚══════╩══════╩══════════╩══════════╝
ID_A = 1&ID_B = 1全部出现在Category_A(= 1,2,3)中 并且 ID_A = 2&ID_B = 2都出现在Category_A(= 1,2,3)
是否有选择这些行的方法? 我尝试根据条件选择“不重复”或“不重复”,但失败了。
select * from ch group by ID_A, ID_B having count(ditinct Category_A)=4;
我希望这样的桌子;
╔══════╦══════╦══════════╦══════════╗
║ ID_A ║ ID_B ║Category_A║Category_B║
╠══════╬══════╬══════════╬══════════╣
║ 1 ║ 1 ║ 1 ║ 5 ║
║ 1 ║ 1 ║ 2 ║ 5 ║
║ 1 ║ 1 ║ 3 ║ 5 ║
║ 2 ║ 2 ║ 1 ║ 3 ║
║ 2 ║ 2 ║ 2 ║ 2 ║
║ 2 ║ 2 ║ 3 ║ 1 ║
╚══════╩══════╩══════════╩══════════╝
答案 0 :(得分:0)
这可能有效
SELECT *
FROM (
SELECT ID_A, ID_B, COUNT(DISTINCT Category_A) as COUNT_CAT_A
FROM CH
GROUP BY ID_A, ID_B
) X
WHERE COUNT_CAT_A = 3