我想使用so_dtl表创建发票。我已经为它写了查询,并且它在正常情况下工作正常。但在某些情况下,它会给出错误的结果。
inv_master
id so_num invoice_type
1 1 PROFORMA INVOICE
2 1 SAMPLE INVOICE
inv_details
id inv_id so_num finish_good item_desc qty unit_price
1 1 1 565 WRE Electronics Card 20 5
2 1 1 564 WRE ELECTRONICS 10 5
3 2 1 565 WRE Electronics Card 10 5
4 2 1 564 WRE ELECTRONICS 5 5
so_dtl
id so_num finish_good item_desc qty unit_price
1 1 565 WRE Electronics Card 20 5
2 1 564 WRE ELECTRONICS 10 5
3 2 444 TEST-PRODUCT 10 4
4 3 54 FPGA Based PM 5 6
5 4 52 FPGA Based PWM 5 6
6 5 560 Test Desc 5 5
select
sid.so_num,
sid.finish_good as fgood_id,
sid.item_desc as description,
sid.qty,
sid.unit_price,
sid.qty - (select sum(qty) from inv_details where finish_good=sid.finish_good and so_num=sid.so_num) as final_qty
from
so_dtl sid
inner join inv_master as i
on i.so_num=sid.sales_id
where sid.so_num IN(1,4) and
i.invoice_type!='PROFORMA INVOICE';
CREATE TABLE IF NOT EXISTS `so_dtl`(
`id` int(11) NOT NULL AUTO_INCREMENT,
`so_num` int(4) NOT NULL COMMENT 'Store id of sales order',
`finish_good` int(5) NOT NULL,
`item_desc` varchar(100) NOT NULL,
`qty` int(4) NOT NULL,
`unit_price` decimal(13,2) NOT NULL DEFAULT '0.00',
`total` decimal(13,2) NOT NULL,
`createdon` datetime NOT NULL,
`createdby` int(3) NOT NULL,
`modifiedon` datetime NOT NULL,
`modifiedby` int(3) NOT NULL,
`deleted` int(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ;
CREATE TABLE IF NOT EXISTS `inv_master` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`inv_no` varchar(15) NOT NULL,
`invoice_type` varchar(35) NOT NULL);
CREATE TABLE IF NOT EXISTS `inv_details` (
`inv_id` int(11) NOT NULL AUTO_INCREMENT,
`invoice_id` int(4) NOT NULL,
`so_num` int(3) NOT NULL,
`item_dsc` varchar(100) NOT NULL,
`qty` int(4) NOT NULL,
`unit_price` decimal(13,2) NOT NULL,
`total` decimal(13,2) NOT NULL,
`createdon` datetime NOT NULL,
`createdby` int(3) NOT NULL,
`finish_good` varchar(10) NOT NULL,
`fgood_id` int(10) NOT NULL,
`modifiedon` datetime NOT NULL,
`modifiedby` int(3) NOT NULL,
PRIMARY KEY (`inv_item_id`)
);