我有3张桌子。
tbl_article
- > id,headline,summary
tbl_user
---> id,name,location,birth_date
tbl_article_follow
---> id,article_id,user_id
我想编写一个查询来查找文章中的那些记录,这些记录是年龄组16-24,25-34
内的用户所遵循的,依此类推。
我在下面尝试过查询:
SELECT COUNT(*), CASE
WHEN age >= 16 AND age <= 24 THEN '16-24'
WHEN age >=25 AND age <=34 THEN '25-34'
END AS ageband
FROM ( SELECT YEAR(DATE_SUB(NOW(), INTERVAL TO_DAYS(u.birth_date) DAY))
AS age , af.* FROM tbl_user u
INNER JOIN tbl_article_follow af on(u.id = af.user_id)) as tbl
where age >= 16 GROUP BY ageband
但是输出在age_group中有多个相同的story_id 有人可以帮我搞清楚吗?