2013-10-15 163 views
0

我要携起手来这两个MySQL查询字符串的结果列像工人,产品,发布,接收pro_unitrs,pro_totMySQL查询字符串结合起来,结果

首先==

select (select wor_code from chall_rec_master where rec_id = chall_rec_pro.rec_id) as Worker, pro_code, sum(pro_qty) as receive, pro_unitrs, pro_tot from chall_rec_pro group by pro_code 

秒= =

select (select worker_code from challan_master where challan_id = challan_mast_prod.challan_id) as Worker , pro_code, sum(pro_qty) as Issued from challan_mast_prod group by pro_code 

列工人,产品,分,接收pro_unitrs,pro_tot

回答

0

一个联合查询应该做的技巧 - 非常简单,只要确保你有相同数量的列返回(做像pro_unitrs或''作为pro_unitrs你不会有其他查询的信息...' mysqltutorial.org/sql-union-mysql.aspx

select * from 
((
    select 
    (select wor_code from chall_rec_master where rec_id = chall_rec_pro.rec_id) as Worker, 
    pro_code, 
    sum(IFNULL(pro_qty,0)) as receive, 
    '' as Issued, 
    pro_unitrs, 
    pro_tot 
    from chall_rec_pro 
    group by pro_code 
) UNION (
    select (select worker_code from challan_master where challan_id = challan_mast_prod.challan_id) as Worker , 
    pro_code, 
    '' as receive, 
    sum(IFNULL(pro_qty,0)) as Issued, 
    '' as pro_unitrs, 
    '' as pro_tot 
    from challan_mast_prod 
    group by pro_code 
)) as results 

- 编辑 - 更新的查询,包括接收和发布 --edit2--更新的查询不总结空值 - MySQL的sum()将空输出如果它试图求和一个空值,所以把它放在ifnull()里面,如果变量设置为零,如果它为空。

+0

否从您的查询字符串我得到工人,pro_code,接收,pro_unitrs,pro_tot而pro_unitrs和pro_tot只显示空或blob和一个更多的列丢失,即发布? –

+0

列像工人,产品,发布,收到,pro_unitrs,pro_tot –

+0

更新 - 看看手册,看看它是如何工作的 –