求一mysql 查询语句 我有一个商品表,结构如下:id name一个商品评论表id pid content其中商品表的id为商品评论表的pid我想查询出商品表每个商品的评论数量该如何些SQL语句?不胜感激! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 select count(*) from 商品评论表 where pid=id $sql="select count(*) from 商品评论表 where pid=(select id from 商品表 )";$re=mysql_query($sql);$row=mysql_fetch_row($re)$total_mdn=$row[0];//商品评论表数量 select pid,count(pid) from 商品评论表 where pid=(select id from 商品表) group by pid select pid,count(*) from 商品评论表 inner join 商品表 where pid=商品表.id group by pid select s.id, s.name, (select count(*) from 评论表 where id=s.id) as 'count'from 商品表 s,评论表 P where s.id=p.id group by s.id 得到结果商品id 商品名称name 评论数count1 fdf 22 ffe 3 SELECT a.name,count(*) AS counts FROM 商品表 a LEFT JOIN 商品评论表 bON a.id=b.pidGROUP BY a.name; 商品名称不是唯一的,有不同商品,名称一样的情况,你group by name不完美 select a.id,a.name,count(b.id) from 商品表 as a,商品评论表 as b where a.id=b.pid group by a.id SELECT a.id,a.name,count(*) AS counts FROM g_info a LEFT JOIN PL bON a.id=b.idGROUP BY a.id; 楼主id和pid不是相等的,注意下!!你们a.id=b.pid啥意思??晕!!评论表中pid是主键! 楼主不是说:其中商品表的id为商品评论表的pid 怎会不相等?pid是外键吧? PHP临时调用文件修改再编译输出如何实现? 搜狗推送严重影响搜索引擎收录,大家不要再上当了 smarty 局部缓存造成的问题 请教:关于系统自动生成数据的问题 正则替换掉最右边的一个点 求php显示pdf的解决方案 用过这个空间的人给点指点 高手请进,变量为空,如何解决? 各位老哥,高分求救,谁有使用session的例程,谢谢. 用程序输出从星期日至星期六,每天变化的只是日期而星期不变 $cc?0:1 和 !$cc?1:0 问号表达式 哪种效率高? 用哪种数据库比较好(PHP+mysql做中转)
$re=mysql_query($sql);
$row=mysql_fetch_row($re)
$total_mdn=$row[0];//商品评论表数量
select pid,count(pid) from 商品评论表 where pid=(select id from 商品表) group by pid
from 商品评论表 inner join 商品表
where pid=商品表.id
group by pid
s.name,
(select count(*) from 评论表 where id=s.id) as 'count'
from
商品表 s,评论表 P
where
s.id=p.id group by s.id
商品id 商品名称name 评论数count
1 fdf 2
2 ffe 3
SELECT a.name,count(*) AS counts
FROM 商品表 a LEFT JOIN 商品评论表 b
ON a.id=b.pid
GROUP BY a.name;
select a.id,a.name,count(b.id) from 商品表 as a,商品评论表 as b where a.id=b.pid group by a.id
FROM g_info a LEFT JOIN PL b
ON a.id=b.id
GROUP BY a.id;
你们a.id=b.pid啥意思??
晕!!
评论表中pid是主键!