SQL 分类帮助
我有一个按分类法关联内容的数据库,我正在尝试按分类法查询该内容。它看起来像这样:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
分类法(content_id,content_taxonomy)
上创建一个UNIQUE INDEX
或PRIMARY KEY
,以便快速工作。在这种情况下,请在
taxonomy (content_taxonomy, content_id)
上创建一个UNIQUE INDEX
或PRIMARY KEY
(注意顺序或字段)。任何一种解决方案都可能比另一种解决方案更有效或更不有效,具体取决于每个内容有多少个分类法以及匹配的概率是多少。
Create a
UNIQUE INDEX
or aPRIMARY KEY
ontaxonomy (content_id, content_taxonomy)
for this to work fast.In this case, create a
UNIQUE INDEX
or aPRIMARY KEY
ontaxonomy (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.