php - 关于关联查询的sql查询不全的问题

查看:71
本文介绍了php - 关于关联查询的sql查询不全的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

就是两个表,一个是文章表:attop_article,一个是分类表:attop_article_type。

文章表:attop_article_type

分类表:attop_article_type

关联的是attop_article表中的type与attop_article_type表中的id。希望统计分类下面所有的文章数量是多少。
目前所用的sql是:

SELECT attop_article_type.id,COUNT(attop_article.type)FROM attop_article_type LEFT JOIN attop_article ON attop_article_type.id=attop_article.type GROUP BY attop_article.type ORDER BY attop_article_type.id;

但是效果并不好,未能统计全,我希望没有文章的能默认填零,而这段只能做到没有文章的忽略。

sql执行效果:

希望能一次性执行,id能全部查询出来,并能统计所属id的文章数量。感谢。

解决方案

select id,title,(select count(*) from attop_article where attop_article.type = attop_article_type.id) as count from attop_article_type;

这篇关于php - 关于关联查询的sql查询不全的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆