SQL 分类帮助

发布于 2024-10-11 10:05:22 字数 623 浏览 1 评论 0原文

我有一个按分类法关联内容的数据库,我正在尝试按分类法查询该内容。它看起来像这样:

Table 1

content_id, content_name

Table 2

content_id, content_taxonmoy

我在查询中尝试的是查找具有两种或多种相关分类法的内容。我的查询如下所示:

SELECT content_id FROM table_1 JOIN table_2 ON table_1.content_id=table_2.content_id WHERE content_taxonomy='ABC' AND content_taxonomy='123'

除了它什么都不返回。后来我尝试使用以下方法进行分组:

SELECT content_id FROM table_1 JOIN table_2 ON table_1.content_id=table_2.content_id WHERE content_taxonomy='ABC' AND content_taxonomy='123'GROUP BY content_id, content_taxonomy

但这也不起作用。请问有什么建议吗?

I have a database that relates content by taxonomy and I am trying to query that content by taxonomy. It looks like this:

Table 1

content_id, content_name

Table 2

content_id, content_taxonmoy

What I am trying in my query is to find content with two or more types of related taxonomy. My query looks like this:

SELECT content_id FROM table_1 JOIN table_2 ON table_1.content_id=table_2.content_id WHERE content_taxonomy='ABC' AND content_taxonomy='123'

Except it returns nothing. I later tried a group by with:

SELECT content_id FROM table_1 JOIN table_2 ON table_1.content_id=table_2.content_id WHERE content_taxonomy='ABC' AND content_taxonomy='123'GROUP BY content_id, content_taxonomy

But that didn't work either. Any suggestions please?

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

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

发布评论

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

评论(1

我只土不豪 2024-10-18 10:05:22
SELECT  *
FROM    content c
WHERE   (
        SELECT  COUNT(*)
        FROM    taxonomy t
        WHERE   t.content_id = c.content_id
                AND t.content_taxonomy IN ('ABC', '123')
        ) = 2

分类法(content_id,content_taxonomy)上创建一个UNIQUE INDEXPRIMARY KEY,以便快速工作。

SELECT  c.*
FROM    (
        SELECT  content_id
        FROM    taxonomy
        WHERE   content_taxonomy IN ('ABC', '123')
        GROUP BY
                content_id
        HAVING  COUNT(*) = 2
        ) t
 JOIN   content c
 ON     c.content_id = t.content_id

在这种情况下,请在taxonomy (content_taxonomy, content_id) 上创建一个UNIQUE INDEXPRIMARY KEY(注意顺序或字段)。

任何一种解决方案都可能比另一种解决方案更有效或更不有效,具体取决于每个内容有多少个分类法以及匹配的概率是多少。

SELECT  *
FROM    content c
WHERE   (
        SELECT  COUNT(*)
        FROM    taxonomy t
        WHERE   t.content_id = c.content_id
                AND t.content_taxonomy IN ('ABC', '123')
        ) = 2

Create a UNIQUE INDEX or a PRIMARY KEY on taxonomy (content_id, content_taxonomy) for this to work fast.

SELECT  c.*
FROM    (
        SELECT  content_id
        FROM    taxonomy
        WHERE   content_taxonomy IN ('ABC', '123')
        GROUP BY
                content_id
        HAVING  COUNT(*) = 2
        ) t
 JOIN   content c
 ON     c.content_id = t.content_id

In this case, create a UNIQUE INDEX or a PRIMARY KEY on taxonomy (content_taxonomy, content_id) (note the order or the fields).

Either solution can be more or less effective than another one, depending on how many taxonomies per content do you have and what is the probability of matching.

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