[英]SQLite return records that match a specifc set of records in another table
I have a table that has a one-to-many relationship to another table.我有一个与另一个表具有一对多关系的表。 I want the records from the first table that match a specific set in the second table.我希望第一个表中的记录与第二个表中的特定集相匹配。
CREATE TABLE A (aId INTEGER PRIMARY KEY);
CREATE TABLE B (bId INTEGER PRIMARY KEY, aId INTEGER, c INTEGER);
INSERT INTO A (aId) VALUES (1);
INSERT INTO A (aId) VALUES (2);
INSERT INTO B (bId, aId, c) VALUES (1, 1, 1);
INSERT INTO B (bId, aId, c) VALUES (2, 1, 2);
INSERT INTO B (bId, aId, c) VALUES (3, 2, 2);
INSERT INTO B (bId, aId, c) VALUES (4, 2, 3);
For example, I was aId Where c is 1 and 2. so aId = 1. I don't want it to return aId 2 because while it matches c = 2 it doesn't have c = 1.例如,我在c中是1和2。
SELECT aId FROM B WHERE c IN(1,2);
Gives me 1,1,2.给我1,1,2。 Is there something similar that matches all elements rather than any?是否有类似的东西匹配所有元素而不是任何元素?
If you know how many items are in the IN clause for C,如果您知道 C 的 IN 子句中有多少项,
SELECT aID FROM B WHERE C IN(1,2) GROUP BY aID HAVING COUNT(*)=2;
where the COUNT(*)
is equal to the number of elements in the IN
.其中COUNT(*)
等于IN
中的元素数。 If the filter is more complicated, we'll need another approach.如果过滤器更复杂,我们将需要另一种方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.