如何在 sqlite 中构建这个查询?

发布于 2024-09-14 03:27:04 字数 851 浏览 5 评论 0原文

我有带有示例结构和数据的 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);

现在我想(在一个选择查询中)找到所有人女巫:

  1. 根本不投票
  2. 不要投票给文章 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:

  1. Do not vote at all
  2. Do not vote for article 67388

I have no ideas how to do this :/

help :'(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰雪梦之恋 2024-09-21 03:27:04

使用 OR:

SELECT p.*
   FROM PERSON p
 WHERE NOT EXISTS(SELECT NULL
                                FROM VOTE v
                               WHERE v.person_id = p.id) -- no votes at all
       OR NOT EXISTS(SELECT NULL
                                 FROM VOTE v
                                WHERE v.person_id = p.id
                                     AND v.article_id = 67388)

使用 UNION

SELECT p.*
  FROM PERSON p
 WHERE NOT EXISTS(SELECT NULL
                                FROM VOTE v
                               WHERE v.person_id = p.id)
UNION
SELECT p.*
  FROM PERSON p
 WHERE NOT EXISTS(SELECT NULL
                                FROM VOTE v
                               WHERE v.person_id = p.id
                                    AND v.article_id = 67388)

UNION 比 UNION ALL 慢,因为它会删除重复项。如果您想要重复,只需在“UNION”后面添加“ALL”关键字即可。

Using OR:

SELECT p.*
   FROM PERSON p
 WHERE NOT EXISTS(SELECT NULL
                                FROM VOTE v
                               WHERE v.person_id = p.id) -- no votes at all
       OR NOT EXISTS(SELECT NULL
                                 FROM VOTE v
                                WHERE v.person_id = p.id
                                     AND v.article_id = 67388)

Using UNION

SELECT p.*
  FROM PERSON p
 WHERE NOT EXISTS(SELECT NULL
                                FROM VOTE v
                               WHERE v.person_id = p.id)
UNION
SELECT p.*
  FROM PERSON p
 WHERE NOT EXISTS(SELECT NULL
                                FROM VOTE v
                               WHERE v.person_id = p.id
                                    AND v.article_id = 67388)

UNION is slower than UNION ALL, because it removes duplicates. If you want duplicates, just add the "ALL" keyword after "UNION".

盛夏已如深秋| 2024-09-21 03:27:04

第 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文