Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

select query for one-to-many relations table?

I have 2 tables. These two tables have one-to-many relations.

TABLE – A

column1 column2
1       label1
2       label2

TABLE – B

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Bcolumn1 Bcolumn2 Bcolumn3
1        value1   value4
1        value2   value5
2        value3   value6

RESULT TABLE


column1 column2 json
1       label1  [[value1,value4],[value2,value5]]
2       label2  [[value3,value6]]

I want to get RESULT TABLE1 using TABLE – A and TABLE – B.

how can I get this result?

Thank you.

>Solution :

We could use GROUP_CONCAT here:

SELECT
    a.column1,
    a.column2,
    '[' || GROUP_CONCAT('[' || b.Bcolumn2 || ',' || b.Bcolumn3 || ']') || ']' AS json
FROM TableA a
LEFT JOIN TableB b
    ON b.Bcolumn1 = a.column1
GROUP BY
    a.column1,
    a.column2;

Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading