我有一张articles
的表格,其中有一些文章有孩子(但只有一个孩子的水平)。
我希望有一个视图,显示按父ID分组的文章。这是我到目前为止所做的。
select
`articles`.`id` AS `group_id`, `child`.`id` AS `article_id`, `articles`.`created` AS `date`
from
`articles`
left join `articles` as `child` on (`articles`.`id` = `child`.`parent_id`)
where
`child`.`id` is not null;
以上产生了这些结果。
trend_id,article_id,date
3374,3172,"2015-04-30 18:31:12"
3374,3211,"2015-04-30 18:31:12"
3374,3213,"2015-04-30 18:31:12"
3297,3217,"2015-04-30 18:31:10"
3170,3222,"2015-04-30 18:31:08"
3187,3226,"2015-04-30 18:31:09"
3187,3281,"2015-04-30 18:31:09"
3170,3284,"2015-04-30 18:31:08"
3170,3285,"2015-04-30 18:31:08"
3170,3320,"2015-04-30 18:31:08"
3187,3323,"2015-04-30 18:31:09"
3187,3333,"2015-04-30 18:31:09"
3187,3355,"2015-04-30 18:31:09"
3297,3393,"2015-04-30 18:31:10"
article_id
列不包含父ID,只包含子项(此类连接需要这些子项)。
我需要的是这样的事情。
trend_id,article_id,date
3374,3374,"2015-04-30 18:31:12" <-- trend_id is repeated as article_id
3374,3172,"2015-04-30 18:31:12"
3374,3211,"2015-04-30 18:31:12"
3374,3213,"2015-04-30 18:31:12"
3297,3297,"2015-04-30 18:31:10" <-- trend_id is repeated as article_id
3297,3217,"2015-04-30 18:31:10"
3170,3170,"2015-04-30 18:31:08" <-- trend_id is repeated as article_id
3170,3222,"2015-04-30 18:31:08"
我一直试图修改查询以获得上述结果,但没有成功。我怀疑它需要2个连接,但我没有运气。