MySQL 似乎没有 UNNEST 支持,所以我无法阅读相关手册。
您还没有提供任何示例输入和您期望的输出,但假设您 abc、def 和 xzy 是独立大小的数组,并且您想要以下 SQL 应该的所有排列工作:
WITH cte_t(id,date, abc, def, xyz, productid) AS (
SELECT * FROM VALUES
(1, '2022-01-23'::date, 'a_b_c_d', 'd_e_f', 'x_y_w', 'not empty'),
(2, '2022-01-23'::date, 'a_d', 'd_f', 'y_w', 'not empty'),
(3, '2022-01-23'::date, 'a_c_d', 'f', 'y1_y2_w', 'not empty')
)
SELECT
t.id,
t.date,
a.value AS catid,
b.value AS productid,
c.value AS quantity
FROM cte_t AS t
,LATERAL SPLIT_TO_TABLE(t.abc, '_') a
,LATERAL SPLIT_TO_TABLE(t.def, '_') b
,LATERAL SPLIT_TO_TABLE(t.xyz, '_') c
WHERE productid != ''
给予:
| ID |
DATE |
CATID |
PRODUCTID |
QUANTITY |
| 1 |
2022-01-23 |
a |
d |
x |
| 1 |
2022-01-23 |
b |
d |
x |
| 1 |
2022-01-23 |
c |
d |
x |
| 1 |
2022-01-23 |
d |
d |
x |
| 1 |
2022-01-23 |
a |
e |
x |
| 1 |
2022-01-23 |
b |
e |
x |
| 1 |
2022-01-23 |
c |
e |
x |
| 1 |
2022-01-23 |
d |
e |
x |
| 1 |
2022-01-23 |
a |
f |
x |
| 1 |
2022-01-23 |
b |
f |
x |
| 1 |
2022-01-23 |
c |
f |
x |
| 1 |
2022-01-23 |
d |
f |
x |
| 1 |
2022-01-23 |
a |
d |
y |
| 1 |
2022-01-23 |
b |
d |
y |
| 1 |
2022-01-23 |
c |
d |
y |
| 1 |
2022-01-23 |
d |
d |
y |
| 1 |
2022-01-23 |
a |
e |
y |
| 1 |
2022-01-23 |
b |
e |
y |
| 1 |
2022-01-23 |
c |
e |
y |
| 1 |
2022-01-23 |
d |
e |
y |
| 1 |
2022-01-23 |
a |
f |
y |
| 1 |
2022-01-23 |
b |
f |
y |
| 1 |
2022-01-23 |
c |
f |
y |
| 1 |
2022-01-23 |
d |
f |
y |
| 1 |
2022-01-23 |
a |
d |
w |
| 1 |
2022-01-23 |
b |
d |
w |
| 1 |
2022-01-23 |
c |
d |
w |
| 1 |
2022-01-23 |
d |
d |
w |
| 1 |
2022-01-23 |
a |
e |
w |
| 1 |
2022-01-23 |
b |
e |
w |
| 1 |
2022-01-23 |
c |
e |
w |
| 1 |
2022-01-23 |
d |
e |
w |
| 1 |
2022-01-23 |
a |
f |
w |
| 1 |
2022-01-23 |
b |
f |
w |
| 1 |
2022-01-23 |
c |
f |
w |
| 1 |
2022-01-23 |
d |
f |
w |
| 2 |
2022-01-23 |
a |
d |
y |
| 2 |
2022-01-23 |
d |
d |
y |
| 2 |
2022-01-23 |
a |
f |
y |
| 2 |
2022-01-23 |
d |
f |
y |
| 2 |
2022-01-23 |
a |
d |
w |
| 2 |
2022-01-23 |
d |
d |
w |
| 2 |
2022-01-23 |
a |
f |
w |
| 2 |
2022-01-23 |
d |
f |
w |
| 3 |
2022-01-23 |
a |
f |
y1 |
| 3 |
2022-01-23 |
c |
f |
y1 |
| 3 |
2022-01-23 |
d |
f |
y1 |
| 3 |
2022-01-23 |
a |
f |
y2 |
| 3 |
2022-01-23 |
c |
f |
y2 |
| 3 |
2022-01-23 |
d |
f |
y2 |
| 3 |
2022-01-23 |
a |
f |
w |
| 3 |
2022-01-23 |
c |
f |
w |
| 3 |
2022-01-23 |
d |
f |
w |
好的,这是我所期望的更多数据,但是你去吧。
如果这三个数组大小相同,那么您可以使用一个SPLIT_TO_TALBE 和两个SPLIT_PART:
WITH cte_t(id,date, abc, def, xyz, productid) AS (
SELECT * FROM VALUES
(1, '2022-01-23'::date, 'a_b_c', 'd_e_f', 'x_y_w', 'not empty')
)
SELECT
t.id,
t.date,
a.value AS catid,
split_part(t.def, '_', a.index) AS productid,
split_part(t.xyz, '_', a.index) AS quantity
FROM cte_t AS t
,LATERAL SPLIT_TO_TABLE(t.abc, '_') a
WHERE productid != ''
给予
| ID |
DATE |
CATID |
PRODUCTID |
QUANTITY |
| 1 |
2022-01-23 |
a |
d |
x |
| 1 |
2022-01-23 |
b |
e |
y |
| 1 |
2022-01-23 |
c |
f |
w |