Wildthing Posted May 25, 2013 Share Posted May 25, 2013 I have a simple test table in mysqlfields name, booked,3 entries nick, 1 john, 1 nick, 1I want to echo out the name with the least amounts of booked sumso output should be johncan anyone help ? Link to comment https://forums.phpfreaks.com/topic/278379-phpsql-query/ Share on other sites More sharing options...
PravinS Posted May 25, 2013 Share Posted May 25, 2013 Use MIN function in mysql to fetch least amount Refer: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_min Link to comment https://forums.phpfreaks.com/topic/278379-phpsql-query/#findComment-1432213 Share on other sites More sharing options...
Jessica Posted May 25, 2013 Share Posted May 25, 2013 You'd need to group them by name, get the sum of each group, then using min. Link to comment https://forums.phpfreaks.com/topic/278379-phpsql-query/#findComment-1432215 Share on other sites More sharing options...
Wildthing Posted May 25, 2013 Author Share Posted May 25, 2013 mysql> select rep_surname, AVG(booked) from appointments group by rep_surname;+-------------+-------------+| rep_surname | AVG(booked) |+-------------+-------------+| nick | 1.0000 || john | 1.0000 |+-------------+-------------+2 rows in set (0.00 sec) But there are 3 rows all all, John has 1 more entry Did I miss something ? I am not real good at this Link to comment https://forums.phpfreaks.com/topic/278379-phpsql-query/#findComment-1432305 Share on other sites More sharing options...
Barand Posted May 25, 2013 Share Posted May 25, 2013 SELECT name, COUNT(*) as tot FROM table GROUP BY name ORDER BY tot LIMIT 1 Link to comment https://forums.phpfreaks.com/topic/278379-phpsql-query/#findComment-1432307 Share on other sites More sharing options...
Wildthing Posted May 26, 2013 Author Share Posted May 26, 2013 So far I have $result = mysql_query("SELECT rep_surname, COUNT(*) as tot FROM appointments GROUP BY rep_surname ORDER BY tot"); Need it to echo the rep_name with the least amounts of "1" in booked fieldkind of stuck Link to comment https://forums.phpfreaks.com/topic/278379-phpsql-query/#findComment-1432323 Share on other sites More sharing options...
Jessica Posted May 26, 2013 Share Posted May 26, 2013 order by DESC and LIMIT 1 Link to comment https://forums.phpfreaks.com/topic/278379-phpsql-query/#findComment-1432364 Share on other sites More sharing options...
Barand Posted May 26, 2013 Share Posted May 26, 2013 order by DESC and LIMIT 1 that gives highest first Link to comment https://forums.phpfreaks.com/topic/278379-phpsql-query/#findComment-1432366 Share on other sites More sharing options...
Jessica Posted May 26, 2013 Share Posted May 26, 2013 I misread, I thought he wanted the highest. Lowest first obviously would be ASC then Link to comment https://forums.phpfreaks.com/topic/278379-phpsql-query/#findComment-1432367 Share on other sites More sharing options...
Barand Posted May 26, 2013 Share Posted May 26, 2013 I gave him the query, can't understand what there is left to be stuck on Link to comment https://forums.phpfreaks.com/topic/278379-phpsql-query/#findComment-1432368 Share on other sites More sharing options...
Jessica Posted May 26, 2013 Share Posted May 26, 2013 I feel ya Link to comment https://forums.phpfreaks.com/topic/278379-phpsql-query/#findComment-1432370 Share on other sites More sharing options...
Wildthing Posted May 27, 2013 Author Share Posted May 27, 2013 Doing my head in lol $result = mysql_query("SELECT rep_surname, COUNT(*) as tot FROM appointments GROUP BY rep_surname order by ASC LIMIT 1");echo $row['rep_surname'];--------------------------------------------- error in apache log PHP Notice: Undefined variable: row in /home/wildthin/public_html/grabbs/test.html on line 10 Link to comment https://forums.phpfreaks.com/topic/278379-phpsql-query/#findComment-1432491 Share on other sites More sharing options...
mac_gyver Posted May 27, 2013 Share Posted May 27, 2013 the error means that you didn't fetch the data from the result set. before you can use php/mysql for your data, you must first learn how to use php/myqsl for any data. you need to read a basic php/mysql tutorial/book so that you understand the steps needed to form an sql query statement, run that query, check if the query executed without any errors, and then fetch any resultant data from that query. Link to comment https://forums.phpfreaks.com/topic/278379-phpsql-query/#findComment-1432503 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.