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,最后再合并循环的结果。

相关推荐
一抓掉一大把3 分钟前
秒杀-StackExchangeRedisHelper连接单例
java·开发语言·jvm
升鲜宝供应链及收银系统源代码服务4 分钟前
升鲜宝生鲜配送供应链管理系统--- 《多语言商品查询优化方案(Redis + 翻译表 + 模糊匹配)》
java·数据库·redis·bootstrap·供应链系统·生鲜配送·生鲜配送源代码
JH30736 分钟前
Redis 中被忽视的“键过期策略”与内存回收机制
数据库·redis·缓存
Microsoft Word6 分钟前
Redis常见面试题
数据库·redis·缓存
bing.shao11 分钟前
mongodb与redis在聊天场景中的选择
数据库·redis·mongodb
dudke11 分钟前
c#实现redis的调用与基础类
数据库·redis·缓存
苦学编程的谢12 分钟前
Redis_7_hash
数据库·redis·哈希算法
许愿OvO13 分钟前
MySQL-索引
数据库·mysql
-指短琴长-14 分钟前
MySQL快速入门——基本查询(上)
android·数据库·mysql
星释17 分钟前
Rust 练习册 :Minesweeper与二维数组处理
开发语言·后端·rust