帮助使用 Delphi 7、ADO 和MS Access SQL 语句 - Part Deuce

发布于 2024-10-08 08:47:46 字数 801 浏览 0 评论 0原文

我需要帮助理解为什么我的 SQL 不起作用。或者,如果我需要以不同的方式编写它以获得我需要的结果。正如标题所示,我使用的是带有 ADO 组件的 Delphi 7 和 MS Access 2000 数据库。您可以在此处查看第一部分中的表结构:
有关 Delphi 7、ADO 和 & 的帮助MS Access SQL语句

我目前使用的根据关键字获取所有知识的SQL如下:

select * from (知识 K
内连接knowledge_keywords KKW on KKW.knowledgeid = K.id)
KW.id = KKW.keywordid 上的内连接关键字 KW
where (KW.keyword = 'job') AND (KW.keyword = 'task')

但是,当 Knowledge_keywords 表中明显存在具有相同知识 id 的这两个单词时,这不会返回结果。

但是,如果我使用 OR 而不是 AND 执行相同的 SQL,我会得到我期望的两条记录

select * from (知识 K
内连接knowledge_keywords KKW on KKW.knowledgeid = K.id)
KW.id = KKW.keywordid 上的内连接关键字 KW
其中 (KW.keyword = 'job') AND (KW.keyword = 'task')

感谢您的帮助

I need help understanding why my SQL does not work. Or, if i need to write it differently to get the results i need. As the title suggests, I am using Delphi 7, with ADO components, and a MS Access 2000 database. You can see my table structure from Part I here:
Help with Delphi 7, ADO, & MS Access SQL Statement

The SQL i am currently using to get all knowledge based on keywords is as follows:

select * from (knowledge K
inner join knowledge_keywords KKW on KKW.knowledgeid = K.id)
inner join keywords KW on KW.id = KKW.keywordid
where (KW.keyword = 'job') AND (KW.keyword = 'task')

However, this does not return and results, when there is clearly both of those words in the knowledge_keywords table with the same knowledge id.

However, if i do the same SQL with an OR instead of an AND, i get the two records i expected

select * from (knowledge K
inner join knowledge_keywords KKW on KKW.knowledgeid = K.id)
inner join keywords KW on KW.id = KKW.keywordid
where (KW.keyword = 'job') AND (KW.keyword = 'task')

thanks for any help

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

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

发布评论

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

评论(2

野侃 2024-10-15 08:47:46

这样想:knowledge_keywords 中有多少条记录两者关键字 = 'job'ANDkeyword = 'task'。没有这样的记录。当您使用 AND 时,您要求的是同时满足两个第一个条件和第二个条件的记录。当您使用 OR 时,您要求的是满足一个条件或另一个条件(或两者)的记录。

在这种情况下,OR 表达了你想要的。 AND 表达不同的东西。

您还可以使用 KW.keyword IN ('job', 'task'),它更简洁,也许更清晰。

Think about it this way: How many records are there in knowledge_keywords for which it is true both that keyword = 'job' AND keyword = 'task'. There are no such records. When you use AND you're asking for records that satisfy both the first condition AND the second condition at the same time. When you use OR, you're asking for records that satisfy one condition OR the other one (or both).

In this case, OR expresses what you want. AND expresses something different.

You can also use KW.keyword IN ('job', 'task') which is more concise and, perhaps, clearer.

ぃ弥猫深巷。 2024-10-15 08:47:46

我认为第一个查询不会返回任何结果,是吗?这是因为语音中的“and”与编程中的“and”不同。当您说您想要关键字“job”和“task”时,您实际上是指您想要关键字为“job”或“task”的行。关键字不能同时是“job”和“task”,这样查询将不会返回任何行。您可以将 OR 替换为 IN 的形式,

WHERE KW.Keyword in ('job', 'task')

但这可能不会给您想要的结果。我怀疑您需要找到与这两个关键字匹配的文章。
要检查知识库是否具有两个关键字,您可能需要类似的内容(尽管我不确定 Access 是否接受此操作:

    select
      * 
    from 
      knowledge K
    where
      exists
          (select 'x' from
            knowledge_keywords KKW
            inner join keywords KW on KW.id = KKW.keywordid
          where
            KKW.knowledgeid = K.id and
             KW.keyword = 'job') 
      and exists
          (select 'x' from
            knowledge_keywords KKW
            inner join keywords KW on KW.id = KKW.keywordid
          where
            KKW.knowledgeid = K.id and
             KW.keyboard = 'task') and

[编辑]

另一种方法,可能在 Access 中效果更好(很抱歉我无法测试它)是通过使用这样的计数,我对本例中 K 中的字段做了一个小假设。
这样,您就可以将每个关键字连接到列表中。对于同时具有“工作”和“任务”的知识库文章,它将首先返回两行。然后,这些行将在知识字段中分组,并对行进行计数。仅返回 count 与关键字总数匹配的文章。

可能的问题:当一篇文章有​​相同的关键字(职位)链接两次时,仍然返回。这可以通过使用唯一约束来防止这种情况发生来解决。

    select
      K.ID,
      K.Title,
      K.Content
    from
      knowledge K
      inner join knowledge_keywords KKW on KKW.knowledgeid = K.id)
      inner join keywords KW on KW.id = KKW.keywordid
    where 
      KW.keyword in ('job', 'task')
    group by
      K.ID,
      K.Title,
      K.Content
    having
      count(*) = 2 /* Number of keywords */

I think the first query won't return any result, does it? That's because 'and' in speech differs from 'and' in programming. When you say, you want the keywords 'job' and 'task', you actually mean you want the rows where keyword is either 'job' or 'task'. A keyword cannot be both 'job' and 'task' so that query won't return any rows. You could replace the OR with an IN in the form of

WHERE KW.Keyword in ('job', 'task')

But this probably won't give you the result you want. I suspect you need to find articles that match both keywords.
To check if a knowledgebase has both keywords, you might need something like this (although I'm not sure if Access accepts this:

    select
      * 
    from 
      knowledge K
    where
      exists
          (select 'x' from
            knowledge_keywords KKW
            inner join keywords KW on KW.id = KKW.keywordid
          where
            KKW.knowledgeid = K.id and
             KW.keyword = 'job') 
      and exists
          (select 'x' from
            knowledge_keywords KKW
            inner join keywords KW on KW.id = KKW.keywordid
          where
            KKW.knowledgeid = K.id and
             KW.keyboard = 'task') and

[edit]

A different approach, that might work better in Access (I'm sorry I can't test it) is by using a count like this. I made a small assumption about the fields in K for this example.
This way, you join each keyword in the list. For a knowledge base article that has both 'job' and 'task' it will return two rows at first. These rows are then grouped on the Knowledge fields, and the rows are counted. Only the articles where count matches the total number of keywords are returned.

Possible problem: When an article has the same keyword (job) linked twice, it is still returned. This can be solved by preventing that from happening using unique constraints.

    select
      K.ID,
      K.Title,
      K.Content
    from
      knowledge K
      inner join knowledge_keywords KKW on KKW.knowledgeid = K.id)
      inner join keywords KW on KW.id = KKW.keywordid
    where 
      KW.keyword in ('job', 'task')
    group by
      K.ID,
      K.Title,
      K.Content
    having
      count(*) = 2 /* Number of keywords */
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文