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'
4

2 回答 2

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'
于 2013-01-18T03:13:03.027 回答
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
于 2013-01-18T03:25:15.253 回答