组合两个mysql查询返回ok而不是行

goucqfw6  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(280)

我有一个查询,在其中我返回了一些有关发票的信息,我把该发票和另一个表“payment”进行比较,看看该发票(fullamount-fullpayment)是否存在于另一个表中,如果它确实存在,某些函数不应该在我的后端代码中运行。

SELECT  a.status, a.rf_reference, a.payor_orig_id , a.gross_amount + a.type_related_amount as fullamount,
                    a.gross_paid + a.type_related_paid as fullpaid
            FROM    invoice a
            where   a.type = 3 and
                    a.status in (10, 30) and
                    a.UPDATE_DT is null
            having  fullamount > fullpaid
            order   by a.ORIG_ID;

上面的查询返回

status| rf_reference | payor_orig_id | fullamount | fullpaid
30        RF123456        212            1000         200

因此,现在我获取上述信息并将其传递到另一个查询,以查看行字段是否匹配。
我就这样传下去

select * 
from    payment 
where 
       payor_orig_id = 212 and
       rf_reference = RF123456 and
       payment_amount = (1000-200) and
       status = 10 and 
      INSERT_DT BETWEEN DATE_SUB(NOW(), INTERVAL 185 DAY) AND NOW() and
     UPDATE_DT IS NULL;

所以现在上面的代码将返回一行,基本上我不运行我的后端函数。
因为这是两个独立的查询,所以我想将它们合并到一个查询中,确保添加一个 having 语句,并检查是否仅返回发票和付款表之间不匹配的行。

SELECT  a.status, a.rf_reference, a.payor_orig_id , a.gross_amount + a.type_related_amount as fullamount,
                a.gross_paid + a.type_related_paid as fullpaid,
                (select b.payment_amount 
                from    payment  b
                where 
                b.payor_orig_id = a.payor_orig_id and
                b.rf_reference = a.rf_reference and
                b.status = 10 and 
                b.INSERT_DT BETWEEN DATE_SUB(NOW(), INTERVAL 185 DAY) AND NOW() and
                b.UPDATE_DT IS NULL) as payment_amount
                FROM    invoice a
                where   a.type = 3 and
                        a.status in (10, 30) and
                        a.UPDATE_DT is null
                having  fullamount > fullpaid and 
                        (fullamount - fullpaid ) <> payment_amount
                order   by a.ORIG_ID;

上面的查询返回“ok”,这是奇怪的,因为我不知道如何调试它。

ctehm74n

ctehm74n1#

尝试查看另一个表是否存在或未使用 NOT EXIST ```
SELECT a.* ,
a.gross_amount + a.type_related_amount as fullamount,
a.gross_paid + a.type_related_paid as fullpaid
FROM invoice a
where a.type = 3 and
a.status in (10, 30) and
a.UPDATE_DT is null and
NOT EXISTS ( select *
from payment
where
payor_orig_id = a.payor_orig_id and
rf_reference = a.rf_reference and
payment_amount = ((a.gross_amount + a.type_related_amount) - (a.gross_paid + a.type_related_paid)) and
status = 10 and
INSERT_DT BETWEEN DATE_SUB(NOW(), INTERVAL 185 DAY) AND NOW() and
UPDATE_DT IS NULL )
having fullamount > fullpaid
order by a.ORIG_ID;

相关问题