그룹별/회원별 조회 권한 관련한 sql 초급질문
본문
안녕하세요. JavaScript, JSP, jQuery, MSSql로 웹 학습중입니다.
대분류DB
| category | |
| code | name |
| IT | IT팀 |
| IN | 인프라팀 |
| SY | 시스템팀 |
소분류DB
| report | |||
| category_code | code | name | auth |
| IT | A | A보고서 | all |
| IN | B | B보고서 | select |
| IN | C | C보고서 | select |
| SY | B | B보고서 | select |
| SY | D | D보고서 | select |
접근권한DB
| auth | |||
| categoty_code | report_code | dept_code | staff_code |
| IN | B | IT01 | |
| IN | C | S001 | |
| SY | B | IT02 | |
| SY | D | S002 |
부서DB
| dept |
| code |
| IT00 |
| IT01 |
| IT02 |
사원DB
| Staff | |
| dept_code | code |
| IT01 | S001 |
| IT01 | S002 |
| IT02 | S003 |
| IT02 | S004 |
위와 같이 다섯개의 테이블이 있습니다.
사원코드 S001로 접근할때 select값
=> [{IT, IT팀, A, A보고서}, {IN, 인프라팀, B, B보고서}, {IN, 인프라팀, C, C보고서}]
사원코드 S002로 접근할때 select값
=> [{IT, IT팀, A, A보고서}, {IN, 인프라팀, B, B보고서}]
사원코드 S003로 접근할때 select값
=> [{IT, IT팀, A, A보고서}, {SY, 시스템팀, B, B보고서}, {SY, 시스템팀, D, D보고서}]
사원코드 S004로 접근할때 select값
=> [{IT, IT팀, A, A보고서}, {SY, 시스템팀, B, B보고서}]
위와같이 가져올수 있는 Select 문을 못짜겠습니다 ㅠㅠ 도와주세요..
답변 1
db modeling이 좀 난해하군요.
select b.category_code, d.name, b.code, b.name from staff a inner join auth b on a.code=b.staff_code
inner join report c on b.category_code=c.categor_code and b.report_code=c.code
inner join category d on b.category_code=d.code
union
select b.category_code, d.name, b.code, b.name from staff a inner join auth b on a.dept_code=b.dept_code
inner join report c on b.category_code=c.categor_code and b.report_code=c.code
inner join category d on b.category_code=d.code