2013-01-18 147 views
1

我一直在嘗試更改此查詢。我不想在這個查詢中使用'Not In'。任何人都可以幫助我如何將此查詢更改爲左連接查詢?如何將此'不在'查詢更改爲左連接查詢

SELECT t.date,t.ticket,t.weight,t.Count, td.description 
FROM tblticket t inner join tblticketdetails td on t.ticket = td.ticket 
WHERE t.ticket NOT IN (SELECT r.Original_ticket from tblRenew R where isnull(r.new_ticket,'') = '' and r.transaction_status = 'valid') 
AND RTRIM(LTRIM(t.status)) = 'OPEN' 
AND td.Type = 62 
AND t.weight/t.Count >= 1000 
AND t.date BETWEEN '2011-12-31' AND '2013-01-17' 

回答

1

提供您不要有多個記錄tblRenew任何門票:

select 
    t.date, t.ticket, t.weight, t.Count, td.description 
from 
    tblticket t 
    inner join tblticketdetails td on t.ticket = td.ticket 
    left join tblRenew R on isnull(r.new_ticket,'') = '' and r.transaction_status = 'valid' and r.Original_ticket = t.ticket 
where 
    r.Original_ticket is not null 
    and RTRIM(LTRIM(t.status)) = 'OPEN' 
    and td.Type = 62 
    and t.weight/t.Count >= 1000 
    and t.date BETWEEN '2011-12-31' AND '2013-01-17' 
1
SELECT t.date, t.ticket,t.weight, t.Count, td.description 
FROM tblticket t inner join 
    tblticketdetails td 
    on t.ticket = td.ticket left outer join 
    (SELECT r.Original_ticket 
     from tblRenew R 
     where isnull(r.new_ticket,'') = '' and 
     r.transaction_status = 'valid' 
    ) v 
    on t.ticket = v.Original_ticket 
WHERE t.ticket NOT IN (SELECT r.Original_ticket from tblRenew R where isnull(r.new_ticket,'') = '' and r.transaction_status = 'valid') 
AND RTRIM(LTRIM(t.status)) = 'OPEN' 
AND td.Type = 62 
AND t.weight/t.Count >= 1000 
AND t.date BETWEEN '2011-12-31' AND '2013-01-17' 
and v.original_tiket is null