2010-09-22 4 views
4

J'ai la table suivante, je voudrais faire un select distinct sur la colonne [code], je n'ai pas besoin d'obtenir le "A" trois fois.Construire un select distinct sur mysql (zend_db)

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

Je veux le résultat comme suit

[ ID ] [ CODE ] [ LIBELLE ] 
1   A  LIBELLE1 
2   B  LIBELLE2 
3   C  LIBELLE3 
6   D  LIBELLE6 
+0

Je n'arrive pas à comprendre votre format? –

Répondre

3

ajouter juste

group by code 
ORDER BY code ASC 

à la fin de votre requête sql

exemple

select * from table 
group by code 
ORDER BY code ASC 
1
SELECT Min(Id) Id, Code, MIN(Libelle) Libelle 
from table 
group by code 
+0

vais le tester, il semble très intelligent cette méthode – Mamadou

1

Si vous recherchez l'utilisation Zend_Db_Select, ici il est

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

$db devrait être votre Zend_Db_Adapter.