为什么不能在t-sql联接中绑定我的字段?

s6fujrry  于 2021-07-26  发布在  Java
关注(0)|答案(3)|浏览(272)

所以我有以下sql语句:

SELECT
    CASE
    WHEN table1.Recommendation = 1 THEN 'Condition1'
    WHEN table1.Recommendation = 2 THEN 'Condition2'
    WHEN table1.Recommendation = 3 THEN 'Condition3'
    ELSE ' '
    END
    AS Recommendation,
    table2.Notes,
    table3.ItemDescription AS Item,
    table4.Description AS Concern
FROM Job
RIGHT JOIN table2 ON Job.JobId = table2.JobId
LEFT JOIN table1 ON table2.table2Id = table1.table2Id
INNER JOIN table3 ON table2.table3Id = table3.table3Id
LEFT JOIN table1 customName ON table4.table4Id = customName.table4Id
WHERE Job.JobId = @JobId AND table1.Condition = 3

这是telerik的报告 JobId 变量将与实际作业id一起输入。 table2 保持 JobId ,以及 table1 身份证。我正在试着拿到 table4.Description ,但每次我出错时:

The multi-part identifier table4.table4Id could not be bound. 
The multi-part identifier table4.Description could not be bound.

在这一点上,我不知道从这里尝试什么。任何帮助都将不胜感激!我将很乐意补充更多的信息,尽可能试图找出这个问题

ua4mk5z4

ua4mk5z41#

你没有使用任何别名 table4 ; 因此,编译器会给出错误。

eoigrqb6

eoigrqb62#

基本上你错过了一个 JOIN . 您只需添加联接条件。

SELECT
    CASE
    WHEN table1.Recommendation = 1 THEN 'Condition1'
    WHEN table1.Recommendation = 2 THEN 'Condition2'
    WHEN table1.Recommendation = 3 THEN 'Condition3'
    ELSE ' '
    END
    AS Recommendation,
    table2.Notes,
    table3.ItemDescription AS Item,
    table4.Description AS Concern
FROM Job
RIGHT JOIN table2 ON Job.JobId = table2.JobId
LEFT JOIN table1 ON table2.table2Id = table1.table2Id
INNER JOIN table3 ON table2.table3Id = table3.table3Id
INNER JOIN table4 ON <I don't what what to join on>
LEFT JOIN table1 customName ON table4.table4Id = customName.table4Id
WHERE Job.JobId = @JobId AND table1.Condition = 3
dohp0rv5

dohp0rv53#

在看了前面的答案和对话之后,我认为最好的解决方案是翻转连接。

SELECT
    CASE
    WHEN table1.Recommendation = 1 THEN 'Condition1'
    WHEN table1.Recommendation = 2 THEN 'Condition2'
    WHEN table1.Recommendation = 3 THEN 'Condition3'
    ELSE ' '
    END
    AS Recommendation,
    table2.Notes,
    table3.ItemDescription AS Item,
    table4.Description AS Concern
FROM Job
RIGHT JOIN table2 ON Job.JobId = table2.JobId
LEFT JOIN table1 ON table2.table2Id = table1.table2Id
INNER JOIN table3 ON table2.table3Id = table3.table3Id
RIGHT JOIN table4 ON table1.table4Id = table4.table4Id
WHERE Job.JobId = @JobId AND table1.Condition = 3

相关问题