📜  postgresql 将子查询显示为 json - SQL 代码示例

📅  最后修改于: 2022-03-11 15:05:02.911000             🧑  作者: Mango

代码示例2
// joining 3 tables and nesting results as json, keys can be named arbitrarily

select
        json_build_object(
                'id', u.id,
                'name', u.name,
                'email', u.email,
                'user_role_id', u.user_role_id,
                'user_role', json_build_object(
                        'id', ur.id,
                        'name', ur.name,
                        'description', ur.description,
                        'duty_id', ur.duty_id,
                        'duty', json_build_object(
                                'id', d.id,
                                'name', d.name
                        )
                )
    )
from users u
inner join user_roles ur on ur.id = u.user_role_id
inner join role_duties d on d.id = ur.duty_id;