iceblox Posted March 19, 2009 Share Posted March 19, 2009 Hi guys, just a quick one. I cant get this query to order by columnaverage, can anyone spot any mistakes? SELECT avg(rating_num) AS columnaverage FROM ratings WHERE rating_id = '$tariff' ORDER BY columnaverage ASC Thanks, Phil Quote Link to comment https://forums.phpfreaks.com/topic/150222-solved-why-wont-this-order/ Share on other sites More sharing options...
Mark Baker Posted March 19, 2009 Share Posted March 19, 2009 The query should only return a single record, so why do you consider an ORDER BY is of any use at all? Quote Link to comment https://forums.phpfreaks.com/topic/150222-solved-why-wont-this-order/#findComment-788914 Share on other sites More sharing options...
iceblox Posted March 19, 2009 Author Share Posted March 19, 2009 ah yeah i think i know where its going wrong lol What a plonker. As there are multiple "$tariffs" what would be the easiest way to order it? Quote Link to comment https://forums.phpfreaks.com/topic/150222-solved-why-wont-this-order/#findComment-788917 Share on other sites More sharing options...
corbin Posted March 19, 2009 Share Posted March 19, 2009 Multiple tariffs? Do you mean something like: SELECT rating_id, avg(rating_num) AS columnaverage FROM ratings GROUP BY rating_id ORDER BY columnaverage ASC Quote Link to comment https://forums.phpfreaks.com/topic/150222-solved-why-wont-this-order/#findComment-788965 Share on other sites More sharing options...
iceblox Posted March 24, 2009 Author Share Posted March 24, 2009 Hi Guys, Thanks all for your help. I think i was trying to over complicate something that should have been so easy! Im pretty much there but i need to chuck in a join as well, so far the query is this; <?php $result = $db->sql_query("SELECT rating_id, avg(rating_num) AS columnaverage FROM ratings GROUP BY rating_id ORDER BY columnaverage DESC"); while($row = $db->sql_fetchrow($result)) { $format_number = number_format($row[columnaverage], 2, '.', ''); echo "<br>$row[rating_id] <b>$format_number</b>"; } ?> Normally i would join a table like this $query = "SELECT * FROM ratings a, models b WHERE a.rating_id = b.modid GROUP BY rating_id"; but as i normally select * im not sure how i would go about this when im selecting certain data? Is any one able to help me to get this done? Thanks, Phil Quote Link to comment https://forums.phpfreaks.com/topic/150222-solved-why-wont-this-order/#findComment-792697 Share on other sites More sharing options...
kickstart Posted March 24, 2009 Share Posted March 24, 2009 Hi Think to give a reasonable reply we would need to know what you want to join. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/150222-solved-why-wont-this-order/#findComment-792701 Share on other sites More sharing options...
iceblox Posted March 24, 2009 Author Share Posted March 24, 2009 Hi Keith, Sorry i didnt give much detail! I needed to join my models table. But i tried this; <?php $result2 = $db->sql_query("SELECT rating_id, modname, manname, modid, avg(rating_num) AS columnaverage FROM ratings a, models b WHERE a.rating_id = b.modid GROUP BY rating_id ORDER BY columnaverage DESC"); while($row2 = $db->sql_fetchrow($result2)) { $format_number = number_format($row2[columnaverage], 2, '.', ''); echo "<br><a target=\"_blank\" href=\"$row2[manname]-$row2[modname]-Mobile-Review-$row2[modid].html\">$row2[manname] $row2[modname]</a> <b>$format_number</b>"; } ?> And got it to work!! Thanks for all your help again! Thanks, Phil Quote Link to comment https://forums.phpfreaks.com/topic/150222-solved-why-wont-this-order/#findComment-792702 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.