如何使用group by获取每个类别的计数?

时间:2017-02-03 11:09:09

标签: php sql cakephp-2.0

我正在使用cakephp 2.0框架。我想计算每个类别的记录数。 这里我有两张桌子

 <?php
        $homeBrands1 =mysql_query("SELECT b.MASTER_ID,b.IMAGES,b.PAGE,b.HOME_LOGO,b.STATUS,m.ID,m.TITLE,m.LOGO FROM cms as b
                      INNER JOIN master as m on m.ID=b.MASTER_ID WHERE b.PAGE='homebrands' AND b.POSITION=1");
        $homeResult1 =mysql_fetch_array($homeBrands1);

        $homeBrands3 =mysql_query("SELECT b.MASTER_ID,b.IMAGES,b.PAGE,b.STATUS,b.HOME_LOGO,m.ID,m.TITLE,m.LOGO FROM cms as b
                      INNER JOIN master as m on m.ID=b.MASTER_ID WHERE b.PAGE='homebrands' AND b.POSITION=3");
        $homeResult3 =mysql_fetch_array($homeBrands3);

        $homeBrands2 =mysql_query("SELECT b.MASTER_ID,b.IMAGES,b.PAGE,b.STATUS,b.HOME_LOGO,m.ID,m.TITLE,m.LOGO FROM cms as b
                      INNER JOIN master as m on m.ID=b.MASTER_ID WHERE b.PAGE='homebrands' AND b.POSITION=2");
        $homeResult2 =mysql_fetch_array($homeBrands2);

        $homeBrands4 =mysql_query("SELECT b.MASTER_ID,b.IMAGES,b.PAGE,b.STATUS,b.HOME_LOGO,m.ID,m.TITLE,m.LOGO FROM cms as b
                      INNER JOIN master as m on m.ID=b.MASTER_ID WHERE b.PAGE='homebrands' AND b.POSITION=4");
        $homeResult4 =mysql_fetch_array($homeBrands4);

        $homeBrands5 =mysql_query("SELECT b.MASTER_ID,b.IMAGES,b.PAGE,b.STATUS,b.HOME_LOGO,m.ID,m.TITLE,m.LOGO FROM cms as b
                      INNER JOIN master as m on m.ID=b.MASTER_ID WHERE b.PAGE='homebrands' AND b.POSITION=5");
        $homeResult5 =mysql_fetch_array($homeBrands5);

        $homeBrands6 =mysql_query("SELECT b.MASTER_ID,b.IMAGES,b.PAGE,b.STATUS,b.HOME_LOGO,m.ID,m.TITLE,m.LOGO FROM cms as b
                      INNER JOIN master as m on m.ID=b.MASTER_ID WHERE b.PAGE='homebrands' AND b.POSITION=6");
        $homeResult6 =mysql_fetch_array($homeBrands6);
     ?>  

      <div class="brands">

        <div class="main-heading">
          <h3>Our<br> Brands</h3>
        </div>
        <ul id="slider1" style="overflow:hidden;">
  <li style="margin-left: -19px !important; margin-right:22px !important;">

    <div class="row" style="padding-left:10px !important; margin-left:1px;">
            <div class="col-sm-4">
              <div class="brand-cat wow fadeInUp" data-wow-delay="0s">
                <img src="images/brands/<?php echo $homeResult1['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult1['TITLE'];?>">
                <div class="brand-name"><img src="images/home_logos/<?php echo $homeResult1['HOME_LOGO'];?>" class="img-responsive" alt=""></div>
                <div class="overlay">
                  <div class="overly-brand-name"><img  src="images/home_logos/<?php echo $homeResult1['HOME_LOGO'];?>" class="img-responsive" alt=""><a href="" onclick="get_details(<?php echo $homeResult1['MASTER_ID'];?>);"><i class="fa fa-long-arrow-right" aria-hidden="true"></i></a></div>
                  <a href="#" onclick="get_details(<?php echo $homeResult1['MASTER_ID'];?>);" class="viewangale">View more</a>
                </div>
              </div>
              <div class="brand-cat wow fadeInUp" data-wow-delay="0.4s">
                <img src="images/brands/<?php echo $homeResult3['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult3['TITLE'];?>">
                <div class="brand-name"><img src="images/home_logos/<?php echo $homeResult3['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult3['TITLE'];?>"></div>
                <div class="overlay">
                  <div class="overly-brand-name"><img src="images/home_logos/<?php echo $homeResult3['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult3['TITLE'];?>"><a href="#" onclick="get_details(<?php echo $homeResult3['MASTER_ID'];?>);"><i class="fa fa-long-arrow-right" aria-hidden="true"></i></a></div>
                  <a href="#" onclick="get_details(<?php echo $homeResult3['MASTER_ID'];?>);" class="viewangale">View more</a>

                </div>
              </div>
            </div>
            <div class="col-sm-8">
              <div class="row">
                <div class="col-sm-6">
                  <div class="brand-cat stradivarius wow fadeInUp" data-wow-delay="0.3s">
                <img src="images/brands/<?php echo $homeResult2['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult2['TITLE'];?>">
                <div class="brand-name"><img src="images/home_logos/<?php echo $homeResult2['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult2['TITLE'];?>"></div>
                <div class="overlay">
                  <div class="overly-brand-name"><img src="images/home_logos/<?php echo $homeResult2['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult2['TITLE'];?>"><a href="#" onclick="get_details(<?php echo $homeResult2['MASTER_ID'];?>);"><i class="fa fa-long-arrow-right" aria-hidden="true"></i></a></div>
                  <a href="#" onclick="get_details(<?php echo $homeResult2['MASTER_ID'];?>);" class="viewangale">View more</a>
                </div>
              </div>
              <div class="brand-cat wow fadeInUp" data-wow-delay="0.5s">
                <img src="images/brands/<?php echo $homeResult4['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult4['TITLE'];?>">
                <div class="brand-name"><img src="images/home_logos/<?php echo $homeResult4['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult4['TITLE'];?>"></div>
                <div class="overlay">
                  <div class="overly-brand-name"><img src="images/home_logos/<?php echo $homeResult4['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult4['TITLE'];?>"><a href="#" onclick="get_details(<?php echo $homeResult4['MASTER_ID'];?>);"><i class="fa fa-long-arrow-right" aria-hidden="true"></i></a></div>

                <a href="#" onclick="get_details(<?php echo $homeResult4['MASTER_ID'];?>);" class="viewangale">View more</a>
                </div>
              </div>
                </div>
                 <div class="col-sm-6" style="padding-left:10px;">
                  <div class="brand-cat gerry wow fadeInUp" data-wow-delay="0.2s">
                    <img src="images/brands/<?php echo $homeResult5['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult5['TITLE'];?>">
                    <div class="brand-name"><img src="images/home_logos/<?php echo $homeResult5['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult5['TITLE'];?>"></div>
                    <div class="overlay">
                  <div class="overly-brand-name"><img src="images/home_logos/<?php echo $homeResult5['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult5['TITLE'];?>"><a href="#" onclick="get_details(<?php echo $homeResult5['MASTER_ID'];?>);"><i class="fa fa-long-arrow-right" aria-hidden="true"></i></a></div>
                  <a href="#" onclick="get_details(<?php echo $homeResult5['MASTER_ID'];?>);" class="viewangale">View more</a>
                </div>
                  </div>
                </div>
              </div>
               <div class="row" style="padding:0px !important;">
                <div class="col-sm-12">
                  <div class="brand-cat wow fadeInUp" data-wow-delay="0.5s">
                    <img src="images/brands/<?php echo $homeResult6['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult6['TITLE'];?>">
                    <div class="brand-name"><img src="images/home_logos/<?php echo $homeResult6['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult6['TITLE'];?>"></div>
                    <div class="overlay">
                  <div class="overly-brand-name"><img src="images/home_logos/<?php echo $homeResult6['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult6['TITLE'];?>"><a href="" onclick="get_details(<?php echo $homeResult6['MASTER_ID'];?>);"><i class="fa fa-long-arrow-right" aria-hidden="true"></i></a></div>
                <a href="#" onclick="get_details(<?php echo $homeResult6['MASTER_ID'];?>);" class="viewangale">View more</a>
                </div>
                  </div>
                </div>
              </div>
            </div>
             <div style="clear:both"></div>
          </div>
        <div style="clear:both"></div>
  </li>

   <li style="margin-left: -19px !important; margin-right:22px !important;">

  <div class="row" style="padding-left:10px !important; margin-left:1px;">
            <div class="col-sm-4">
              <div class="brand-cat wow fadeInUp" data-wow-delay="0s">
                <img src="images/brands/<?php echo $homeResult1['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult1['TITLE'];?>">
                <div class="brand-name"><img src="images/home_logos/<?php echo $homeResult1['HOME_LOGO'];?>" class="img-responsive" alt=""></div>
                <div class="overlay">
                  <div class="overly-brand-name"><img  src="images/home_logos/<?php echo $homeResult1['HOME_LOGO'];?>" class="img-responsive" alt=""><a href="" onclick="get_details(<?php echo $homeResult1['MASTER_ID'];?>);"><i class="fa fa-long-arrow-right" aria-hidden="true"></i></a></div>
                  <a href="#" onclick="get_details(<?php echo $homeResult1['MASTER_ID'];?>);" class="viewangale">View more</a>
                </div>
              </div>
              <div class="brand-cat wow fadeInUp" data-wow-delay="0.4s">
                <img src="images/brands/<?php echo $homeResult3['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult3['TITLE'];?>">
                <div class="brand-name"><img src="images/home_logos/<?php echo $homeResult3['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult3['TITLE'];?>"></div>
                <div class="overlay">
                  <div class="overly-brand-name"><img src="images/home_logos/<?php echo $homeResult3['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult3['TITLE'];?>"><a href="#" onclick="get_details(<?php echo $homeResult3['MASTER_ID'];?>);"><i class="fa fa-long-arrow-right" aria-hidden="true"></i></a></div>
                  <a href="#" onclick="get_details(<?php echo $homeResult3['MASTER_ID'];?>);" class="viewangale">View more</a>

                </div>
              </div>
            </div>
            <div class="col-sm-8">
              <div class="row">
                <div class="col-sm-6">
                  <div class="brand-cat stradivarius wow fadeInUp" data-wow-delay="0.3s">
                <img src="images/brands/<?php echo $homeResult2['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult2['TITLE'];?>">
                <div class="brand-name"><img src="images/home_logos/<?php echo $homeResult2['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult2['TITLE'];?>"></div>
                <div class="overlay">
                  <div class="overly-brand-name"><img src="images/home_logos/<?php echo $homeResult2['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult2['TITLE'];?>"><a href="#" onclick="get_details(<?php echo $homeResult2['MASTER_ID'];?>);"><i class="fa fa-long-arrow-right" aria-hidden="true"></i></a></div>
                  <a href="#" onclick="get_details(<?php echo $homeResult2['MASTER_ID'];?>);" class="viewangale">View more</a>
                </div>
              </div>
              <div class="brand-cat wow fadeInUp" data-wow-delay="0.5s">
                <img src="images/brands/<?php echo $homeResult4['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult4['TITLE'];?>">
                <div class="brand-name"><img src="images/home_logos/<?php echo $homeResult4['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult4['TITLE'];?>"></div>
                <div class="overlay">
                  <div class="overly-brand-name"><img src="images/home_logos/<?php echo $homeResult4['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult4['TITLE'];?>"><a href="#" onclick="get_details(<?php echo $homeResult4['MASTER_ID'];?>);"><i class="fa fa-long-arrow-right" aria-hidden="true"></i></a></div>

                <a href="#" onclick="get_details(<?php echo $homeResult4['MASTER_ID'];?>);" class="viewangale">View more</a>
                </div>
              </div>
                </div>
                <div class="col-sm-6" style="padding-left:10px;">
                  <div class="brand-cat gerry wow fadeInUp" data-wow-delay="0.2s">
                    <img src="images/brands/<?php echo $homeResult5['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult5['TITLE'];?>">
                    <div class="brand-name"><img src="images/home_logos/<?php echo $homeResult5['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult5['TITLE'];?>"></div>
                    <div class="overlay">
                  <div class="overly-brand-name"><img src="images/home_logos/<?php echo $homeResult5['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult5['TITLE'];?>"><a href="#" onclick="get_details(<?php echo $homeResult5['MASTER_ID'];?>);"><i class="fa fa-long-arrow-right" aria-hidden="true"></i></a></div>
                  <a href="#" onclick="get_details(<?php echo $homeResult5['MASTER_ID'];?>);" class="viewangale">View more</a>
                </div>
                  </div>
                </div>
              </div>
             <div class="row" style="padding:0px !important;">
                <div class="col-sm-12">
                  <div class="brand-cat wow fadeInUp" data-wow-delay="0.5s">
                    <img src="images/brands/<?php echo $homeResult6['IMAGES']; ?>" class="img-responsive" alt="<?php echo $homeResult6['TITLE'];?>">
                    <div class="brand-name"><img src="images/home_logos/<?php echo $homeResult6['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult6['TITLE'];?>"></div>
                    <div class="overlay">
                  <div class="overly-brand-name"><img src="images/home_logos/<?php echo $homeResult6['HOME_LOGO'];?>" class="img-responsive" alt="<?php echo $homeResult6['TITLE'];?>"><a href="" onclick="get_details(<?php echo $homeResult6['MASTER_ID'];?>);"><i class="fa fa-long-arrow-right" aria-hidden="true"></i></a></div>
                <a href="#" onclick="get_details(<?php echo $homeResult6['MASTER_ID'];?>);" class="viewangale">View more</a>
                </div>
                  </div>
                </div>
              </div>
            </div>
             <div style="clear:both"></div>
          </div>
       <div style="clear:both"></div>
  </li>

  </ul>

      </div>

我使用此查询获取此数据: -

eve_appointment_icons ->having columns- appointment_icon_id,appointment_icon_type_name
eve_appointments ->having columns- id, appointment_icon_id

这时我打印这个:

$this->EveAppointment->query("SELECT
                    a.appointment_type
                    d.appointment_icon_id,
                    a.appointment_icon_id , 
                    d.appointment_icon_type_name,   
                FROM  eve_appointments a,
                    eve_appointment_icons d
                WHERE  a.appointment_type =1
                AND d.appointment_icon_id = a.appointment_icon_id

            UNION
                SELECT 
                    a.appointment_type
                    d.appointment_icon_id,
                    a.appointment_icon_id , 
                    d.appointment_icon_type_name,   

                FROM  eve_appointments a,
                    eve_appointment_icons d
                WHERE  a.appointment_type =2
                    AND d.appointment_icon_id = a.appointment_icon_id
            UNION
                SELECT 
                    a.appointment_type
                    d.appointment_icon_id,
                    a.appointment_icon_id , 
                    d.appointment_icon_type_name, 
                FROM  eve_appointments a,
                    eve_appointment_icons d
                WHERE a.appointment_type =3
                    AND d.appointment_icon_id = a.appointment_icon_id");

现在,您可以看到Array ( [0] => Array ( [appointment_icon_id] => 1 [appointment_icon_type_name] => Yoga ) [1] => Array ( [appointment_icon_id] => 1 [appointment_icon_type_name] => Yoga ) [2] => Array ( [appointment_icon_id] => 2 [appointment_icon_type_name] => Visit ) [3] => Array ( [appointment_icon_id] => 3 [appointment_icon_type_name] => Physio Therapy ) [4] => Array ( [appointment_icon_id] => 4 [appointment_icon_type_name] => Diagnostic ) [5] => Array ( [appointment_icon_id] => 4 [appointment_icon_type_name] => Diagnostic ) [6] => Array ( [appointment_icon_id] => 4 [appointment_icon_type_name] => Diagnostic ) ) appointment_icon_id的瑜伽,1 appointment_icon_id访问,反之亦然。 我想要算上所有这些类别。 像:

2

1 个答案:

答案 0 :(得分:2)

所有你需要的是:

select d.appointment_icon_type_name, count(*)   
FROM  eve_appointments a,
eve_appointment_icons d
WHERE d.appointment_icon_id = a.appointment_icon_id
group by d.appointment_icon_type_name