Search
 
SCRIPT & CODE EXAMPLE
 

SQL

postgresql display subquery as json

// 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;
Comment

postgresql display subquery as json

// PostgreSQL >= 9.4 : to_json(), json_build_object(), json_object() and json_build_array(), 
// though it's verbose due to the need to name all the fields explicitly:

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

PREVIOUS NEXT
Code Example
Sql :: Insert and initialize a SQL column with value dependent on another column data 
Sql :: create more than 1 tables with references to each other in sqlite3 
Sql :: SQLAlchemy query to return only n results? 
Sql :: rollback to in sql 
Sql :: install phpmyadmin pma 
Sql :: mysql faster delete 
Sql :: which is the order of precedence among following operator IN OUT AND OR in sql 
Sql :: apex call duration 
Sql :: power query case when 
Sql :: mysql server on and off 
Sql :: how to make sure two tables have same exact data in sql 
Sql :: id desde sql 
Sql :: how to compile a function in oracle 
Sql :: firebase sql 
Sql :: VERIFY INDEXES WITH PARTITIONS IN SQL ORACLE 
Sql :: employee name starting with in sql 
Sql :: export all stored procedures to .sql files 
Sql :: kie business put user infos in database 
Sql :: dbt unique key 
Sql :: Rows, INSERT INTO 
Sql :: MySQL 8.0 - Client does not support authentication protocol requested by server; consider upgrading MySQL client 
Sql :: max in postgresql 
Sql :: run sql command line download 
Sql :: check if two tables are identical sql 
Sql :: sql server isnull function nor working count 
Csharp :: get appdata file path c# 
Csharp :: c# check if type implements interface 
Csharp :: check if string is email c# 
Csharp :: unity to string 
Csharp :: how to clear console in c# 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =