0
我有MySQL查詢,用於選擇用戶名和他們的知識(可能不止一個)。如何在查詢中對結果進行分組?
它返回這樣的事情...
array(5) {
[0]=>
array(5) {
["user_id"]=>
string(2) "30"
["name"]=>
string(6) "foo1"
["knowledge"]=>
string(15) "Basic Materials"
}
[1]=>
array(5) {
["user_id"]=>
string(2) "33"
["name"]=>
string(6) "foo2"
["knowledge"]=>
string(15) "Basic Materials"
}
[2]=>
array(5) {
["user_id"]=>
string(2) "34"
["name"]=>
string(10) "foo3"
["knowledge"]=>
string(9) "Eating"
}
[3]=>
array(5) {
["user_id"]=>
string(2) "34"
["name"]=>
string(10) "foo3"
["knowledge"]=>
string(9) "Financial"
}
[4]=>
array(5) {
["user_id"]=>
string(2) "34"
["name"]=>
string(10) "foo3"
["knowledge"]=>
string(8) "Services"
}
}
正如你所看到的,在這個例子中,它的回報五個條目。但是,其中三個有重複的ID(和名稱)。我正在尋找一種方式來回報只有三個條目是這樣的...
是否有可能在查詢呢?
array(5) {
[0]=>
array(5) {
["user_id"]=>
string(2) "30"
["name"]=>
string(6) "foo1"
["knowledges"]=>
array(1) {
[0] => string(15) "Basic Materials"
}
}
[1]=>
array(5) {
["user_id"]=>
string(2) "33"
["name"]=>
string(6) "foo2"
["knowledges"]=>
array(1) {
[0] => string(15) "Basic Materials"
}
}
[2]=>
array(5) {
["user_id"]=>
string(2) "34"
["name"]=>
string(10) "foo3"
["knowledges"]=>
array(1) {
[0] => string(15) "Eating"
[1] => string(15) "Financial"
[2] => string(15) "Services"
}
}
}
我看到其他選項來處理服務器端的結果。
下面是該查詢的樣子:
SELECT `profiles`.`user_id`, `users`.`name`, `users`.`surname`, `users`.`country`, `profile_knowledges`.`knowledge`
FROM `profiles`
JOIN `users`
ON (`users`.`id` = `profiles`.`user_id`)
JOIN `profile_knowledges`
ON (`profile_knowledges`.`profile_id` = `profiles`.`id`)
謝謝,這是我的想法。 [This](http://pastie.org/2567903)是我想出來的......如何用ORM實現這一點? :) – daGrevis