简体   繁体   English

从3个到多个表的嵌套json响应

[英]Nested json response from 3 one to many tables

I am designing data model in following way: 我正在通过以下方式设计数据模型:

Table 1: 表格1:
id: number 身份证号
somedata: String somedata:字符串
somedata: String somedata:字符串

Table 2: 表2:
id: number 身份证号
Table1_id: number Table1_id:编号
somedata: String somedata:字符串
somedata: String somedata:字符串

Table 3: 表3:
id: number 身份证号
Table2_id: number Table2_id:编号
somedata: String somedata:字符串

1st table is joined to 2nd table with one to many relationship. 第一个表以一对多关系连接到第二个表。 2nd table is joined to 3rd table with one to many relationship. 第二表以一对多关系连接到第三表。 I am using java, JPA. 我正在使用Java,JPA。 I have to return JSON via rest api in following format: 我必须通过rest api以以下格式返回JSON:

{
  "table1_id": 1,
  "somedata": "somedata",
  "table2_data": [
    {
      "table2_id": 1,
      "somedata": "somedata",
      "table3_data": [
        {
          "table3_id": 1,
          "somedata": "somedata"
        },
        {
          "table3_id": 2,
          "somedata": "somedata"
        }
      ]
    },
    {
      "table2_id": 2,
      "somedata": "somedata",
      "table3_data": [
        {
          "table3_id": 3,
          "somedata": "somedata"
        },
        {
          "table3_id": 4,
          "somedata": "somedata"
        }
      ]
    }
  ]
}

If I use mysql and above table structure I will need to use minimum 3 database calls to fetch from 3 tables separately and will have to construct my json. 如果我使用mysql和以上的表结构,则需要至少使用3个数据库调用来分别从3个表中获取数据,并且必须构造json。 Is there a way where I can reduce database calls and also reduce effort to construct my json? 有没有一种方法可以减少数据库调用并减少构造json的工作量? I am open to change database(mysql) also. 我也愿意更改数据库(mysql)。 I just need fast, optimised and best solution. 我只需要快速,优化和最佳的解决方案。 Note that table1, table2 and table3 has different fields so I can not denormalize them to single table. 请注意,table1,table2和table3具有不同的字段,因此我无法将它们归一化为单个表。

Sorry for bad formatting of json. 很抱歉json格式错误。 Couldn't figure out how to show it here. 无法弄清楚如何在这里显示它。

I already have seen this question and my question is different than this: Nested JSON from 3 one-to-many Tables 我已经看过这个问题,但我的问题与此不同: 3个一对多表中的嵌套JSON

Edit: I got the answer here: https://dba.stackexchange.com/questions/164370/nested-json-response-from-3-one-to-many-tables 编辑:我在这里得到了答案: https : //dba.stackexchange.com/questions/164370/nested-json-response-from-3-one-to-many-tables

I recommend you to perform join first of all three table and then you can manipulate the outcome of the database query(depend on your select statement) as per your requirement as i see you created a json object of the tables.For example there is three tables Category Subcategory and Items lets see how i perform join and then fetch json data according to requirement:- 我建议您首先对所有三个表执行联接,然后您可以根据需要操作数据库查询的结果(取决于select语句),因为我看到您创建了表的json对象。例如,有三个表格Category Subcategory和Items让我们了解如何执行联接,然后根据要求获取json数据:-

    $query = "SELECT
items.id, items.category_id, items.sub_category_id, items.price_per_quantity, items.discounted_price, items.total_quantity, items.image, items.status, items.name as name, sub_categories.name as sub_category_name, categories.name as category_name
FROM items
    INNER JOIN sub_categories ON sub_categories.id = items.sub_category_id
    INNER JOIN categories ON categories.id  = items.category_id";

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

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