使用以下select查询组合两个表:
SELECT item_name,batch_no,invoice_no,invoice_date,damaged_qty,qty,unit_vat,unit_discount,unit_price,packing,free FROM
(SELECT item_name,batch_no,invoice_no,invoice_date,damaged_qty,qty,unit_vat,unit_discount,unit_price,packing,free
FROM damage_stocks AS S UNION ALL
SELECT item_name,batch_no,invoice_no,invoice_date,qty,qty,unit_vat,free,unit_discount,unit_price,packing
FROM purchase_invoice AS D) AS alias_table
GROUP BY invoice_no,item_name,batch_no
HAVING COUNT(*)=1 and invoice_no=1
ORDER BY item_name,batch_no,qty;结果产出:
'MM TONE_', '1', '1', '2015-06-24', 10, 10, 0.00, 2.00, 0.00, 20.00, '10'您可以看到,它将free显示为10,但在我的damage_stocks表中,空闲列不包含值,而在purchase_invoice表中,列free包含invoice_no 1的2。
select free from purchase_invoice where invoice_no = 1;产出:
2这
select free from damage_stocks where invoice_no = 1;返回一个空值,因为没有添加任何值。
我的问题是,
free列显示10而不是2,因为purchase_invoice中的空闲列包含invoice_no 1的2?free的输出作为2而不是10?发布于 2015-06-24 07:10:58
因为在purchase_invoice查询中“空闲”列的序列不匹配。查询应该是:
SELECT item_name,batch_no,invoice_no,invoice_date,qty,qty,unit_vat,unit_discount,unit_price,packing,free FROM purchase_invoice AS D我在select序列的末尾放置了“空闲”列。
https://stackoverflow.com/questions/31019804
复制相似问题