我希望将这两个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
发布于 2013-10-15 10:43:30
联合查询应该做到这一点-非常简单,只需确保返回的列数相同(如NULL、pro_unitrs或'‘as 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()放入其中,如果变量为空,则将变量设置为零。
https://stackoverflow.com/questions/19372354
复制相似问题