Jump to content

GROUP BY & ORDER BY in same query


Go to solution Solved by PriteshP23,

Recommended Posts

I would like to solve this query.

 

 

Query:

$sql = 'SELECT DISTINCT *, c.ci as c_i FROM `GSM_cellule` c '.
           'LEFT JOIN '.$table_freq.' f ON(c.nidtint=f.nidtint) '.
           'WHERE c.NAP_'.$site->ur().' =\'1\' '.
            $this->_in.' and '.$this->_inetatbdespec.' and '.$this->_inindus.' GROUP BY c.nidtint ORDER BY c.nidtint ';

Error:

 

SELECT DISTINCT *, c.ci as c_i FROM `GSM_cellule` c LEFT JOIN GSM_celluleterrain f ON(c.nidtint=f.nidtint) WHERE c.NAP_PA ='1' AND RIGHT(LEFT(c.`nidtnoeud`,10),2) IN ('U1','U8') and c.etat IN ('ED + ES') and GROUP BY c.nidtint ORDER BY c.nidtint

 

Thanks in advanced.

 

:facewall:

Link to comment
https://forums.phpfreaks.com/topic/281595-group-by-order-by-in-same-query/
Share on other sites

$this->_inindus is empty resulting in the syntax - and GROUP BY

 

also, GROUP BY some_term performs an ORDER BY some_term (the query needs to get the same rows being grouped, adjacent to each other before it can consolidate them.). so, you don't need ORDER BY unless you want a different order than what the GROUP BY resulted in.

Edited by mac_gyver
  • Solution

Thanks a lot Guru..!!

SELECT DISTINCT * , c.ci AS c_i
FROM `GSM_cellule` c
LEFT JOIN GSM_celluleterrain f ON ( c.nidtint = f.nidtint )
WHERE c.NAP_PA = '1'
AND RIGHT( LEFT( c.`nidtnoeud` , 10 ) , 2 )
IN (
'U1'
)
AND c.etat
IN (
'ED + ES'
)
GROUP BY c.nidtint
ORDER BY c.nidtint
LIMIT 0 , 30

Are you sure this works? Youre doing a DISTINCT * which means you are getting duplicate rows through a mistake in the query. de GROUP-BY is further evidence of this bevause you are not doing any aggregation. This is not good.

 

What are you trying to achieve here?

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.