与JSON的一对多数据库关系

时间:2013-10-20 10:12:46

标签: php mysql json

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

我有两个数据库表,Department和Team。

部门基本上只是一个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"
              }
           ]
        }
    ]
}

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

...在我的PHP文件的最后使用json_encode,但我不知道如何到达那里。

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

3 个答案:

答案 0 :(得分: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。我对这个主题进行了一些研究,但是找不到很多例子或详细的教程。它确实看起来很有趣。

答案 1 :(得分: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);

你是说这个?

答案 2 :(得分: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