如何在 sqlite 中构建这个查询?
我有带有示例结构和数据的 sqlite3 数据库:
CREATE TABLE person(id INTEGER PRIMARY KEY NOT NULL, name STRING NOT NULL);
INSERT INTO "person" VALUES(1,'Jack');
INSERT INTO "person" VALUES(2,'Daniel');
INSERT INTO "person" VALUES(3,'Sam');
INSERT INTO "person" VALUES(4,'T`lc');
CREATE TABLE vote(person_id INTEGER NOT NULL, article_id NUMBER NOT NULL, FOREIGN KEY(person_id) REFERENCES person(id));
INSERT INTO "vote" VALUES(1,43256);
INSERT INTO "vote" VALUES(1,43436);
INSERT INTO "vote" VALUES(1,67388);
INSERT INTO "vote" VALUES(1,43678);
INSERT INTO "vote" VALUES(2,678);
INSERT INTO "vote" VALUES(2,6788);
INSERT INTO "vote" VALUES(2,67388);
INSERT INTO "vote" VALUES(4,67388);
INSERT INTO "vote" VALUES(4,67658);
现在我想(在一个选择查询中)找到所有人女巫:
- 根本不投票
- 不要投票给文章 67388
我不知道如何做到这一点:/
帮助:' (
I have sqlite3 database with example structure and data:
CREATE TABLE person(id INTEGER PRIMARY KEY NOT NULL, name STRING NOT NULL);
INSERT INTO "person" VALUES(1,'Jack');
INSERT INTO "person" VALUES(2,'Daniel');
INSERT INTO "person" VALUES(3,'Sam');
INSERT INTO "person" VALUES(4,'T`lc');
CREATE TABLE vote(person_id INTEGER NOT NULL, article_id NUMBER NOT NULL, FOREIGN KEY(person_id) REFERENCES person(id));
INSERT INTO "vote" VALUES(1,43256);
INSERT INTO "vote" VALUES(1,43436);
INSERT INTO "vote" VALUES(1,67388);
INSERT INTO "vote" VALUES(1,43678);
INSERT INTO "vote" VALUES(2,678);
INSERT INTO "vote" VALUES(2,6788);
INSERT INTO "vote" VALUES(2,67388);
INSERT INTO "vote" VALUES(4,67388);
INSERT INTO "vote" VALUES(4,67658);
Now I would like to (in one select query) find all persons witch:
- Do not vote at all
- Do not vote for article 67388
I have no ideas how to do this :/
help :'(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 OR:
使用 UNION
UNION 比 UNION ALL 慢,因为它会删除重复项。如果您想要重复,只需在“UNION”后面添加“ALL”关键字即可。
Using OR:
Using UNION
UNION is slower than
UNION ALL
, because it removes duplicates. If you want duplicates, just add the "ALL" keyword after "UNION".第 1 项:
从 id 不在的人员中选择 id、姓名(从投票中选择 person_id)
项目 2:
从 id 不在的人员中选择 id、姓名(从投票中选择 person_id,其中article_id = 67388)
Item 1:
select id,name from person where id not in (select person_id from vote)
Item 2:
select id,name from person where id not in (select person_id from vote where article_id = 67388)