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

相关推荐
踢球的打工仔35 分钟前
PHP面向对象(7)
android·开发语言·php
q***81642 小时前
MySQL:数据查询-limit
数据库·mysql
p***92482 小时前
DBeaver连接本地MySQL、创建数据库表的基础操作
数据库·mysql
汤姆yu3 小时前
基于python的外卖配送及数据分析系统
开发语言·python·外卖分析
Yue丶越3 小时前
【C语言】字符函数和字符串函数
c语言·开发语言·算法
JIngJaneIL3 小时前
社区互助|社区交易|基于springboot+vue的社区互助交易系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·社区互助
晚风吹人醒.3 小时前
缓存中间件Redis安装及功能演示、企业案例
linux·数据库·redis·ubuntu·缓存·中间件
翔云 OCR API3 小时前
人脸识别API开发者对接代码示例
开发语言·人工智能·python·计算机视觉·ocr
Y***98513 小时前
DVWA靶场通关——SQL Injection篇
数据库·sql
Yawesh_best3 小时前
告别系统壁垒!WSL+cpolar 让跨平台开发效率翻倍
运维·服务器·数据库·笔记·web安全