简体   繁体   中英

how to count rows while joining the table

i have two table one is for outlets details with location and one is for no. of offers of that particular outlets and i want to join two table to display outlets and their respective offers and location also , i successfully fetch the outlets and their offers by joining the table but i want how to count the no. of offers on particular location. my sql query for joining table is

$sql_2="SELECT p.id,p.outlet_id,p.offer_title,p.original_price,
 p.discount,p.discount_price,p.offer_image,p.offer_details,
 o.outlet_id,o.outlet_title,o.outlet_locality,o.segment_type  
FROM products as p,outlets as o 
WHERE p.outlet_id=o.outlet_id AND o.segment_type='$category' 
GROUP BY o.outlet_locality ";

from above i can fectch the group of localities and i want to count the total offers and ie. ALL() and indivisual locality offers on every localities my html is '

<h3>Location</h3>
  <ul class="offer-location" >
    <li class="checkbox">
      <label>
         <input type="checkbox" class="i-check">All <small>(657)</small>
      </label>
    </li>
    <?php
      foreach($qry_2 as $v2){
    ?>
        <li class="checkbox">
          <label>
            <input type="checkbox" class="i-check"/>
               <?php echo $v2['outlet_locality'] ?> <small>(120)</small>
          </label>
        </li>
    <?php
    }
    ?>
  </ul>
 $sql_2="SELECT p.id,p.outlet_id,p.offer_title,p.original_price,
 p.discount,p.discount_price,p.offer_image,p.offer_details,
 o.outlet_id,o.outlet_title,o.outlet_locality,o.segment_type, count(p.outlet_id) as outletCount 
 FROM products as p,outlets as o 
 WHERE p.outlet_id=o.outlet_id AND o.segment_type='$category' 
 GROUP BY o.outlet_locality ";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM