从两个表创建自定义json数组

时间:2013-12-09 11:59:06

标签: php mysql json api rest

我在mysql中有下表。     1)用户

userid    username    college
1         robert      sarauniversity
2         albert      oxford

2)UserDetails
userid     subjectid
1          1
1          2
2          3
1          4
2          1

3)subjectdetails
subjectid  subjectname          subjectteacher
1          basic science        pro. vengaskar
2          advance mathematics  pro. richard
3          history              pro. michale
4          geography            pro. renuka

我需要在json中跟踪数据

  [  
  {
   id:1
   username:robert      
   subjects:[
   {
       subjectname:pro. geography            ,
       subject teacher:algebra

   },
  {
       subjectname:pro. vengaskar,
       subject teacher:pro. renuka
  }
  ]
  },
  {
   id:2
   username:albert      
   subjects:[
   {
       subjectname:history                          ,
       subject teacher: pro. michale

   },
  {
       subjectname:basic science,
       subject teacher: pro. vengaskar

  }
  ]
  }
  }

我已经多次开发了json rest api,但这次它是带有json数组的对象的json数组。 我迷惑了如何迭代内容并获得最终所需的json。 我没有粘贴PHP代码,因为没有什么可以帮助我。 很难过,我花了两天时间,但仍然在开始......

2 个答案:

答案 0 :(得分:1)

使用这样的查询得到结果

SELECT A.userid,A.username, C.subjectname,C.subjectteacher FROM `A` AS users INNER JOIN `UserDetails` AS B on A.userid = b.userid INNER JOIN `subjectdetails` AS C on C.subjectid = B.subjectid

然后在循环中以下面的格式操作结果

$arr = array (
        array (
                "id" => "1",
                "username" => "Robert",
                "subjects" => array (
                        array (
                                "subjectname" => "pro. geography",
                                "subject teacher" => "algebra" 
                        ) 
                ) 
        ) 
);

echo json_encode ( $arr );

输出

[
    {
        "id": "1",
        "username": "Robert",
        "subjects": [
            {
                "subjectname": "pro. geography",
                "subject teacher": "algebra"
            }
        ]
    }
]

答案 1 :(得分:0)

试试这个

SELECT DISTINCT @pv := usr.userid, CONCAT(  "[", CONVERT( GROUP_CONCAT(DISTINCT  "{\"id\":", usr.userid,  ", \"subjects\":", (
SELECT CONCAT(  "[", CONVERT( GROUP_CONCAT(  "{\"id\":", subjectid,  ", \"subjectname\":\"", subjectname,  "\"}" ) 
USING utf8 ) ,  "]" ) 
FROM subjectdetails
WHERE subjectid
IN (
SELECT subjectid
FROM UserDetails
WHERE userid = @pv)
),  ", \"username\":\"", usr.username,  "\"}" ) 
USING utf8 ) ,  "]" ) AS jsn
FROM User usr
JOIN UserDetails det ON det.userid = usr.userid
LEFT OUTER JOIN subjectdetails sub ON sub.subjectid = det.subjectid

输出:

ID  JSN
1   [{"id":1, "subjects":[{"id":1, "subjectname":"basic science"},{"id":2, "subjectname":"advance mathematics"},{"id":4, "subjectname":"geography"}], "username":"robert"},{"id":2, "subjects":[{"id":1, "subjectname":"basic science"},{"id":2, "subjectname":"advance mathematics"},{"id":4, "subjectname":"geography"}], "username":"albert"}]

具有结构

的JSN列
[
    {
        "id": 1,
        "subjects": [
            {
                "id": 1,
                "subjectname": "basic science"
            },
            {
                "id": 2,
                "subjectname": "advance mathematics"
            },
            {
                "id": 4,
                "subjectname": "geography"
            }
        ],
        "username": "robert"
    },
    {
        "id": 2,
        "subjects": [
            {
                "id": 1,
                "subjectname": "basic science"
            },
            {
                "id": 2,
                "subjectname": "advance mathematics"
            },
            {
                "id": 4,
                "subjectname": "geography"
            }
        ],
        "username": "albert"
    }
]

SQLFIDDLE

注意:问题中的JSON结构不是有效的。使用jsonlint生成有效的json。