2011-03-08 89 views

回答

4

只需发出command并设置密钥distinct

看看下面的例子从文档:

查找所有不同值的关键。

<?php 

$people = $db->people; 

$people->insert(array("name" => "Joe", "age" => 4)); 
$people->insert(array("name" => "Sally", "age" => 22)); 
$people->insert(array("name" => "Dave", "age" => 22)); 
$people->insert(array("name" => "Molly", "age" => 87)); 

$ages = $db->command(array("distinct" => "people", "key" => "age")); 

foreach ($ages['values'] as $age) { 
    echo "$age\n"; 
} 

?> 

上例的输出类似于:

4 
22 
87 
+1

这个答案没有按” t解决了“WHERE”子句 – monofonik 2012-07-10 01:35:32

4

如果您需要添加WHERE子句,请使用以下语法:

$ages = $db->command(array(
    "distinct" => "people", 
    "key" => "age", 
    "query" => array("someField" => "someValue")));