Jump to content

GROUP BY & ORDER BY in same query


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.

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?

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.