Jump to content

Error in query


piyush23424

Recommended Posts

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;

Link to comment
https://forums.phpfreaks.com/topic/201302-error-in-query/
Share on other sites

  • 2 weeks later...

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.

Link to comment
https://forums.phpfreaks.com/topic/201302-error-in-query/#findComment-1060568
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.