gpt4 book ai didi

mysql - 查询差异问题

转载 作者:行者123 更新时间:2023-11-29 16:59:33 26 4
gpt4 key购买 nike

我使用此查询来显示带有发票和付款值的 Saldo 列。效果很好,但如果我没有发票但有付款,我可以看到它,直到我添加发票。我该如何纠正它?

select B.razonsocial as Empresa, Facturas , 
IFNULL(pay,0) as Pagos, (Facturas - IFNULL(pay,0)) Saldo
FROM (select TblFacturasCompras.id_proveedor, TblProveedores.razonsocial,
SUM(case when TblFacturasCompras.tipocomprobante = 'A' or TblFacturasCompras.tipocomprobante='B' or TblFacturasCompras.tipocomprobante = 'NDA' or TblFacturasCompras.tipocomprobante = 'NDB' or TblFacturasCompras.tipocomprobante = 'C' or TblFacturasCompras.tipocomprobante = 'NDC' or TblFacturasCompras.tipocomprobante = 'X' then TblFacturasCompras.total else -TblFacturasCompras.total end) as Facturas
from TblFacturasCompras left join TblProveedores on TblFacturasCompras.id_proveedor = TblProveedores.id group by TblProveedores.id) A
LEFT JOIN (select TblProveedores.id, TblProveedores.razonsocial, SUM(TblRecibosCompras.total) as Pay from
TblProveedores left join TblRecibosCompras on TblRecibosCompras.id_proveedor = TblProveedores.id group by TblProveedores.id) B ON A.id_proveedor = B.id
where B.razonsocial <> '' order by B.razonsocial Asc

最佳答案

您正在使用 where 条件中涉及左连接的列

 where B.razonsocial <> '' 

这作为内部联接尝试将条件添加到相关的 on 子句

select B.razonsocial as Empresa
, Facturas
, IFNULL(pay,0) as Pagos
, (Facturas - IFNULL(pay,0)) Saldo
FROM (
select TblFacturasCompras.id_proveedor
, TblProveedores.razonsocial
, SUM(case when
TblFacturasCompras.tipocomprobante = 'A'
or TblFacturasCompras.tipocomprobante='B'
or TblFacturasCompras.tipocomprobante = 'NDA'
or TblFacturasCompras.tipocomprobante = 'NDB'
or TblFacturasCompras.tipocomprobante = 'C'
or TblFacturasCompras.tipocomprobante = 'NDC'
or TblFacturasCompras.tipocomprobante = 'X'
then TblFacturasCompras.total
else -TblFacturasCompras.total end) as Facturas
from TblFacturasCompras
left join TblProveedores on TblFacturasCompras.id_proveedor = TblProveedores.id
group by TblProveedores.id
) A
LEFT JOIN (
select TblProveedores.id
, TblProveedores.razonsocial
, SUM(TblRecibosCompras.total) as Pay
from TblProveedores
left join TblRecibosCompras on TblRecibosCompras.id_proveedor = TblProveedores.id
group by TblProveedores.id
) B ON A.id_proveedor = B.id and B.razonsocial <> ''
order by B.razonsocial Asc

建议您可以使用 IN 子句而不是多个 OR 子句

select B.razonsocial as Empresa
, Facturas
, IFNULL(pay,0) as Pagos
, (Facturas - IFNULL(pay,0)) Saldo
FROM (
select TblFacturasCompras.id_proveedor
, TblProveedores.razonsocial
, SUM(case when
TblFacturasCompras.tipocomprobante IN ('A','B','NDA','NDB','C','NDC','X')
then TblFacturasCompras.total
else -TblFacturasCompras.total end) as Facturas
from TblFacturasCompras
left join TblProveedores on TblFacturasCompras.id_proveedor = TblProveedores.id
group by TblProveedores.id
) A
LEFT JOIN (
select TblProveedores.id
, TblProveedores.razonsocial
, SUM(TblRecibosCompras.total) as Pay
from TblProveedores
left join TblRecibosCompras on TblRecibosCompras.id_proveedor = TblProveedores.id
group by TblProveedores.id
) B ON A.id_proveedor = B.id and B.razonsocial <> ''
order by B.razonsocial Asc

关于mysql - 查询差异问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52371507/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com