我有一个商品表,结构如下:
id name一个商品评论表
id pid content其中商品表的id为商品评论表的pid我想查询出商品表每个商品的评论数量该如何些SQL语句?不胜感激!

解决方案 »

  1.   

    select count(*) from  商品评论表  where pid=id 
      

  2.   

    $sql="select count(*) from  商品评论表  where pid=(select id from  商品表  )";
    $re=mysql_query($sql);
    $row=mysql_fetch_row($re)
    $total_mdn=$row[0];//商品评论表数量
      

  3.   


    select pid,count(pid) from 商品评论表 where pid=(select id from  商品表) group by pid
      

  4.   

    select pid,count(*) 
    from  商品评论表 inner join 商品表  
    where pid=商品表.id 
    group by pid
      

  5.   

    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 
      

  6.   

    得到结果
    商品id 商品名称name 评论数count
    1      fdf          2
    2      ffe          3
      

  7.   


    SELECT a.name,count(*) AS counts 
    FROM 商品表 a LEFT JOIN 商品评论表 b
    ON a.id=b.pid
    GROUP BY a.name;
      

  8.   

    商品名称不是唯一的,有不同商品,名称一样的情况,你group by name不完美
      

  9.   


    select a.id,a.name,count(b.id) from 商品表 as a,商品评论表 as b where a.id=b.pid group by a.id
      

  10.   

    SELECT a.id,a.name,count(*) AS counts 
    FROM g_info a LEFT JOIN PL b
    ON a.id=b.id
    GROUP BY a.id;
      

  11.   

    楼主id和pid不是相等的,注意下!!
    你们a.id=b.pid啥意思??
    晕!!
    评论表中pid是主键!
      

  12.   

    楼主不是说:其中商品表的id为商品评论表的pid 怎会不相等?pid是外键吧?