简体   繁体   English

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

[英]Dump data in json format from two tables from MySQL

I have the following scenario. 我有以下情况。 I have 2 tables as follow Table 1 ID, Name, Desc, Seo 我有2个表,如下表1 ID,名称,Desc,Seo

Table 2 ID, Table1_ID, Relation_Table1_ID 表2 ID,Table1_ID,Relationship_Table1_ID

in Table 1 I have all my data that I need as: 在表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   |
-----------------------------------

And then we have our table 2 as follow 然后我们的表2如下

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

So far I have my first table dump with jSON() as follow: 到目前为止,我使用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;
?>

That code will give me the following: 该代码将为我提供以下内容:

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

What I can't figure is how do I get the following 我无法确定的是如何获得以下信息

[
  {
    "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"
            }
          ]
  }... ... ...

]

In plain text it would be something like 用纯文本格式将类似于

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

I'm learning how to use json and I just got the first part as you can see, but my lack of knowledge of SQL and php wont let me get the what I really want, so please can any one help me achieve this scenario please. 我正在学习如何使用json,正如您所见,我只是获得了第一部分,但是由于我对SQL和php的了解不足,所以我无法真正获得想要的东西,所以请有人可以帮助我实现此方案吗? 。

Thank you. 谢谢。

you can loop through $getlist 您可以遍历$ getlist

then query 然后查询

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;
}

then take the result of that, inside the loop, and assign it to whichever key of getlist you want for example 然后在循环内获取结果,然后将其分配给您想要的getlist的任意键

hopefully thats specific enough. 希望多数民众赞成在足够具体。 let me know if you have problems with that. 让我知道您是否对此有疑问。

You need to join across the two tables. 您需要连接两个表。 First, join the second table with the first table. 首先,将第二个表与第一个表连接起来。 This can be done with the following code: 可以使用以下代码完成此操作:

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

which results in: 结果是:

+------+-----------+--------+------+------+------+------+
| 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   |
+------+-----------+--------+------+------+------+------+

now we want to join the columns if the table1_id is the same. 现在,如果table1_id相同,我们想加入列。 This can be done with the GROUP_CONCAT command: 这可以通过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

which results in: 结果是:

+-----------+-----------+----------+---------+--------+
| 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    |
+-----------+-----------+----------+---------+--------+

The as command can be used to rename columns in the output. as命令可用于重命名输出中的列。 Try running the query without it and you should see column names like group_concat(t1.name) instead of name_rel . 尝试在没有查询的情况下运行查询,您应该看到诸如group_concat(t1.name)类的列名,而不是name_rel

Now we want to join this selection with table_1 where the table1_id is the same as the id in table1. 现在,我们要将此选择与table_1联接起来,其中table1_id与table1中的id相同。 This can be done with the following query: 可以使用以下查询完成此操作:

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;

which results in: 结果是:

+-----------+-----------+----------+---------+--------+------+--------+--------+------+
| 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    |
+-----------+-----------+----------+---------+--------+------+--------+--------+------+

As you can see, you know have all the data. 如您所见,您知道所有数据。 All that is left is to turn the *_rel columns into separate objects, which can be done in php. 剩下的就是将* _rel列变成单独的对象,可以在php中完成。 Go ahead and give that a shot. 继续尝试一下。 I've gotta go for now, but I'll edit this post later if you're still having trouble. 我得先走了,但是如果您仍然遇到问题,我稍后会编辑。

Alright, I'm back. 好吧,我回来了。 My php is rusty, but something along these lines should work: 我的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;

This will create each of the Relations objects and add them to the row. 这将创建每个Relations对象,并将它们添加到行中。 The unset commands should remove the keys we no longer care about (id_rel, name_rel, desc_rel, and seo_rel). unset命令应该删除我们不再关心的键(id_rel,name_rel,desc_rel和seo_rel)。 We no longer care about them because their data should have been moved to the Relations object. 我们不再关心它们,因为它们的数据应该已经移动到Relations对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM