将表记录联接到嵌套JSON

时间:2019-05-25 11:03:35

标签: mysql sql

MySQL是否与JSON集成在一起,还是只能使用代码嵌套?

用户

+----+-------+------+
| id | first | dept |
+----+-------+------+
|  1 | John  | 1    |
|  2 | Jane  | 2    |
+----+-------+------+

部门

+----+----------+
| id |   name   |
+----+----------+
|  1 | admin    |
|  2 | accounts |
|  3 | sales    |
+----+----------+

SQL

SELECT * FROM user
INNER JOIN dept ON user.dept = dept.id

当前输出

[
    {
        "id": 1,
        "first": "John",
        "dept": 1,
        "id": 1,
        "name": "admin"
    },
    {
        "id": 2,
        "first": "Jane",
        "dept": 2,
        "id": 2,
        "name": "accounts"
    },
]

预期产量

[
    {
        "id": 1,
        "first": "John",
        "dept": {"id": 1, "name": "admin"}
    },
    {
        "id": 2,
        "first": "Jane",
        "dept": {"id": 2, "name": "accounts"}
    }
]

我当前的实现是将其嵌套在代码中,想知道SQL中是否有最佳解决方案。

0 个答案:

没有答案