DB(Database)/MySQL

[MySQL] ๋‹ค๋Œ€๋‹ค ๊ด€๊ณ„ ํ…Œ์ด๋ธ” SQL ์ฟผ๋ฆฌ๋ฌธ์œผ๋กœ JOIN ํ•˜๊ธฐ

ํƒฑ์ ค 2021. 2. 16. 00:26

์ผ๋Œ€๋‹ค, ๋‹ค๋Œ€๋‹ค ๊ด€๊ณ„

  • 1:๋‹ค ๊ด€๊ณ„
    • ํ•œ์ชฝ ํ…Œ์ด๋ธ”์—๋Š” ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ์กด์žฌํ•˜๊ณ , ๊ทธ ๊ฐ’๋งŒ ๋Œ€์‘๋˜๋Š” ๋‹ค๋ฅธ ์ชฝ ํ…Œ์ด๋ธ”์˜ ๊ฐ’์€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ด€๊ณ„
    • EX) ํ•™์ƒ ํ…Œ์ด๋ธ”๊ณผ ์ ์ˆ˜ ํ…Œ์ด๋ธ”, ์ง์› ํ…Œ์ด๋ธ”๊ณผ ์›”๊ธ‰ ํ…Œ์ด๋ธ”
  • ๋‹ค:๋‹ค ๊ด€๊ณ„
    • ํ•œ ํ•™์ƒ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋™์•„๋ฆฌ์— ๊ฐ€์ž…ํ•  ์ˆ˜ ์žˆ๊ณ , ํ•œ ๋™์•„๋ฆฌ์—๋Š” ์—ฌ๋Ÿฌ ํ•™์ƒ์ด ๊ฐ€์ž…ํ•  ์ˆ˜ ์žˆ์–ด ํ•™์ƒ ํ…Œ์ด๋ธ”๊ณผ ๋™์•„๋ฆฌ ํ…Œ์ด๋ธ”์€ ๋‹ค๋Œ€๋‹ค ๊ด€๊ณ„์ž„.
    • ๋‹ค๋Œ€๋‹ค ๊ด€๊ณ„์˜ ๊ฒฝ์šฐ ์—ฐ๊ฒฐ ํ…Œ์ด๋ธ”๊ณผ ๋‘ ํ…Œ์ด๋ธ”์ด ์ผ๋Œ€๋‹ค ๊ด€๊ณ„๋ฅผ ๋งบ๋„๋ก ๊ตฌ์„ฑ
    • EX) ํ•™์ƒ ํ…Œ์ด๋ธ”, ํ•™์ƒ_๋™์•„๋ฆฌ ํ…Œ์ด๋ธ”(ํ•™์ƒํ…Œ์ด๋ธ”๊ณผ ๋™์•„๋ฆฌํ…Œ์ด๋ธ”์˜ ์—ฐ๊ฒฐ ํ…Œ์ด๋ธ”), ๋™์•„๋ฆฌ ํ…Œ์ด๋ธ”


ํ•™์ƒ ํ…Œ์ด๋ธ”, ํ•™์ƒ_๋™์•„๋ฆฌ ํ…Œ์ด๋ธ”, ๋™์•„๋ฆฌ ํ…Œ์ด๋ธ” ์ƒ์„ฑํ•˜๋Š” ์ฟผ๋ฆฌ๋ฌธ

-- ํ•™์ƒ ํ…Œ์ด๋ธ”
drop table if exists stdtbl;
create table stdtbl(
	stdname varchar(10) not null primary key,
    addr varchar(5) not null
);
desc stdtbl;

-- ๋™์•„๋ฆฌ ํ…Œ์ด๋ธ”
drop table if exists clubtbl;
create table clubtbl(
	clubname varchar(10) not null primary key,
    roomno varchar(5) not null
);
desc clubtbl;

-- stdclubtbl์„ ๋งŒ๋“ค์–ด์ค˜์„œ ํ•™์ƒ, ๋™์•„๋ฆฌ ํ…Œ์ด๋ธ”์—์„œ ์„œ๋กœ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ๊ฐˆ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์–ด์ค€๋‹ค.
drop table if exists stdclubtbl;
create table stdclubtbl(
	num int auto_increment not null primary key,
	stdname varchar(10) not null,
    clubname varchar(10) not null,
    foreign key(stdname) references stdtbl(stdname),
    foreign key(clubname) references clubtbl(clubname)
);
desc stdclubtbl;
-- ๋ฐ์ดํ„ฐ ์‚ฝ์ž…ํ•˜๊ธฐ
insert into stdtbl values ('๊ฐ•ํ˜ธ๋™','๊ฒฝ๋ถ'),('๊น€์ œ๋™','๊ฒฝ๋‚จ'),('๊น€์šฉ๋งŒ','์„œ์šธ'),
('์ดํœ˜์žฌ','๊ฒฝ๊ธฐ'),('๋ฐ•์ˆ˜ํ™','์„œ์šธ');

insert into clubtbl values ('์ˆ˜์˜','101ํ˜ธ'),('๋ฐ”๋‘‘','102ํ˜ธ'),('์ถ•๊ตฌ','103ํ˜ธ'),
('๋ด‰์‚ฌ','104ํ˜ธ');

insert into stdclubtbl values (null, '๊ฐ•ํ˜ธ๋™','๋ฐ”๋‘‘'),(null, '๊ฐ•ํ˜ธ๋™','์ถ•๊ตฌ'),
(null, '๊น€์šฉ๋งŒ','์ถ•๊ตฌ'),(null, '์ดํœ˜์žฌ','์ถ•๊ตฌ'),(null, '์ดํœ˜์žฌ','๋ด‰์‚ฌ'),
(null, '๋ฐ•์ˆ˜ํ™','๋ด‰์‚ฌ');

desc stdtbl ๊ฒฐ๊ณผ
desc clubtbl ๊ฒฐ๊ณผ
desc stdclubtbl ๊ฒฐ๊ณผ

 

์œ„์—์„œ ๋งŒ๋“  3๊ฐœ์˜ ํ…Œ์ด๋ธ”๋กœ ์ด๋ฆ„, ์ง€์—ญ, ๋™์•„๋ฆฌ๋ช…, ๋™์•„๋ฆฌ๋ฐฉ ํ˜ธ์ˆ˜ ์ถœ๋ ฅ

๋Œ€๋ถ€๋ถ„ PK, FK ์ œ์•ฝ ์กฐ๊ฑด์„ ๊ฐ€์ง€๊ณ  ์„ค์ •ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•ด์•ผํ•จ.

-- ํ•™์ƒ๋ช…์„ ๊ธฐ์ค€์œผ๋กœ ์ฟผ๋ฆฌ๋ฌธ ์ž‘์„ฑ
select S.stdname, S.addr, C.clubname, C.roomno
  from stdtbl S
  inner join stdclubtbl SC
  on S.stdname = SC.stdname
  inner join clubtbl C
  on SC.clubname = C.clubname
order by S.stdname;

-- ๋™์•„๋ฆฌ๋ช…์„ ๊ธฐ์ค€์œผ๋กœ ์ฟผ๋ฆฌ๋ฌธ ์ž‘์„ฑ
select C.clubname, C.roomno, S.stdname, S.addr
  from clubtbl C
  inner join stdclubtbl SC
  on C.clubname = SC.clubname
  inner join stdtbl S
  on SC.stdname = S.stdname
order by C.clubname;

ํ•™์ƒ๋ช… ๊ธฐ์ค€ ์ฟผ๋ฆฌ๋ฌธ
๋™์•„๋ฆฌ๋ช… ๊ธฐ์ค€ ์ฟผ๋ฆฌ๋ฌธ


์ฐธ๊ณ ) MySQL ๊ธฐ์ดˆ์—์„œ ์‹ค๋ฌด๊นŒ์ง€ ์™„์ „์ •๋ณต ํ•˜๊ธฐ

728x90