如何将此左连接添加到我的查询中?

时间:2015-11-06 16:06:52

标签: sql join pervasive

如何添加此...

LEFT JOIN quote_header ON 
    (job_header.job = '0' + LEFT(quote_header.user_5, 5))

对此...

SELECT 
    job_header.job,
    job_header.suffix,
    job_header.customer,
    job_header.description,
    job_header.comments_1,
    job_header.date_due,
    job_header.part,
    job_header.customer_po,
    job_header.date_closed,
    job_header.flag_hold,
    wo_user_flds.user_7,
    wo_user_flds.user_3,
    wo_user_flds.user_6,
    wo_user_flds.user_5,
    wo_user_flds.user_2

FROM job_header

LEFT JOIN wo_user_flds ON 
    (job_header.job = wo_user_flds.job) AND
    (job_header.suffix = wo_user_flds.suffix)

WHERE job_header.date_closed = '000000'

AND LENGTH(job_header.job) > '5';

我尝试在第一次左连接后立即添加它,但它只是崩溃了。

2 个答案:

答案 0 :(得分:0)

也许尝试使用CONCAT代替' 0' +左(......

OR

也许您可以尝试将quote_header放入带有修改字段的临时表中,然后加入到该表中......

SELECT CONCAT('0', LEFT(quote_header.user_5, 5)) as quoteHeaderUser_5, field2, field3 etc etc
INTO #tmpQH
FROM quote_header

那么你可以......

LEFT JOIN #tmpQH ON 
    (job_header.job = quoteHeaderUser_5)

答案 1 :(得分:0)

尝试创建几个视图......

<强>视图1

SELECT 
    ...

FROM job_header
WHERE job_header.date_closed = '000000'
AND LENGTH(job_header.job) > '5';

<强>视图2

SELECT 
    ...,
    '0' + LEFT(quote_header.user_5, 5) as 'FirstFiveOfUser5'
from quote_header 

然后,您可以像这样加入......

SELECT 
    job_header.job,
    job_header.suffix,
    job_header.customer,
    job_header.description,
    job_header.comments_1,
    job_header.date_due,
    job_header.part,
    job_header.customer_po,
    job_header.date_closed,
    job_header.flag_hold,
    wo_user_flds.user_7,
    wo_user_flds.user_3,
    wo_user_flds.user_6,
    wo_user_flds.user_5,
    wo_user_flds.user_2

FROM View1 as job_header

LEFT JOIN wo_user_flds ON 
    (job_header.job = wo_user_flds.job) AND
    (job_header.suffix = wo_user_flds.suffix)
LEFT JOIN View2 as quote_header ON 
    (job_header.job = quote_header.FirstFiveOfUser5)

这可能对性能有所帮助。