4

I HAVE THE FOLLOWING TABLE, I WOULD LIKE TO GET to do a select distinct on the the column [code], i don't need to get the "A" three times.

[ ID ]   [ CODE ]     [ LIBELLE ]
1         A        LIBELLE1  
2         B        LIBELLE2
3         C        LIBELLE3
4         A        LIBELLE4  
5         A        LIBELLE5
6         D        LIBELLE6 

I want the result as following

[ ID ] [ CODE ] [ LIBELLE ]
1         A        LIBELLE1  
2         B        LIBELLE2
3         C        LIBELLE3
6         D        LIBELLE6 
Charles
  • 50,943
  • 13
  • 104
  • 142
Mamadou
  • 2,177
  • 5
  • 31
  • 43

3 Answers3

3

Just add

group by code 
ORDER BY code ASC

at end of your sql query

example

select * from table
group by code 
ORDER BY code ASC
Pramendra Gupta
  • 14,667
  • 4
  • 33
  • 34
1
 SELECT Min(Id) Id, Code, MIN(Libelle) Libelle
 from table
 group by code
Michael Pakhantsov
  • 24,855
  • 6
  • 60
  • 59
1

If you are looking for Zend_Db_Select usage, here it is

$db->select()->from('table', array(
    'Id' => new Zend_Db_Expr('Min(ID)'),
    'Code' => 'CODE',
    'Libelle' => new Zend_Db_Expr('Min(LIBELLE)')
))->group('CODE');

$db should be your Zend_Db_Adapter.

Nithesh Chandra
  • 1,840
  • 13
  • 12