如何编写返回未选择记录的查询
我编写了一个心理测试应用程序,其中向用户提供了一个单词列表,他/她必须选择十个非常描述自己的单词,然后选择部分描述自己的单词,以及一些与他本人无关的词语。应用程序本身运行良好,但我有兴趣探索元数据的可能性:哪些单词在第一类别中最常被选择,哪些单词在第一类别中从未被选择。第一个查询不是问题,但第二个查询(从未选择过哪些单词)让我难住了。
表结构如下:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许这会更快一些:
Maybe this is a bit faster:
像这样的事情应该可以解决问题:
Something like that should do the trick:
确保您有
选项(类、wid)
的索引。Make sure you have an index on
choices (class, wid)
.