具有一搏與此設立(或假設):
我創建了一個balls
表的假設下,爲了讓您知道什麼球ID值你需要過濾,他們必須來自某個地方..而我的列maybeballname
僅僅是爲了給它(有一個balls
表)稍微多一點的意義,像球ID是什麼意思?所以我給它起了一個名字作爲例子。只是一個例子。
-- balls should have its own table
CREATE TABLE balls (
ballid INT
, maybeballname VARCHAR(10)
);
-- sample buckets
CREATE TABLE buckets (
bucketid INT
, ballid INT
);
--populate with data
INSERT INTO balls VALUES(1,'BALL#1');
INSERT INTO balls VALUES(2,'BALL#2');
INSERT INTO balls VALUES(3,'BALL#3');
INSERT INTO balls VALUES(4,'BALL#4');
INSERT INTO balls VALUES(5,'BALL#5');
INSERT INTO balls VALUES(6,'BALL#6');
INSERT INTO balls VALUES(7,'BALL#7');
INSERT INTO balls VALUES(8,'BALL#8');
INSERT INTO balls VALUES(9,'BALL#9');
INSERT INTO buckets VALUES(1,1);--complete (1,3,6)
INSERT INTO buckets VALUES(1,3);
INSERT INTO buckets VALUES(1,6);
INSERT INTO buckets VALUES(1,2);
INSERT INTO buckets VALUES(1,4);
INSERT INTO buckets VALUES(2,5);
INSERT INTO buckets VALUES(2,2);
INSERT INTO buckets VALUES(3,1);
INSERT INTO buckets VALUES(4,1);--complete (1,3,6)
INSERT INTO buckets VALUES(4,3);
INSERT INTO buckets VALUES(4,6);
INSERT INTO buckets VALUES(5,3);
-- this should return bucket id values 1 and 4, right?
SELECT bk.bucketid
FROM buckets AS bk
INNER JOIN balls AS b ON bk.ballid = b.ballid
WHERE b.ballid
IN (1, 3, 6)
GROUP BY bk.bucketid
HAVING COUNT(bk.bucketid) >= 3 -- at least the number of ball ids provided
-- and bucket id 1 has ballid values 1-6
的組合[GROUP_CONCAT()](HTTP ://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat)和[FIND_IN_SET()](http://dev.mysql.com/doc/refman/ 5.0/en/string-functions.html#function_find-in-set)應該可以做到。 – str
你的查詢應該會給你你需要的結果。如果沒有,你能否向我們提供表格結構和一些數據? – Nonym
查詢返回bucketid的包括那些只有一個ballid或ballid的子集的那些。我希望它是一個絕對的條件,像bucketid必須有所有ballid列在它... – David19801