求一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是外键吧? 有人用过cactiEZ监控MYSQL吗。碰到问题希望帮忙解决 寻求一个PHP在线考试系统 数组分割循环读出 新手关于用php写helloworld的问题 js模拟鼠标点击 在程序的运行进程当中,由于需要远程读取文件,导致apache的cpu占用达到99%最后死机,是什么原因? 刚学php,第一个程序遇到的问题,请指教 进程阻塞? 为何这里的变量没有注册 php链接数据库进行显示问题 $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是主键!