2017-10-12 111 views
-1

我有以下表格:我怎样才能返回结果?

标签:

| id | title | 

:因为我们正在重新使用

| tag_id (FK tags.id) | category_tag_id (FK tags.id) | 

category_tag_id引用tags.id标签表(我们可以一类简单地分配给标签与此表

文章

| id | title | body | etc... | 

articles_tags

| article_id (FK articles.id) | tag_id (FK tags.id) | 

我试图创造出选择关联到这个技能的所有类别中查询。我们通过搜索是在articles_tags表链接标签抢类别,那么我们只需要检查被检查类别表相关联的那些标签哪些类别。

这里是我试过

select tags.* 
FROM tags 
INNER JOIN categories ON tags.id = categories.tag_id 
INNER JOIN article_tags ON article_tags.article_id = ID_OF_ARTICLE_WE_WANT_CATEGORIES_FROM 

我也尝试添加一个

where article_id = ID_OF_ARTICLE_WE_WANT_CATEGORIES_FROM 

然而,这似乎查询返回相关的技能的所有类别,而不仅仅是那些。

+0

见https://meta.stackoverflow.com/questions/333952/为什么-应该-I-提供-AN-MCVE换什么,似乎对我将要-A-极简单的SQL查询 – Strawberry

回答

0
select tags.* 
FROM tags 
INNER JOIN categories ON tags.id = categories.tag_id 
INNER JOIN article_tags ON article_tags.article_id = ID_OF_ARTICLE_WE_WANT_CATEGORIES_FROM 

在您的查询中,表article_tags实际上并未加入其他任何内容。看着你所提供的模式,似乎article_tags通过与tagsarticle_tags.tag_id - >tags.id

尝试此查询:

SELECT tags.* 
FROM tags 
INNER JOIN categories ON tags.id = categories.tag_id 
INNER JOIN article_tags ON article_tags.article_id = tags.id 
WHERE article_tags.article_id = :user_input 
相关问题