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)) |

A1、A2:执行简单SQL,条件查询。

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

相关推荐
lsx20240636 分钟前
jQuery 删除元素
开发语言
高梦轩5 小时前
MySQL高可用
android·运维·数据库
紫金修道8 小时前
【DeepAgent】概述
开发语言·数据库·python
Via_Neo8 小时前
JAVA中以2为底的对数表示方式
java·开发语言
书到用时方恨少!8 小时前
Python multiprocessing 使用指南:突破 GIL 束缚的并行计算利器
开发语言·python·并行·多进程
cch89188 小时前
PHP五大后台框架横向对比
开发语言·php
孟章豪8 小时前
《SQL拼接 vs 参数化,为什么公司禁止拼接SQL?(附真实案例)》
服务器·数据库·sql
荒川之神8 小时前
ORACLE LEVEL函数练习
数据库·oracle
·云扬·8 小时前
【MySQL】实战:用pt-table-sync修复主从数据一致性问题
数据库·mysql·ffmpeg
天真萌泪8 小时前
JS逆向自用
开发语言·javascript·ecmascript