在mysql上构建一个select distinct(zend_db)

时间:2010-09-22 08:48:26

标签: mysql zend-framework zend-db

我有以下表格,我想在列[代码]上做一个选择区别,我不需要三次获得“A”。

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

我希望结果如下

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

3 个答案:

答案 0 :(得分:3)

只需添加

即可
group by code 
ORDER BY code ASC

在您的SQL查询结束时

<强> 例如

select * from table
group by code 
ORDER BY code ASC

答案 1 :(得分:1)

 SELECT Min(Id) Id, Code, MIN(Libelle) Libelle
 from table
 group by code

答案 2 :(得分:1)

如果您正在寻找Zend_Db_Select用法,请点击此处

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

$db应该是您的Zend_Db_Adapter