PeerFly Posted June 11, 2009 Share Posted June 11, 2009 The code below works fine. select distinct(u.userid), u.*, sum(e.amount) as amnt from `users` as u left join `earnings` as e on u.userid = e.referral where u.referrer = '$userid' group by u.userid ORDER BY amnt DESC LIMIT $from, $max_results Now, if I want to ONLY display rows where amnt is GREATER THAN zero, it doesn't work. How can I make it a statement on a column that was just created from a sum? This doesn't work: select distinct(u.userid), u.*, sum(e.amount) as amnt from `users` as u left join `earnings` as e on u.userid = e.referral where u.referrer = '$userid' and amnt > 0 group by u.userid ORDER BY amnt DESC LIMIT $from, $max_results This seems like a really simple problem but I can't figure it out. Quote Link to comment https://forums.phpfreaks.com/topic/161794-solved-only-show-rows-with-a-group-sum-of-greater-than-zero/ Share on other sites More sharing options...
fenway Posted June 11, 2009 Share Posted June 11, 2009 DISTINCT is not a function. Use group by. Quote Link to comment https://forums.phpfreaks.com/topic/161794-solved-only-show-rows-with-a-group-sum-of-greater-than-zero/#findComment-853708 Share on other sites More sharing options...
PeerFly Posted June 11, 2009 Author Share Posted June 11, 2009 Ok, but that doesn't fix my problem. I'm trying to only show rows where the amnt column is greater than zero but it's not working because the amnt column really isn't a physical column, just one that I made up on the fly from a sum. How can I make it work? Quote Link to comment https://forums.phpfreaks.com/topic/161794-solved-only-show-rows-with-a-group-sum-of-greater-than-zero/#findComment-853808 Share on other sites More sharing options...
J.Daniels Posted June 11, 2009 Share Posted June 11, 2009 sum(e.amount) is going to return the sum of the amount column for all returned rows. Did you want to include only the rows that were greater than 0?? If so, change: amnt > 0 to: e.amount > 0 Quote Link to comment https://forums.phpfreaks.com/topic/161794-solved-only-show-rows-with-a-group-sum-of-greater-than-zero/#findComment-853854 Share on other sites More sharing options...
PeerFly Posted June 11, 2009 Author Share Posted June 11, 2009 J, what the code does so far is it adds up all the users earnings from the database and then puts their totals into a new separate column called "amnt". For instance, if Joe has 5 rows in the earnings table where each dollar value from the "amount" column sums up to $10.00, then by doing this code, it will distinctively display Joe one time, and then the new "amnt" column will show Joe's total sum of money. What I am trying to do is display ONLY the rows where "amnt" is greater than zero. The strange thing is that you can order by the temp amnt column, but you cannot create a "where" clause with it for some reason. There has to be a way. Quote Link to comment https://forums.phpfreaks.com/topic/161794-solved-only-show-rows-with-a-group-sum-of-greater-than-zero/#findComment-854071 Share on other sites More sharing options...
J.Daniels Posted June 11, 2009 Share Posted June 11, 2009 I understand now. I haven't tested this, but you'll want to look at the HAVING clause: select distinct(u.userid), u.*, sum(e.amount) as amnt from `users` as u left join `earnings` as e on u.userid = e.referral where u.referrer = '$userid' and amnt > 0 group by u.userid HAVING amnt > 0 DESC LIMIT $from, $max_results Quote Link to comment https://forums.phpfreaks.com/topic/161794-solved-only-show-rows-with-a-group-sum-of-greater-than-zero/#findComment-854096 Share on other sites More sharing options...
PeerFly Posted June 12, 2009 Author Share Posted June 12, 2009 You da man! Quote Link to comment https://forums.phpfreaks.com/topic/161794-solved-only-show-rows-with-a-group-sum-of-greater-than-zero/#findComment-854180 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.