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
Share on other sites

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

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.