通过标签和content_type过滤文章..?

时间:2012-03-19 13:34:21

标签: sql tags content-type

我有两个表(带字段):

articles : id,title,description
content_tags : content_type_id, object_id ,tag_id

content_type = https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/

基本上content_type_id类似表格的ID ... object_id是ROW ID(在该表中)..

所以(content_type_id + object_id)唯一标识一行.. !!

现在,每篇文章都使用标记链接到任何content_type。!!

Mercedez Benz ”的文章将包含与标签相同标签的文章 .. !!

所以我的问题是:

A SQL Query to filter ARTICLES, both by `tags` AND `Content_type + 
Object_id` ..??

类似的东西:

SELECT [article_scope] FROM (.....) WHERE tag_id in (1,2,4)

OR

SELECT [article_scope] FROM (.....) 
WHERE content_type_id=[content_type_id of Cars table] 
AND object_id = [id of Mercedez benz]


我想要一个类似于此的查询,但仅包含 JOINS 且没有 INTERSECT

(SELECT jc.id FROM articles as jc  ORDER BY jc.id) 
       INTERSECT 
(SELECT distinct(object_id) FROM content_tags 
  WHERE content_type_id=52 AND tag_id in (
    SELECT tag_id FROM content_tags 
    WHERE content_type_id=14 
    AND object_id in (2)
  )
);

// Where 52 is content_type_id of Articles table
// and 14 is content_type_id of Cars table
// and 2 is RowID of Mercedez Benz

0 个答案:

没有答案