graham23s Posted January 18, 2009 Share Posted January 18, 2009 Hi Guys, i currently have this query: <?php // GET THE NUMBER OF ORDERS $qO = "SELECT DISTINCT(`CART_ID`) FROM `fcp_orders_master_log` WHERE `PROCESSED`='Y' AND `CUSTOMER_ID`='$cusID'"; $rO = mysql_query($qO); $nO = mysql_num_rows($rO); ?> This displays the number of orders each person has made bit in no particular order: Graham 0 orders David 2 order Greg 1 order i was tryiong to put a clause in so the customer swith the most orders are at the top, but i'm not sure what the clause would be lol any help would be appreciated cheers Graham Link to comment https://forums.phpfreaks.com/topic/141374-help-with-a-query/ Share on other sites More sharing options...
Mark Baker Posted January 18, 2009 Share Posted January 18, 2009 Please don't tell me you're retrieving a list of customers using one SQL query, then looping through each of those customers executing an individual query for each to retrieve the number of orders. You should use just one query to retrieve both, with a GROUP BY CUSTOMER_ID. Then you can order by your CART_ID. Link to comment https://forums.phpfreaks.com/topic/141374-help-with-a-query/#findComment-739973 Share on other sites More sharing options...
jonsamwell Posted January 18, 2009 Share Posted January 18, 2009 SELECT CUSTOMER_ID, COUNT(CART_ID) FROM `fcp_orders_master_log` WHERE `PROCESSED`='Y' GROUP BY CUSTOMER_ID ORDER BY COUNT(CART_ID) [DESC], CUSTOMER_ID [DESC]; This will count the number of processed orders with the most orders at the top then further sorted by customer id descending Regards Link to comment https://forums.phpfreaks.com/topic/141374-help-with-a-query/#findComment-740015 Share on other sites More sharing options...
graham23s Posted January 19, 2009 Author Share Posted January 19, 2009 Thanks guys, i have never been great with the old queries lol cheers Graham Link to comment https://forums.phpfreaks.com/topic/141374-help-with-a-query/#findComment-740120 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.