basic many-to-many sql select query

后端 未结 9 1911
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-17 03:06

I think this should be easy, but it\'s evading me. I\'ve got a many-to-many relationship between Accounts and Account Groups. An Account can be in zero or more Groups, so I\

9条回答
  •  渐次进展
    2021-01-17 03:46

    SELECT act.acctid AS AcctId, bankName, acctNumber, Balance, 
           NVL(jag.gid, '-') AS GroupID, NVL(gp.groupname, '-') AS GroupName
    FROM accounts act 
         LEFT OUTER JOIN JointAccountGroups jag ON (act.id = jag.aid) 
         LEFT OUTER JOIN AccounGroups gp ON (jag.gid = gp.id)
    

    NVL is a function that means "if first argument is null, return the second argument; otherwise, return first argument". NVL happens to be how Oracle do it -- all DBs have something like that, but it doesn't have a standard name; look up how Access does it.

提交回复
热议问题