如何用mysql中的join替换子查询

时间:2015-07-07 12:12:33

标签: mysql join

我有以下查询。

select paytype, sum(amtpaid)
from `tbl_payments`
where locationid = 3  and
      checkstatus not in ('Refund','Tip','Tipadj') and
      paytype not in ('Void', 'Comp') and
      oid in (select oid
              from `tbl_checkout_stage`
              where locationid = 3
              group by oid
              having count(checkid) = sum(checkstatus = 'Paid')
             )
group by paytype

如何在上面的情况下用join替换子查询。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

尝试此查询

select paytype, sum(amtpaid)
from `tbl_payments`
join (select oid
              from `tbl_checkout_stage`
              where locationid = 3
              group by oid
              having count(checkid) = sum(checkstatus = 'Paid')
             ) a on a.oid=`tbl_payments`.oid
where locationid = 3  and
      checkstatus not in ('Refund','Tip','Tipadj') and
      paytype not in ('Void', 'Comp') and
group by paytype