Jump to content

Query GROUP BY


Aman22

Recommended Posts

Please advise me, to this following group function in PHP. this works perfectly fine,it groups the airline name and starts with the MIN price, however the problem is that it also shows the season but it is picking the MIN season which is not required. I want it to pick the season for the MIN price which it applies for in the same row.

 

the code is below...all help is much appreciated...thank you in advance:

 

$sql="SELECT MIN(price),dep,des,airline,flighttype,baggage,season,cabin,id FROM faresheet WHERE cabin = '".$q."' AND des = 'DEL' GROUP BY airline ORDER BY MIN(price) ASC";

Link to comment
Share on other sites

OK, this "should" work, but I leave it to you to test. Also, not sure if this is the most efficient method. This basically puts the records in the correct order before doing the GROUP BY

 

$sql="SELECT *
      FROM (SELECT price, dep, des, airline, flighttype, baggage, season, cabin, id
            FROM faresheet
            WHERE cabin = '{$q}'
              AND des = 'DEL'
            ORDER BY price ASC) as temp
      GROUP BY airline";

Link to comment
Share on other sites

You can't use GROUP BY with *.

Sure you can. I tested the query above before I posted and I just ran a very simply query using

SELECT * FROM table_name GROUP BY field_name

which worked fine as well.

 

My results showed that when using GROUP BY on such a query the first record for the duplicate values in the GROUP BY clause are returned. So, the query I posted above creates a temporary table that orders the values such that the records returned are the ones the OP is after. I did state

. . . not sure if this is the most efficient method

 

But, it does work. I would be interested in knowing a more proper method to get the requested data.

 

 

EDIT: From the MySQL manual (emphasis added)

In standard SQL, a query that includes a GROUP BY clause cannot refer to nonaggregated columns in the select list that are not named in the GROUP BY clause. For example, this query is illegal in standard SQL because the name column in the select list does not appear in the GROUP BY:

 

SELECT o.custid, c.name, MAX(o.payment)
  FROM orders AS o, customers AS c
  WHERE o.custid = c.custid
  GROUP BY o.custid;

 

For the query to be legal, the name column must be omitted from the select list or named in the GROUP BY clause.

 

MySQL extends the use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause. This means that the preceding query is legal in MySQL

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.