database.sarang.net
UserID
Passwd
Database
DBMS
ㆍMySQL
PostgreSQL
Firebird
Oracle
Informix
Sybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
MySQL Q&A 27614 게시물 읽기
No. 27614
Mysql Out Join 질문입니다.
작성자
최원일(cwi7942)
작성일
2008-06-20 14:54ⓒ
2008-06-20 14:58ⓜ
조회수
3,818

안녕하세요??  Mysql 을 사용하는 중 Join 에 대해 궁금한 점이 있어서 글을 남깁니다.

song_temp   -> 자료수 ( 1023323 개 )
album          -> 자료수 ( 76237 개 )
album_detail -> 자료수 ( 76237 개 )
code            -> 자료수 ( 171 개 )

현재 위의 4개의 테이블을 OUT JOIN  하려 합니다.
기준은 song_temp 테이블의 수량 만큼 나와야 하는 것이구요..

테이블 스키마를 적자면... 테이블 4개 구용...

참고로 
code 테이블 code 필드와 album 테이블 genre_code 와 조인 되구요..
album 테이블 album_uid 와 song_temp 테이블  album_uid와 조인 되구요.
album 테이블 album_uid 와 album_detail 테이블 album_uid 와 조인이 됩니다.

조인되어서 결과 갯수가 song_temp 수량과 같게 나와야 하는데... 
하단에 쿼리 한번 보시구요...
지도편달(?) 부탁드립니다....



TABLE album

 album_uid
 album_img_yn
 title1
 title2
 title_search
 title_key
 genre_codee
 album_type1
 album_type2
 nationality_code
 volume
 barcode
 pid(FK)
 oid
 recommend
 UPC
 img_chk


TABLE album_detail
 
 album_uid(FK)
 regist_date
 modify_date
 release_date
 album_profile
 album_etc
 runtime
 management_code


TABLE song_temp

 song_uid(FK)
 album_uid(FK)
 aod_uid(FK)
 artlist_uid(FK)
 cd_num
 seq_num
 titlesong
 hitsong
 regist_date
 nationality_code
 genre2_code
 genre3_code
 genre1_code
 theme_code
 title
 title2
 title_search
 title_key
 artist_name
 artist_name_search
 artist_name_key
 ISRC


TABLE code
 code
 gubun
 value
 etc



쿼리는 아래와 같이 했습니다.

select count(*) from song_temp  ,album ,album_detail ,code
 
 LEFT OUTER JOIN album AB
 ON song_temp.album_uid = AB.album_uid

 LEFT OUTER JOIN album_detail AD
 ON song_temp.album_uid = AD.album_uid

 LEFT OUTER JOIN code CD
 ON album_detail.album_etc = CD.etc

 where song_temp.album_uid = album.album_uid
   and album.album_uid = album_detail.album_uid
   and code.code = album.genre_code;

이 글에 대한 댓글이 총 3건 있습니다.

일단 한단의 select 쿼리 작성이 잘못 된 것 같고요,


count 쿼리의 경우 left join 은 왼쪽 테이블의 조건에 맞는 모든 로우를 참조하므로 join이 무의미 합니다.


설명드리면 다음의 쿼리에서...

1) select count(*) from tbl_1 left join tbl_2 on tbl_1.no = tbl_2.no where tbl_1.no < 1000

2) select count(*) from tbl_1 where no < 1000


1번과 2번의 결과는 같습니다.


원하고자 하는 데이터에 대한 정의가 필요할 것 같습니다.

trinity님이 2008-06-23 11:41에 작성한 댓글입니다. Edit
select count(*)
from song_temp
LEFT OUTER JOIN album AB ON song_temp.album_uid = AB.album_uid
LEFT OUTER JOIN album_detail AD ON song_temp.album_uid = AD.album_uid
LEFT OUTER JOIN code CD ON album_detail.album_etc = CD.etc
;

면 원하시는 대로 갯수는 맞지 않겠나 생각이 듭니다. *^^*


위에 댓글 trinity님께서 말씀해 주신 부분은 특수한 경우에만 해당이 되지 않나 싶습니다.

CREATE TABLE tbl_1( no int , col1 int );
CREATE TABLE tbl_2( no int , title char(1) );

INSERT INTO tbl_1( no, col1 ) VALUES ( 1, 1 );
INSERT INTO tbl_1( no, col1 ) VALUES ( 2, 10 );
INSERT INTO tbl_2( no, title ) VALUES ( 1, 'a' );
INSERT INTO tbl_2( no, title ) VALUES ( 2, 'b' );
INSERT INTO tbl_2( no, title ) VALUES ( 1, 'c' );
INSERT INTO tbl_2( no, title ) VALUES ( 2, 'd' );

로 data를 생성하신 후에 말씀해 주셨던 

1) select count(*) from tbl_1 left join tbl_2 on tbl_1.no = tbl_2.no where tbl_1.no < 1000

2) select count(*) from tbl_1 where no < 1000

를 각각 실행해 보시면 두 쿼리 문의 결과가 서로 다른 결과를 얻게 되시는 것을 보실 수 있으십니다.

우욱님이 2008-06-24 13:00에 작성한 댓글입니다. Edit

아.. 제가 착각 했네요..


그래도 우욱님이 답변 잘 달아주셨군요. ㅎ


다행입니다.

trinity님이 2008-06-30 12:05에 작성한 댓글입니다. Edit
[Top]
No.
제목
작성자
작성일
조회
27617디비업데이트와 추가시 소요시간의 차이? [1]
김수정
2008-06-23
3485
27616테이블3개 조인시 키를 그룹으로 묶으면서 카운트구하는 쿼리 도와주세요. [3]
김명현
2008-06-22
7843
27615insert 후 select만 일어날때 어떤 DBMS가 가장 빠를까요? [2]
DB
2008-06-21
3587
27614Mysql Out Join 질문입니다. [3]
최원일
2008-06-20
3818
27613오류 메시지 해석 좀 부탁드려요... [1]
조진희
2008-06-19
3608
27611[질문] 기간검색 쿼리 문의 [3]
새벽소리
2008-06-19
4140
27610MySQL Cluster NDB Master의 전이에 관한 질문입니다.
Gloridea
2008-06-19
3226
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.017초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다