gpt4 book ai didi

sql - 结合两个查询 - 如何?

转载 作者:行者123 更新时间:2023-11-29 12:01:01 25 4
gpt4 key购买 nike

我有以下问题:

查询 1:

Select * from T10,T11,T12,T13,T14 
where T10.C0 = T11.C0 and T11.C1 = T12.C0
and T12.C1 = T13.C0 and T13.C1 = T14.C0;

查询 2:

Select * from T20,T21,T22,T23,T24 
where T20.C0 = T21.C0 and T21.C1 = T22.C0
and T22.C1 = T23.C0 and T23.C1 = T24.C0;

如何组合这 2 个查询来显示这些表的所有值?我希望加入 T10.C1 = T20.C1

当尝试 union 时,我收到一条警告,提示列数不相同,这是真的,这些表不一样


联盟

Select * from 
"ProductConfig","Board","PcbBuild","Model","TcssCalib"
where "Model"."idModel" = "PcbBuild"."Model" and "Board"."PcbBuild" = "PcbBuild"."idPcbBuild"
and "Board"."idBoard" = "TcssCalib"."Board" and "ProductConfig"."TcssCalib" = "TcssCalib"."idTcssCalib"

union

Select * from"ProductBuild","TxResultsLink","TxResults","DspValues" where "ProductBuild"."idProductBuild"
= "TxResultsLink"."ProductBuild" and "TxResults"."idTxResults" = "TxResultsLink"."TxResults"
and "TxResults"."DspValues" = "DspValues"."idDspValues";

这里我想要 ProductBuild.Productconfig 加入 ProductConfig.idProductConfig

报错:

[Err] 错误:每个 UNION 查询必须具有相同的列数


当我尝试 inner join 时,我在 inner 处或附近遇到语法错误

有没有办法将这两个查询连接在一起?

最佳答案

这就是你想要的:

Select * from T10,T11,T12,T13,T14,T20,T21,T22,T23,T24
where T10.C0 = T11.C0 and T11.C1 = T12.C0
and T12.C1 = T13.C0 and T13.C1 = T14.C0
and T10.C1 = T20.C1 and T20.C0 = T21.C0 and T21.C1 = T22.C0
and T22.C1 = T23.C0 and T23.C1 = T24.C0;

另一种更易读的表达方式是这样的:

select * from T10
inner join T11 on T10.C0 = T11.C0
inner join T12 on T11.C1 = T12.C0
inner join T13 on T12.C1 = T13.C0
inner join T14 on T13.C1 = T14.C0
inner join T20 on T10.C1 = T20.C1
inner join T21 on T20.C0 = T21.C0
inner join T22 on T21.C1 = T22.C0
inner join T23 on T22.C1 = T23.C0
inner join T24 on T23.C1 = T24.C0;

要生成 UNION,表中的列数必须相同并且来自相同/可转换的数据类型。

Select * from T10,T11,T12,T13,T14 
where T10.C0 = T11.C0 and T11.C1 = T12.C0
and T12.C1 = T13.C0 and T13.C1 = T14.C0

UNION

Select * from T20,T21,T22,T23,T24
where T20.C0 = T21.C0 and T21.C1 = T22.C0
and T22.C1 = T23.C0 and T23.C1 = T24.C0;

Select * from 
"ProductConfig","Board","PcbBuild","Model","TcssCalib","ProductBuild","TxResultsLink","TxResults","DspValues"
where "Model"."idModel" = "PcbBuild"."Model" and "Board"."PcbBuild" = "PcbBuild"."idPcbBuild"
and "Board"."idBoard" = "TcssCalib"."Board" and "ProductConfig"."TcssCalib" = "TcssCalib"."idTcssCalib" and "ProductBuild"."idProductBuild"
= "TxResultsLink"."ProductBuild" and "TxResults"."idTxResults" = "TxResultsLink"."TxResults"
and "TxResults"."DspValues" = "DspValues"."idDspValues"
and "ProductBuild"."ProductConfig" = "ProductConfig"."idProductConfig";

关于sql - 结合两个查询 - 如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8895659/

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