我有两张表有博客帖子 和其他没有观看表 两者都是通过主键加入。
想要通过最大视图获取数据顺序。
views table -
post id ip address
1 117.02.13.04
1 35.02.13.04
1 86.02.13.04
2 78.02.13.04
2 39.02.13.04
post table -
ID title
1 abc
2 cde
3 fgh
4 ijk
5 asds
答案 0 :(得分:0)
select * from post p
left join
(select post_id,count(ip_address) as count_views from views group by post_id ) v
on p.id=v.post_id ORDER BY v.count_views DESC
我建议您考虑不要使用带有空格的列名,例如ip address
,这就是我在答案中使用ip_address
的原因。
答案 1 :(得分:0)
我认为这会对你有帮助 -
select count(views.post_id), posts.* from posts as posts left views as views on posts.id=views.post_id group by views.post_id order by views.post_id
答案 2 :(得分:-1)
SELECT *, count(post id) as nos
FROM view table, post table
WHERE post id= id
GROUP BY post id
ORDER BY nos desc