如何对查询中的结果进行分组?

发布于 2024-12-05 20:19:02 字数 2037 浏览 0 评论 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`)

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

腹黑女流氓 2024-12-12 20:19:02

对于这种特殊情况,您可以编写如下内容:

$users = array();

while ($row = /* fetch a single row from result set */) {
    if (isset($users[$row['user_id']]) == false) {
        $users[$row['user_id']] = array(
            'id' => $row['user_id'],
            'name' => $row['name'],
            'knowledges' => array()
        );
    }

    $users[$row['user_id']]['knowledges'][] = $row['knowledge'];
}

然而,使用 ORM 例如 Doctrine 来处理这种情况。

For this particular case you could write something like this:

$users = array();

while ($row = /* fetch a single row from result set */) {
    if (isset($users[$row['user_id']]) == false) {
        $users[$row['user_id']] = array(
            'id' => $row['user_id'],
            'name' => $row['name'],
            'knowledges' => array()
        );
    }

    $users[$row['user_id']]['knowledges'][] = $row['knowledge'];
}

However it's a better idea to use an ORM such as Doctrine to handle such situations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文