sql - My query join from more than 2 TABLES return double values using same key reference -


i'm trying join more 2 tables using 1 same key reference, works fine when join 2 two tables using left join :

select userprofile.userprofileid, userprofile.name, jobposition.levelname master.eliuserprofile userprofile left join master.elijobposition jobposition on userprofile.userprofileid = jobposition.userprofileid userprofile.userprofileid = '5001'

result :

userprofileid    | name    |  levelname   | -----------------+---------+--------------+ 5001             | dirdple | direktur ple |  5001             | dirdple | direktur ple |  

but when want join table it, return double values, using master table reference (which userprofile) :

select userprofile.userprofileid, userprofile.name, jobposition.levelname, useracmmapping.useracmmappingid, useracmmapping.refid  master.eliuserprofile userprofile  left join master.elijobposition jobposition  on userprofile.userprofileid = jobposition.userprofileid  left join transactions.eliuseracmmapping useracmmapping  on userprofile.userprofileid = useracmmapping.userprofileid  userprofile.userprofileid = '5001' 

and turned out resulted :

userprofileid    | name    | levelname    | useracmmappingid | refid | -----------------+---------+--------------+------------------+-------+ 5001             | dirdple | direktur ple |        1         |   21  | 5001             | dirdple | direktur ple |        7         |   22  | 5001             | dirdple | direktur ple |       158        |   23  | 5001             | dirdple | direktur ple |        1         |   21  |  5001             | dirdple | direktur ple |        7         |   22  | 5001             | dirdple | direktur ple |       158        |   23  | 

is because iam using same key(userprofile.userprofileid)? fyi, 3rd table (useracmmapping) have 3 values userprofileid=5001 , 2nd table (jobposition) have 2 values userprofileid=5001. , table want referenced second table 2 values (userprofileid=5001)..

thank help..

it because joining 2 different tables 1 table same key. joining jobpositions , useracmmapping userprofile. mean there 2 joins on 1 table same field, result doubled.

you should instead join jobpositions userprofile , join useracmmapping jobpositions, this:

select userprofile.userprofileid, userprofile.name, jobposition.levelname, useracmmapping.useracmmappingid, useracmmapping.refid  master.eliuserprofile userprofile  left join master.elijobposition jobposition  on userprofile.userprofileid = jobposition.userprofileid  left join transactions.eliuseracmmapping useracmmapping  on jobposition.userprofileid = useracmmapping.userprofileid  userprofile.userprofileid = '5001' 

here 1 join per table.

note: didn't test code, since don't have actual tables, should work.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -