问题描述
我想将这两个mysql查询字符串一起加入结果列,例如worker,product,Issued,received,pro_unitrs,pro_tot
首先==
select(select来自chall_rec_master的wor_code,其中rec_id = chall_rec_pro.rec_id)作为Worker,pro_code,sum(pro_qty)作为接收,pro_unitrs,pro_tot来自chall_rec_pro group by pro_code
秒==
select (选择challan_master中的worker_code,其中challan_id = challan_mast_prod.challan_id)作为Worker,pro_code,sum(pro_qty)由challan_mast_prod组发布pro_code
列,如worker,product,Issued,received,pro_unitrs,pro_tot
I want to join these two mysql query string together for result columns like worker, product, Issued, received, pro_unitrs, pro_tot
First ==
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
Second ==
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
columns like worker, product, Issued, received, pro_unitrs, pro_tot
推荐答案
Union
,用于所需的结果集。
但是为此,您需要在两个查询中拥有相同数量的列。在您的情况下,两个查询都应该有 worker,product,Issued,received,pro_unitrs,pro_tot 列。
首次查询:
for your required result set.
But for that you need to have equal number of columns in both the queries. In your case, both queries should have worker, product, Issued, received, pro_unitrs, pro_tot columns.
First query :
select (select wor_code from chall_rec_master where rec_id = chall_rec_pro.rec_id) as Worker,
pro_code,
0 as Issued,
sum(pro_qty) as received,
pro_unitrs,
pro_tot
from chall_rec_pro group by pro_code
第二次查询:
Second query:
select (select worker_code from challan_master where challan_id = challan_mast_prod.challan_id) as Worker ,
pro_code,
sum(pro_qty) as Issued ,
0 as received,
0 as pro_unitrs,
0 as pro_tot
from challan_mast_prod group by pro_code
最终查询:
Final Query:
select (select wor_code from chall_rec_master where rec_id = chall_rec_pro.rec_id) as Worker,
pro_code,
0 as Issued,
sum(pro_qty) as received,
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,
sum(pro_qty) as Issued ,
0 as received,
0 as pro_unitrs,
0 as pro_tot
from challan_mast_prod group by pro_code
请检查一下是否有效。
谢谢,
Lok ..
Please check if this works.
Thanks,
Lok..
这篇关于加入两个mysql查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!