0

我想将这两个 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

4

1 回答 1

0

联合查询应该可以解决问题 - 非常简单,只需确保返回的列数相同(执行 NULL 作为 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

--edit-- 更新查询以包括接收和发出 --edit2-- 更新查询以不求和空值 -sum()如果 MySQL 尝试对空值求和,则将输出为空,因此将其放入ifnull()其中以将变量设置为零如果它为空。

于 2013-10-15T02:43:29.747 回答