如何对查询中的结果进行分组?
我有一个 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`)
I have MySQL query that selects user names and theirs knowledge (may be more than one).
It returns something like this...
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"
}
}
As you can see, in this example, it does returns five entries. However, three of them have duplicate IDs (and names). I'm looking for a way to return only three entries like this...
Is it possible to do in the query?
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"
}
}
}
Other option I see is to process results on server-side.
Here is how the query looks like:
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`)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于这种特殊情况,您可以编写如下内容:
然而,使用 ORM 例如 Doctrine 来处理这种情况。
For this particular case you could write something like this:
However it's a better idea to use an ORM such as Doctrine to handle such situations.