Click here to Skip to main content
15,792,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 14-Oct-13 17:58pm
v2

1 solution

You should try
SQL
Union
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 :

SQL
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:

SQL
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:

SQL
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


Please check if this works.

Thanks,
Lok..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900