从MySQL的两个表转储json格式的数据

时间:2013-07-31 21:35:56

标签: php mysql json

我有以下情况。 我有2张表如下 表格1 ID,Name,Desc,Seo

表2 ID,Table1_ID,Relation_Table1_ID

表1中的

我拥有我需要的所有数据:

-----------------------------------
|ID | Name        |Desc      |Seo |
-----------------------------------
| 1 | Smith       |Father    |f   |
| 2 | Jonh        |Son       |j   |
| 3 | Margat      |Mother    |m   |
| 4 | Bis3        |son       |b1  |
| 5 | Bis2        |son       |b2  |
| 6 | Bis1        |son       |b3  |
| 7 | Lanos       |Brother   |l   |
-----------------------------------

然后我们的表2如下

-------------------------------------
|ID | Table1_ID   |Relation_Table1_id|
--------------------------------------
| 1 |     1       |         4        |
| 2 |     1       |         5        |
| 3 |     3       |         6        |
| 4 |     3       |         2        |
| 5 |     7       |         0        |
--------------------------------------

到目前为止,我的第一个表转储带有jSON(),如下所示:

<?php
include ('config.php');
$dump1 = mysql_query("SELECT * FROM Table1 ") or die(mysql_error()); 
 $getList = array(); 
while ($row = mysql_fetch_assoc($dump1)) { 
    $getList[] = $row;
}
  print json_encode($getList);   exit;
?>

该代码将为我提供以下内容:

[
  {
    "ID":"1",
    "Name":"Smith",
    "Desc":"Father",
    "Seo":"f"
  },
{
    "ID":"2",
    "Name":"Jonh",
    "Desc":"Son",
    "Seo":"j"
  },
{
    "ID":"3",
    "Name":"Margat",
    "Desc":"Mother",
    "Seo":"m"
  }... ... ...
]

我无法想象的是如何获得以下内容

[
  {
    "ID":"1",
    "Name":"Smith",
    "Desc":"Father",
    "Seo":"f",
        "Relations":[
            {
             "ID":"4",
             "Name":"Bis3",
             "Desc":"Son",
             "Seo":"b1"
            }
          ]
  },
  {
    "ID":"3",
    "Name":"Margat",
    "Desc":"Father",
    "Seo":"f",
        "Relations":[
            {
             "ID":"6",
             "Name":"Bis2",
             "Desc":"Son",
             "Seo":"b2"
            },

            {
             "ID":"2",
             "Name":"Jonh",
             "Desc":"Son",
             "Seo":"j"
            }
          ]
  }... ... ...

]

在纯文本中,它将类似于

  ID 1 Smith
  |   |_ID 4 Bis3
  |   
  |_ ID 3 Margat
      |_ID 5 Bis2
      |_ID 2 Jonh

我正在学习如何使用json,我刚刚看到了第一部分,但是我对SQL和php的不了解不会让我得到我真正想要的东西,所以请任何人帮我实现这个场景请。

谢谢。

2 个答案:

答案 0 :(得分:0)

你可以遍历$ getlist

然后查询

for($i = 0; $i <= count($getlist); $i++){
    "SELECT * FROM Table2 WHERE Table1_ID = $getlist[$i]['ID']"
// get the result however way
$getlist[$i]['something'] = $result;
}

然后在循环中获取结果,并将其分配给您想要的getlist的任何键 例如

希望这个具体到底。如果你有问题,请告诉我。

答案 1 :(得分:0)

您需要跨两个表加入。首先,将第二个表与第一个表连接起来。这可以使用以下代码完成:

select * from table_2 as t2 join table_1 as t1 on t1.id = t2.rel_id;

导致:

+------+-----------+--------+------+------+------+------+
| ID   | table1_id | rel_id | ID   | name | desc | seo  |
+------+-----------+--------+------+------+------+------+
|    4 |         3 |      2 |    2 | John | Son  | j    |
|    1 |         1 |      4 |    4 | Bis3 | son  | b1   |
|    2 |         1 |      5 |    5 | Bis2 | son  | b2   |
|    3 |         3 |      6 |    6 | Bis1 | son  | b3   |
+------+-----------+--------+------+------+------+------+

现在我们想要在table1_id相同的情况下加入列。这可以使用GROUP_CONCAT命令完成:

select table1_id,group_concat(t1.name) as name_rel,
group_concat(t1.desc) as desc_rel, group_concat(seo) as seo_rel,
group_concat(rel_id) as id_rel from table_2 as t2 join table_1 as t1 on 
t2.rel_id = t1.id group by t2.table1_id

导致:

+-----------+-----------+----------+---------+--------+
| table1_id | name_rel  | desc_rel | seo_rel | id_rel |
+-----------+-----------+----------+---------+--------+
|         1 | Bis2,Bis3 | son,son  | b2,b1   | 5,4    |
|         3 | John,Bis1 | Son,son  | j,b3    | 2,6    |
+-----------+-----------+----------+---------+--------+

as命令可用于重命名输出中的列。尝试在没有它的情况下运行查询,您应该看到group_concat(t1.name)而不是name_rel的列名。

现在我们要将此选择与table_1连接,其中table1_id与table1中的id相同。这可以通过以下查询完成:

select * from (
select table1_id,group_concat(t1.name) as name_rel,
group_concat(t1.desc) as desc_rel, group_concat(seo) as seo_rel,
group_concat(rel_id) as id_rel from table_2 as t2 join table_1 as t1 on
t2.rel_id = t1.id group by t2.table1_id
) as joined_table 
join table_1 as t3 on t3.id = joined_table.table1_id;

导致:

+-----------+-----------+----------+---------+--------+------+--------+--------+------+
| table1_id | name_rel  | desc_rel | seo_rel | id_rel | ID   | name   | desc   | seo  |
+-----------+-----------+----------+---------+--------+------+--------+--------+------+
|         1 | Bis2,Bis3 | son,son  | b2,b1   | 5,4    |    1 | Smith  | Father | f    |
|         3 | John,Bis1 | Son,son  | j,b3    | 2,6    |    3 | Margat | Mother | m    |
+-----------+-----------+----------+---------+--------+------+--------+--------+------+

如您所见,您知道拥有所有数据。剩下的就是将* _rel列转换为单独的对象,这可以在php中完成。 继续拍摄。我现在必须去,但如果你还有问题,我会稍后编辑这篇文章。

好的,我回来了。我的php很生疏,但这些内容应该有效:

while ($row = mysql_fetch_assoc($dump1)) { 
    $name_rel = explode("," , $row["name_rel"]);
    $desc_rel = explode("," , $row["desc_rel"]);
    $seo_rel = explode("," , $row["seo_rel"]);
    $id_rel = explode("," , $row["id_rel"]);
    $relations = array();
    for($i = 0; $i < count($id_rel); ++$i){
        $temp = array("ID" => $id_rel[$i],
                      "Name" => $name_rel[$i],
                      "Desc" => $desc_rel[$i],
                      "Seo" => $seo_rel[$i],);
        $relations[] = $temp ;
    }
    unset($row["id_rel"]);
    unset($row["name_rel"]);
    unset($row["desc_rel"]);
    unset($row["seo_rel"]);
    $row["Relations"] = $relations ;
    $getList[] = $row;
}
print json_encode($getList);   exit;

这将创建每个Relations对象并将它们添加到行中。 unset命令应该删除我们不再关心的键(id_rel,name_rel,desc_rel和seo_rel)。我们不再关心它们,因为它们的数据应该已移至Relations对象。