DEV Community

Judy
Judy

Posted on

SQL, output associated records having many-to-many relationship row by row #eg26

Two database tables – Primary and Secondary – have same structure, where W, H and D are primary key fields.

Image description
Secondary table:

Image description
Task: Find records meeting the condition W=100, H=500, and D=300 from both tables, and output the eligible record in Primary table first and then each related record in Secondary table. Below is the expected result:

Image description
Write the following SPL code:

Image description
A1, A2: Run simple SQL statements to perform conditional queries.

A3: Return null if A2 is empty; and if A2 isn’t empty, loop through each record of A1 (represented by ~), concatenate ~ and A2, and then concatenate results of all rounds of loops.

Source

SPL open source address

Top comments (0)