SQLite 返回与另一个表中的特定记录集匹配的记录
我有一个表与另一个表具有一对多关系。我想要第一个表中的记录与第二个表中的特定集匹配。
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);
例如,我是aId,其中c是1和2。所以aId = 1。我不希望它返回aId 2,因为当它匹配c = 2时,它没有c = 1。
SELECT aId FROM B WHERE c IN(1,2);
给我1,1, 2.是否有类似的东西可以匹配所有元素而不是任何元素?
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.
SELECT aId FROM B WHERE c IN(1,2);
Gives me 1,1,2. Is there something similar that matches all elements rather than any?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您知道 C 的 IN 子句中有多少项,
其中
COUNT(*)
等于IN
中的元素数。如果过滤器更复杂,我们将需要另一种方法。If you know how many items are in the IN clause for C,
where the
COUNT(*)
is equal to the number of elements in theIN
. If the filter is more complicated, we’ll need another approach.