Order-based Set Operations – Get Values by Foreign Key
【Question】
Table 1:
Table 2:
The expected result:
a,b,c
d,c
a,b,c
【Answer】
You can get it done using group_concat function and find_in_set function within a database. But this has two shortcomings. One is that the duplicates will be removed from the result set when Table 2’s ids field contains same values; the other is that SQL can’t make sure that records in the joining result set are ordered the same way as Table 2’s ids field. To avoid theses weaknesses, here I handle it in SPL (Structured Process Language) that supports order-based set operations. The script is simple:
A |
|
1 |
$select id,name from t1 |
2 |
$select ids from t2 |
3 |
>A1.keys(id) |
3 |
=A2.(ids.split@cp().(A1.find(~).name).concat@c()) |
A4’s result:
esProc provides JDBC interface to easily integrate with a Java application. See How to Call an SPL Script in Java.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL