我正在尝试在MySQL 5.0.81中运行一个查询,该查询将从尚未支付的发票中扣除应付金额。
我有三张表看起来像这样:
CREATE TABLE `CLMS_invoices` (
`invoices_id` mediumint(8) NOT NULL auto_increment,
`invoices_client_id` mediumint(8) NOT NULL default '0',
`invoices_datetimestamp` varchar(50) collate latin1_german2_ci NOT NULL default '',
`invoices_description` varchar(100) collate latin1_german2_ci NOT NULL default '',
`invoices_discount` decimal(12,2) NOT NULL default '0.00',
`invoices_note` text collate latin1_german2_ci NOT NULL,
`invoices_status` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`invoices_id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1 COLLATE=latin1_german2_ci AUTO_INCREMENT=5 ;
CREATE TABLE `CLMS_invoices_payments` (
`invoices_payments_id` mediumint(8) NOT NULL auto_increment,
`invoices_payments_invoice_id` mediumint(8) NOT NULL default '0',
`invoices_payments_datetimestamp` varchar(50) collate latin1_german2_ci NOT NULL,
`invoices_payments_type` mediumint(8) NOT NULL default '0',
`invoices_payments_paid` decimal(12,2) NOT NULL default '0.00',
PRIMARY KEY (`invoices_payments_id`)
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=latin1 COLLATE=latin1_german2_ci AUTO_INCREMENT=10 ;
CREATE TABLE `CLMS_invoices_products` (
`invoices_products_id` mediumint(8) NOT NULL auto_increment,
`invoices_products_invoice_id` mediumint(8) NOT NULL default '0',
`invoices_products_name` varchar(50) collate latin1_german2_ci NOT NULL default '',
`invoices_products_price` decimal(12,2) NOT NULL default '0.00',
`invoices_products_profit` decimal(12,2) NOT NULL default '0.00',
`invoices_products_qty` mediumint(8) NOT NULL default '0',
`invoices_products_shipping` decimal(12,2) NOT NULL default '0.00',
PRIMARY KEY (`invoices_products_id`)
) ENGINE=MyISAM AUTO_INCREMENT=12 DEFAULT CHARSET=latin1 COLLATE=latin1_german2_ci AUTO_INCREMENT=12 ;
我需要查询来执行以下操作: 根据未支付的client_id(invoices_client_id)提取发票(invoices_status = 0)。
老化应如下: 不到30天 31 - 60天 61 - 90天 超过90天
还需要返回与落入老化组的金额相关联的客户端ID。
组中的美元价值应按如下方式计算: 发票产品成本 - 发票折扣 - 到目前为止支付的金额
发票产品成本计算为
SUM((invoices_products_price + invoices_products_profit + invoices_products_shipping)* invoices_products_qty)在哪里 invoices_products_invoice_id = invoices_id
发票折扣存储在invoices_discount
中到目前为止支付的金额计算为
SUM(invoices_payments_paid)WHERE invoices_payments_invoice_id = invoices_id
最终结果应如下所示:
client_id pastDue1 pastDue2 pastDue3 pastDue4
1 12.00 0.00 0.00 15.00
5 2.00 60.00 80.00 32.00
etc
有人可以帮我构建此查询吗?
答案 0 :(得分:0)
如果我理解正确 - 这里的关键问题是将所有用户分成不同的年龄组。
这可以通过MySQL中的IF(condition, then, else)
语句来完成,如下所示:
将第一个查询放入临时表 -
CREATE TABLE `tmeporary_invoices`
SELECT invoices.`invoices_client_id`,
SUM((products.`invoices_products_price` + products.`invoices_products_profit` + products.`invoices_products_shipping`) * products.`invoices_products_qty`) AS invoice_product_cost,
SUM(payments.`invoices_payments_paid`) AS amount_paid_so_far,
IF(DATEDIFF(NOW(), invoices.`invoices_datetimestamp`) <= 30 , 'pastDue1', IF(DATEDIFF(NOW(),invoices.`invoices_datetimestamp`) <= 60, 'pastDue2', 'pastDue2' )) AS age
FROM `CLMS_invoices` invoices
LEFT JOIN `CLMS_invoices_payments` payments
ON payments.`invoices_payments_invoice_id` = invoices.`invoices_id`
LEFT JOIN `CLMS_invoices_products` products
ON products.`invoices_products_invoice_id` = invoices.`invoices_id`
WHERE invoices.`invoices_status = 0
然后从上面的临时表中选择如下 -
SELECT `invoices_client_id`,
SUM(`invoice_product_cost`) AS invoice_product_cost,
SUM(`amount_paid_so_far`) AS amount_paid_so_far,
SUM(IF(`age` = 'pastDue1',1,0) AS `pastDue1`,
SUM(IF(`age` = 'pastDue2',1,0) AS `pastDue2`,
SUM(IF(`age` = 'pastDue3',1,0) AS `pastDue3`,
FROM `tmeporary_invoices`
GROUP BY `invoices_client_id`
查询应该作为如何完成所需结果的一般准则(简单地复制/粘贴它们可能会导致命名约定/语法中的一些错误,因为我现在无法测试它们。
祝你好运!