2011-03-08 23 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")));