好吧,看看下面的代码片段。正如 Morten Sickel 所说,我们试图为您提供一些帮助。顺便说一句,您NIF
指的是 numéro d'identification 财务吗?不应该varchar
吗?我已经相应地创建了,请输入您需要的正确详细信息和类型。
参考:
在上面的示例中,我根据表单的下拉选择放置了您提到的多个简单查询。例如,基于客户的已付发票、基于客户的未付发票、计算每个客户的所有已付、未付的每个客户、显示每个客户的所有发票(已付、未付)等...
MySQL 查询:
//-- show todos facturas/cliente
SELECT c.*,f.*
FROM clientes c, facturas f
WHERE c.ref_cliente = f.ref_cliente
ORDER BY c.ref_cliente
;
//-- show pagado(paid) facturas/cliente
SELECT c.*,f.*
FROM clientes c, facturas f
WHERE c.ref_cliente = f.ref_cliente
AND f.estado = 'Paid'
ORDER BY c.ref_cliente
;
//-- show pte(open/unpaid) facturas/cliente
SELECT c.*,f.*
FROM clientes c, facturas f
WHERE c.ref_cliente = f.ref_cliente
AND f.estado = 'NotPaid'
ORDER BY c.ref_cliente
;
//-- count number of paid, unpaid facturas/cliente
SELECT c.*,
COUNT(CASE WHEN f.estado = 'Paid'
THEN f.ref_cliente END) AS PAID,
COUNT(CASE WHEN f.estado = 'NotPaid'
THEN f.ref_cliente END) AS NOTPAID
FROM clientes c, facturas f
WHERE c.ref_cliente=f.ref_cliente
GROUP BY c.ref_cliente
;
//-- with a group concat to show paid/open facturas/cliente
SELECT c.*,
GROUP_CONCAT(CASE WHEN f.estado = 'Paid'
THEN f.ref_factura END) AS PAID,
GROUP_CONCAT(CASE WHEN f.estado = 'NotPaid'
THEN f.ref_factura END) AS UNPAID
FROM clientes c, facturas f
WHERE c.ref_cliente=f.ref_cliente
GROUP BY f.ref_cliente
;
结果:对于上面的每个查询。
REF_CLIENTE NOMBRE NIF REF_FACTURA ESTADO
1 428 57432844Z 1001A Paid
1 428 57432844Z 1005D NotPaid
2 418 67832814K 1002B NotPaid
2 418 67832814K 1001B NotPaid
2 418 67832814K 1002A Paid
3 438 87932854F 1001C Paid
4 448 97232834S 1003A Paid
REF_CLIENTE NOMBRE NIF REF_FACTURA ESTADO
1 428 57432844Z 1001A Paid
2 418 67832814K 1002A Paid
3 438 87932854F 1001C Paid
4 448 97232834S 1003A Paid
REF_CLIENTE NOMBRE NIF REF_FACTURA ESTADO
1 428 57432844Z 1005D NotPaid
2 418 67832814K 1002B NotPaid
2 418 67832814K 1001B NotPaid
//-- count number of paid, unpaid facturas/cliente
REF_CLIENTE NOMBRE NIF PAID NOTPAID
1 428 57432844Z 1 1
2 418 67832814K 1 2
3 438 87932854F 1 0
4 448 97232834S 1 0
//-- with a group concat to show paid/open facturas/cliente
REF_CLIENTE NOMBRE NIF PAID UNPAID
1 428 57432844Z 1001A 1005D
2 418 67832814K 1002A 1002B,1001B
3 438 87932854F 1001C (null)
4 448 97232834S 1003A (null)
PS:对不起,我不是特别使用PHP
,因此您需要将查询放入正确的PHP
语法中。诸如estado
和之类的参数ref_cliente
可能是您从表单的下拉/文本字段中传递的,以通过 sql 触发数据库。MortenSickel似乎可以帮助您:P