0

我有以下情况。我有 2 个表,如下表 1 ID、名称、描述、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 的了解不会让我得到我真正想要的东西,所以请任何人都可以帮我实现这个场景.

谢谢你。

4

2 回答 2

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 的任何键,例如

希望那足够具体。如果您对此有任何疑问,请告诉我。

于 2013-07-31T22:27:16.413 回答
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.ini 中完成。来吧,试一试。我得先走了,但如果你还有问题,我稍后会编辑这篇文章。

好吧,我回来了。我的 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对象并将它们添加到行中。未设置的命令应该删除我们不再关心的键(id_rel、name_rel、desc_rel 和 seo_rel)。我们不再关心它们,因为它们的数据应该已经移动到Relations对象中。

于 2013-07-31T22:49:21.557 回答