已知表:
分类表:type
内容表:list其中:分类表type typecode = 内容表list catid循环type的分类,同时循环list中与分类对应的内容问:在分类表循环中统计内容表中对应的内容数量的语句怎么写?

解决方案 »

  1.   

    select   A.typecode,count(*)
    from type A,list B
    where A.typecode = B.catid
    group by A.typecode
      

  2.   

    select  catid,count(*)
    from list
    group by catid
      

  3.   

    select A.typecode,count(*) from type a inner join list B
    on A.typecode = B.catid group by A.typecode
      

  4.   

    谢谢上面的答复,可能没表达清楚,两个表已经在循环中的统计:
    查询贴出来:
    其中:分类表type typecode = 内容表list catid<?php
    //分类表
    $sql="select * from ".$BIAOTOU."type order by sort desc limit 0,15";
    $rs_type=mysql_query($sql);//内容表
    $sql="select * from ".$BIAOTOU."list where end_time >='".date('Y-m-d')."' order by id desc";
    $rs_list=mysql_query($sql);
    $i=0;
    while ($row2 = mysql_fetch_array($rs_list)){
    $allmalllist[$i]['id']= $row2['id'];
    $allmalllist[$i]['catid']= $row2['catid'];
    $allmalllist[$i]['name']= $row2['name'];
    $allmalllist[$i]['fan']= $row2['fan'];
    $allmalllist[$i]['logo']= $row2['logo'];
    $i++;
    }
    ?> <?php
        while ($row1 = mysql_fetch_array($rs_type)) {?>
        <?=$row1['typename']?>(这里统计该分类的内容量)
        <ul>
           <?php 
                 $i=0;
              foreach ($allmalllist as $row3){
          if($row3["catid"]==$row1["typecode"]&&$i10){
        echo "<li>".$row3['name']."</li>";
      $i++;
         }
              }
             ?>
        </ul>
    <?php }?>
      

  5.   

    加上下面一段:
    //查询出每个分类下面的数量。
    $result=mysql_query("select catid,count(*) as num
    from list
    group by catid");
    while($row3=mysql_fetch_array($result)){
         $arr[$row3['catid']]=$row3['num'];
    }  while ($row1 = mysql_fetch_array($rs_type)) {?>
      <?=$row1['typename']?> <?=$arr[$row1['typecode']]?>
      <ul>
      

  6.   

    select A.typecode,count(*) from type a 
    inner join list B
    on A.typecode = B.catid 
    group by A.typecode