添加两个 SQlite 查询的结果

发布于 2024-11-09 03:31:43 字数 1085 浏览 0 评论 0原文

我想添加两个单独的计数 SQlite 查询的结果。假设我有 2 个名为 entriesscores 的表,并且有 2 个查询:

SELECT COUNT(1) FROM entries WHERE
    key NOT IN (SELECT key FROM scores)

SELECT COUNT(1) FROM scores WHERE
    value <= threshold

也许我可以这样做来获取它们结果的总和:

SELECT COUNT(1) from (
    SELECT key FROM entries WHERE
        key NOT IN (SELECT key FROM scores)
    UNION ALL
    SELECT key FROM scores WHERE
        value <= threshold
)

但这是否有点太低效了?这会被频繁调用,并且可能会影响 UI 的流畅度。

谢谢。

[编辑]我实际上想做的事情:

我正在制作一个应用程序来帮助学习词汇。 entries 表保存有关单词类型、定义等的“静态”数据。scores 表保存有关您学习单词的程度的信息(例如,表现、安排的下一个单词)复习时间)

为了检查要学习/复习的剩余单词数量,我会计算 scores 表中尚不存在的单词数量(即从未接触过)或者当累积分​​数相当低时(即需要审查)。

我不将这 2 个表合并为 1 个表(这将使我的生活变得更轻松)的原因是,有时我需要通过插入新单词、删除几个单词或删除 entries 表来更新 entries 表。更新他们的内容,但我还没有找到一个简单的方法来做到这一点 。如果我只是执行INSERT OR REPLACE,我将丢失有关分数的信息。

I want to add the results of two separate counting SQlite queries. Suppose I have 2 tables named entries and scores and have 2 queries:

SELECT COUNT(1) FROM entries WHERE
    key NOT IN (SELECT key FROM scores)

SELECT COUNT(1) FROM scores WHERE
    value <= threshold

Maybe I could do something like this to get the sum of their results:

SELECT COUNT(1) from (
    SELECT key FROM entries WHERE
        key NOT IN (SELECT key FROM scores)
    UNION ALL
    SELECT key FROM scores WHERE
        value <= threshold
)

But is this a little too inefficient? This is called pretty often and may interfere with the UI's smoothness.

Thank you.

[EDIT] What I'm actually trying to do:

I'm making an app to help learning vocabulary. The entries table keeps 'static' data about word type, definition, etc. The scores table keeps information about how well you've learned the words (e.g. performance, scheduled next review time)

To check for the number of remaining words to learn/review, I count how many words do not exist in the scores table yet (i.e. never touched) or when the accumulated score is pretty low (i.e. needs reviewing).

The reason I don't merge those 2 tables into 1 (which would make my life much easier) is because sometimes I need to update the entries table either by inserting new words, deleting a few words, or updating their content, and I haven't found a simple way to do that. If I simply do INSERT OR REPLACE, I will lose the information about scores.

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

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

发布评论

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

评论(1

咿呀咿呀哟 2024-11-16 03:31:43

我认为您正在寻找UNION。并集组合两个查询的结果。试试这个(抱歉它没有经过测试,我无权访问 SQLite):

SELECT COUNT(1) FROM
(
    SELECT 1
    FROM entries
    WHERE key NOT IN (SELECT key FROM scores)
    UNION ALL
    SELECT 1
    FROM scores
    WHERE scores.value <= threshold
)

在阅读问题中解释你需要做什么的编辑后,我认为 JOIN 会更合适。这是一种将两个表组合成一个查询的方法。像这样的东西:

SELECT COUNT(1)
FROM entries
LEFT JOIN score
    ON score.key = entries.key
WHERE score.value <= threshold
OR score.key is null

I think you're looking for a UNION. A union combines the results from two queries. Try this (sorry it isn't tested, I don't have access to SQLite):

SELECT COUNT(1) FROM
(
    SELECT 1
    FROM entries
    WHERE key NOT IN (SELECT key FROM scores)
    UNION ALL
    SELECT 1
    FROM scores
    WHERE scores.value <= threshold
)

After reading the edit in your question explaining what you need to do, I think a JOIN would be more appropriate. This is a way of combining two tables into one query. Something like this:

SELECT COUNT(1)
FROM entries
LEFT JOIN score
    ON score.key = entries.key
WHERE score.value <= threshold
OR score.key is null
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文