sql - How to choose proper table name dynamically from multiple tables for creating join query -


i want create join query dynamically contains following tables:

table 1) usertable: contains userid, userfirstname, userlastname

table 2) grouptable: contains groupid, groupname, groupdescription

table 3) usergroup : contains relation between usertable , grouptable

table 4) grouproles: contains relations between groupid , roleid (any random column)

case1:

if want know in tables groupid present/common, gives result enter image description here

query:

i have 1 form accepts userid. using userid have find: userid, userfirstname, userlastname , groupname.

problem:

the problem while creating dynamic query join clause.

as in case 1 there 2 options of tables: either grouproles or usergroup.

so how come know which table select these 2 options dynamically takes me grouptable via relational table usergroup table in case? in case have find userid, userfirstname, userlastname , groupname.

i can userid, userfirstname , userlastname usertable. groupname column present in grouptable have take usergroup table relation join query.

so how can come know table select either of tablenames (as in case1 ) dynamically, can groupname column query?

i think maybe i've understand question , want following output as:

select  a.userid, a.userfirstname, a.userlastname, b.groupname usertable inner join  grouptable b on a.userid=b.groupid 

usertable:

1| a| b  2| c| d 

grouptable:

1 |x |io 2 |y |oi 

output:

1| a| b|x  2| c| d|y 

update:

try 1 click

a generic database design limits proliferation of new tables number of forms , data elements grows. on other hand simplest query involved 3 tables, in our case.

queries indeed got mind boggling. after 3 years of became of opinion explicit tables better. i'd rather deal more tables better reflect business form/model - can understand looking @ them; vice 1/2 tables multitudes of meaningless join tables requiring astonishing long, nested queries make rudimentary sense out of data. start memorizing meaning/context of individual identity column primary keys!

finally, had evolved had 1 table rule them away of inherent complexities (and bad coding , design decisions of past) in existing design , evolving user requirements. nonetheless, we'd have problems table size went million+ row size.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -