使用以下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
包含{{ 1}}表示2
1。
invoice_no
输出:
select free from purchase_invoice where invoice_no = 1;
此
2
返回一个空值,因为没有添加任何值。
我的问题是,
为什么select free from damage_stocks where invoice_no = 1;
列显示10而不是2,因为free
中的免费列包含purchase_invoice
1的2
如何将invoice_no
col的输出输出为2而不是10?
答案 0 :(得分:1)
因为'免费' 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
我放置了#free;'选择序列末尾的列。