1

我使用 MySQL 和 PHP (+ PDO_mysql),我的目标是返回一些 JSON。

我有两个数据库表,部门和团队。

部门基本上只是一个 id + 部门名称。
一个团队基本上是一个 id + 团队名称和对应部门的外键。

一个团队只能属于一个部门,一个部门可以有多个团队(因此是一对多关系)。

我想以这种方式返回一些 JSON:

{
    "departments": [
        {
           "departmentname": "Kids",
           "teams": [
              {
                  "teamname": "Black n' white",
                  "homepage": "www.some.thing"
              },
              {
                  "teamname": "Team-1337",
                  "homepage": "www.some.thing"
              }
           ]
        },
        {
           "departmentname": "Kids",
           "teams": [
              {
                  "teamname": "I <3 Sundays",
                  "homepage": "www.some.thing"
              },
              {
                  "teamname": "Stack Overflow",
                  "homepage": "www.some.thing"
              }
           ]
        }
    ]
}

我想我必须以这种方式在两个表(部门和团队)之间进行 INNER JOIN:
SELECT * FROM team INNER JOIN department ON team.department_id=department.id

......最后我的 PHP-file use json_encode,但我不知道如何到达那里。

我非常感谢您能提供的任何帮助

4

3 回答 3

1

一种方法是使用以 ID 列为键的关联数组在 PHP 代码中而不是在 SQL 中执行连接。请注意,以下代码根本没有经过测试,但您明白了。

$pdo=new PDO("mysql:dbname=database;host=127.0.0.1","user","password");
$statement=$pdo->prepare("SELECT * FROM team WHERE department_id IN (SELECT department.id FROM department)");// get teams in departments
$statement->execute();
$teams=$statement->fetchAll(PDO::FETCH_ASSOC);
$statement=$pdo->prepare("SELECT * FROM department"); // get departments
$statement->execute();
$departments=$statement->fetchAll(PDO::FETCH_ASSOC); // Assumes that department id it the first field

// merge the teams with the departments
foreach($teams as $team){
    $teamDept = $departments[$team["department_id"]];// The department of current team
    // Create an associative list keyed on team id inside the departments
    if(!array_key_exists("teams", $teamDept))// Create team array in department if not exist
        $teamDept["teams"] = array(); // Unsure if I need to do this.
    $teamDept["teams"][$team["id"]] = $team;
}
$json=json_encode($departments);

另一种方法是使用 PDO::fetch_group。我对该主题进行了一些研究,但找不到太多示例或详细教程。不过看起来确实很有趣。

于 2013-11-11T18:26:55.773 回答
0
$pdo=new PDO("mysql:dbname=database;host=127.0.0.1","user","password");
$statement=$pdo->prepare("SELECT * FROM team INNER JOIN department ON team.department_id=department.id");
$statement->execute();
$results=$statement->fetchAll(PDO::FETCH_ASSOC);
$json=json_encode($results);

你是这个意思?

于 2013-10-20T10:20:48.703 回答
0

用于PDO::fetch_group根据第一列对记录进行分组。

这是一个例子:

$data = $pdo->query('SELECT sex, name, car FROM users')->fetchAll(PDO::FETCH_GROUP);
array (
  'male' => array (
    0 => array (
      'name' => 'John',
      'car' => 'Toyota',
    ),
    1 => array (
      'name' => 'Mike',
      'car' => 'Ford',
    ),
  ),
  'female' => array (
    0 => array (
      'name' => 'Mary',
      'car' => 'Mazda',
    ),
    1 => array (
      'name' => 'Kathy',
      'car' => 'Mazda',
    ),
  ),
)

示例来源:https ://phpdelusions.net/pdo

于 2018-02-14T04:51:40.557 回答