gpt4 book ai didi

sql - 查找多列的引用值

转载 作者:行者123 更新时间:2023-11-29 13:24:44 26 4
gpt4 key购买 nike

我有一个表 Setpoints,其中包含 3 列 BaseEffectiveActual,其中包含一个 id 是指在 io 中找到的项目。

我想进行一个查询,该查询将返回在 io 表中找到的 io_value 以及在 Setpoints 中找到的 id

目前我的查询将返回多个 id,然后我查询 io 表以找到每个 id 的 io_value

Ex 查询返回行中的 ID

row #   |  base  | effective  | actual
1 | 24 | 30 | 40
2 | 25 | 31 | 41
3 | 26 | 32 | 42

但我希望它返回值而不是 id

Ex 返回 id 的值

row #   |  base  | effective  | actual
1 | 2.3 | 4.5 | 3.44
2 | 4.2 | 7.7 | 4.41
3 | 3.9 | 8.12 | 5.42

这是表格字段

IO
io_value
io_id

Setpoints
stpt_base
stpt_effective
stpt_actual

使用 postgres 9.5

我现在在用什么

SELECT * from setpoints
For each row
SELECT io_id, io_value
from io
where io_id in
(stpt_effective, stpt_actual, stpt_base);
// these are from previous query

最佳答案

您可以通过将 io 表三次连接到 setpoints 表来解决此问题,在每个单独的 JOIN 中使用三列:

SELECT a.io_value AS base,
b.io_value AS effective,
c.io_value AS actual
FROM setpoints s
JOIN io a ON a.io_id = s.stpt_base
JOIN io b ON b.io_id = s.stpt_effective
JOIN io c ON c.io_id = s.stpt_actual;

关于sql - 查找多列的引用值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35730869/

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