gpt4 book ai didi

MySQL 特例枢轴

转载 作者:行者123 更新时间:2023-11-28 23:21:36 25 4
gpt4 key购买 nike

我找了好几天都找不到答案!

在 MySQL 中我有 2 个表

ProcessList 包含所有来自进程表的外键

ID  |Operation1|Operation2|Operation3|etc....
---------------------------------------
1 | 1 | 4 | 6 | ....
---------------------------------------
2 | 2 | 4 | 5 |....
---------------------------------------
.
.
.

Process Table
ID | Name
-------------------
1 | Quote
2 | Order
3 | On-Hold
4 | Manufacturing
5 | Final Inpection
6 | Complete

现在,我是 SQL 的新手,但我知道 MYSQL 没有我研究过的枢轴函数,我看到了一些 UNION 等示例,但我需要一个类似(伪代码)的 SQL 表达式

SELECT name FROM process
(IF process.id APPEARS in a row of the ProcessList)
WHERE processListID = 2

所以我得到了结果

Order
Manufacturing
Final Inspection

我真的需要查询的最后一行

WHERE processListID = ?

因为否则我将不得不完全重写我的应用程序,因为 SQL 存储在 Java 中的字符串中,并且应用程序仅在语句末尾提供键索引。

最佳答案

一种选择是使用union 取消透视 processlist 表并将其连接到进程表。

select p.name
from process p
join (select id,operation1 as operation from processlist
union all
select id,operation2 from processlist
union all
select id,operation3 from processlist
--add more unions as needed based on the number of operations
) pl
on pl.operation=p.id
where pl.id = ?

关于MySQL 特例枢轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41368918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com