piyush23424 Posted May 10, 2010 Share Posted May 10, 2010 I am trying to run this query but it is giving error. Please tell me what is wrong with this query ? $qry = "SELECT * FROM `products` where price > avg(price)" One more question : can we use group by clause on two columns : eg : Select name from students group by subject, name; Quote Link to comment https://forums.phpfreaks.com/topic/201302-error-in-query/ Share on other sites More sharing options...
Maq Posted May 10, 2010 Share Posted May 10, 2010 What is the error? Quote Link to comment https://forums.phpfreaks.com/topic/201302-error-in-query/#findComment-1056127 Share on other sites More sharing options...
piyush23424 Posted May 10, 2010 Author Share Posted May 10, 2010 What is the error? For the first query error is : Invalid use of group function For the second query : #1140 - Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause Quote Link to comment https://forums.phpfreaks.com/topic/201302-error-in-query/#findComment-1056129 Share on other sites More sharing options...
jagdish kothapalle Posted May 19, 2010 Share Posted May 19, 2010 1. For the query, $qry = "SELECT * FROM `products` where price > avg(price)" try the below one $qry = "SELECT * FROM products where price > (select avg(price) from products)" The mysql engine doesn't understand avg(price) unless you have a variable something like $avgprice (which is already assigned) passed from your php. 2. Ideally, you can use multiple fields in group by clause provided they are also present in the select clause. Quote Link to comment https://forums.phpfreaks.com/topic/201302-error-in-query/#findComment-1060568 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.