如何编写返回未选择记录的查询

发布于 2024-08-08 23:46:55 字数 624 浏览 5 评论 0原文

我编写了一个心理测试应用程序,其中向用户提供了一个单词列表,他/她必须选择十个非常描述自己的单词,然后选择部分描述自己的单词,以及一些与他本人无关的词语。应用程序本身运行良好,但我有兴趣探索元数据的可能性:哪些单词在第一类别中最常被选择,哪些单词在第一类别中从未被选择。第一个查询不是问题,但第二个查询(从未选择过哪些单词)让我难住了。

表结构如下:

table words: id, name
table choices: pid (person id), wid (word id), class (value between 1-6)

大概答案涉及单词和选项之间的左连接,但必须有一个修改语句 - where choice.class = 1 - 这给我带来了问题。编写类似的内容

select words.name
from words left join choices
on words.id = choices.wid
where choices.class = 1
and choices.pid = null

会导致数据库管理器进行长途旅行,无处可去。我使用的是 Delphi 7 和 Firebird 1.5。

TIA, 诺姆

I have written a psychological testing application, in which the user is presented with a list of words, and s/he has to choose ten words which very much describe himself, then choose words which partially describe himself, and words which do not describe himself. The application itself works fine, but I was interested in exploring the meta-data possibilities: which words have been most frequently chosen in the first category, and which words have never been chosen in the first category. The first query was not a problem, but the second (which words have never been chosen) leaves me stumped.

The table structure is as follows:

table words: id, name
table choices: pid (person id), wid (word id), class (value between 1-6)

Presumably the answer involves a left join between words and choices, but there has to be a modifying statement - where choices.class = 1 - and this is causing me problems. Writing something like

select words.name
from words left join choices
on words.id = choices.wid
where choices.class = 1
and choices.pid = null

causes the database manager to go on a long trip to nowhere. I am using Delphi 7 and Firebird 1.5.

TIA,
No'am

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

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

发布评论

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

评论(3

冬天旳寂寞 2024-08-15 23:46:55

也许这会更快一些:

SELECT w.name
FROM words w
WHERE NOT EXISTS
   (SELECT 1
    FROM choices c 
    WHERE c.class = 1 and c.wid = w.id)

Maybe this is a bit faster:

SELECT w.name
FROM words w
WHERE NOT EXISTS
   (SELECT 1
    FROM choices c 
    WHERE c.class = 1 and c.wid = w.id)
两人的回忆 2024-08-15 23:46:55

像这样的事情应该可以解决问题:

SELECT name
FROM words
WHERE id NOT IN
   (SELECT DISTINCT wid   -- DISTINCT is actually redundant
    FROM choices 
    WHERE class == 1)

Something like that should do the trick:

SELECT name
FROM words
WHERE id NOT IN
   (SELECT DISTINCT wid   -- DISTINCT is actually redundant
    FROM choices 
    WHERE class == 1)
悲念泪 2024-08-15 23:46:55
SELECT words.name
FROM
    words
    LEFT JOIN choices ON words.id = choices.wid AND choices.class = 1
WHERE choices.pid IS NULL

确保您有选项(类、wid)的索引。

SELECT words.name
FROM
    words
    LEFT JOIN choices ON words.id = choices.wid AND choices.class = 1
WHERE choices.pid IS NULL

Make sure you have an index on choices (class, wid).

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