Mysql查询字符串组合结果

时间:2013-10-15 02:29:21

标签: mysql sql

我想将这两个mysql查询字符串一起加入结果列,例如worker,product,Issued,received,pro_unitrs,pro_tot

首先==

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

列worker,product,Issued,received,pro_unitrs,pro_tot

1 个答案:

答案 0 :(得分:0)

联合查询应该做的很简单 - 只需确保返回相同数量的列(像pro_unitrs那样执行NULL,或者作为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()将使输出为空,因此如果它为null,则将{{1}}置于其中以将变量设置为零