SQL,将多对多的关联记录按行输出

数据库的Primary表和Secondary表有相同的结构,其中W、H、D是主键。Primary表:

Name

W

H

D

Primary item 1

100

500

300

Primary item 2

100

600

300

Primary item 3

200

500

300

Primary item 4

100

500

300

Primary item 5

100

600

300

Primary item 6

200

500

300

Secondary表:

Name

W

H

D

Secondary item 1

100

500

300

Secondary item 2

100

600

300

Secondary item 3

200

500

300

Secondary item 4

100

500

300

Secondary item 5

100

600

300

Secondary item 6

200

500

300

要求从两张表中找出符合条件W=100,H=500,D=300的记录,先输出Primay里的一行,再输出相关联的Secondary的每一行。

Name

W

H

D

Primary item 1

100

500

300

Secondary item 1

100

500

300

Secondary item 4

100

500

300

Primary item 4

100

500

300

Secondary item 1

100

500

300

Secondary item 4

100

500

300

编写SPL代码:


A

1

=sqlServer1.query("select * from Primary where W = 100 and H = 500 and D = 300")

2

=sqlServer1.query("select * from Secondary where W = 100 and H = 500 and D = 300")

3

=if(A2,A1.conj(~|A2))

A1A2:执行简单SQL,条件查询

A3:A2为空时返回空,A2不为空时,循环A1每条记录 ~,合并 ~ 和 A2,最后再合并循环的结果。

来源:https://stackoverflow.com/questions/78435969/sql-join-two-tables-as-many-times-as-there-are-matches-in-the-first-table