我无法为该SQL查询使用语法。
我有一个数据库,其中有[多于] 3个表:
TUnit包含字段
TBatch包含字段¨
TProcess包含字段
我正在尝试让SQL为TUnit中的每个记录返回这些字段:
我可以显示
具有以下(有效)代码
SELECT
TUNIT.UNITUNIT,
TUNIT.UNITNEXTUNIT,
TBATCH.BATUNIT,
TBATCH.BATBATCH,
TPROCESS.PROCENDTIME,
TPROCESS.PROCSTARTTIME from TPROCESS
inner join TBATCH on TBATCH.BATBATCH=TPROCESS.PROCBATCH
inner join TUNIT on TUNIT.UNITNEXTUNIT=TBATCH.BATUNIT
where TBATCH.BATTYPE=100
我尝试如下添加另一个ProcStartTime,但失败并显示错误消息“','附近的语法不正确”。 :
SELECT
TUNIT.UNITUNIT,
TUNIT.UNITNEXTUNIT,
TBATCH.BATUNIT,
TBATCH.BATBATCH,
TPROCESS.PROCENDTIME,
TPROCESS.PROCSTARTTIME from TPROCESS
inner join TBATCH on TBATCH.BATBATCH=TPROCESS.PROCBATCH
inner join TUNIT on TUNIT.UNITNEXTUNIT=TBATCH.BATUNIT
where TBATCH.BATTYPE=100,
TPROCESS.PROCSTARTTIME from TPROCESS
inner join TBATCH on TBATCH.BATBATCH=TPROCESS.PROCBATCH
inner join TUNIT on TUNIT.UNITPREVUNIT=TBATCH.BATUNIT
where TBATCH.BATTYPE=100
在这里帮我吗?如何使这两个联接成为同一条语句?
对不起,我显然是数据库新手,所以我不知道表达问题的正确语言...
答案 0 :(得分:2)
您需要以JOIN
开始写TUNIT
,然后JOIN
两次TBATCH
和TPROCESS
来获取每个单元的开始时间:< / p>
SELECT
TUNIT.UNITUNIT,
TUNIT.UNITNEXTUNIT,
TB1.BATUNIT AS NEXTUNIT,
TB1.BATBATCH AS NEXTBATCH,
TP1.PROCENDTIME AS NEXTENDTIME,
TP1.PROCSTARTTIME AS NEXTSTARTTIME,
TUNIT.UNITPREVUNIT,
TB2.BATUNIT AS PREVUNIT,
TB2.BATBATCH AS PREVBATCH,
TP2.PROCENDTIME AS PREVENDTIME,
TP2.PROCSTARTTIME AS PREVSTARTTIME
FROM TUNIT
INNER JOIN TBATCH TB1 on TUNIT.UNITNEXTUNIT=TB1.BATUNIT
INNER JOIN TPROCESS TP1 on TB1.BATBATCH=TP1.PROCBATCH
INNER JOIN TBATCH TB2 on TUNIT.UNITPREVUNIT=TB2.BATUNIT
INNER JOIN TPROCESS TP2 on TB2.BATBATCH=TP2.PROCBATCH
WHERE TB1.BATTYPE=100 AND TB2.BATTYPE = 100
答案 1 :(得分:1)
我认为您正在寻找联盟。
select unitunit, TProcess.ProcStartTime, TProcess.ProcEndTime from TUnit
inner join TBatch on TBatch.BatBatch = TUnit.UnitNextUnit
inner join TProcess on TProcess.ProcBatch = TBatch.BatBatch
union
select unitunit, TProcess.ProcStartTime, TProcess.ProcEndTime from TUnit
inner join TBatch on TBatch.BatBatch = TUnit.UnitLastUnit
inner join TProcess on TProcess.ProcBatch = TBatch.BatBatch
您可能必须添加别名以使列更易读。