我試圖得到一個在MySQL 5.0.81中工作的查詢,它將提取尚未支付的發票上的到期金額。MySQL帳戶老化報告
我有一個看起來像這樣三個表:沒有支付基於CLIENT_ID(invoices_client_id) 拉發票(invoices_status = 0):
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 ;
我需要查詢做到以下幾點。
老化應該如下: 不到30天 31 - 60天 61 - 90天 超過90天
大它也需要返回與該下降的數額相關聯的客戶端id進入老齡化羣體。
在組中的美元價值的計算方法如下: 發票產品成本 - 發票貼現 - 已付金額至今
發票產品成本的計算公式爲
SUM((invoices_products_price + invoices_products_profit + invoices_products_shipping)* invoices_products_qty)WHERE 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
有人可以幫我建立這個查詢嗎?
此列命名風格讓我感到噁心。 – cherouvim 2009-12-08 22:26:48
我很樂意幫忙!我應該在哪裏發送我的諮詢服務建議? – JohnFx 2009-12-08 22:32:00
@cherouvim:等到你看到一個3表連接完全合格的字段名稱... – gahooa 2009-12-08 22:34:13