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 27728 게시물 읽기
No. 27728
어려운 JOIN Query 질문입니다.
작성자
김성준
작성일
2008-08-10 09:45ⓒ
2008-08-10 10:11ⓜ
조회수
4,465

JOIN문이 이렇게 어려운지 몰랐습니다.
도와 주시면 감사하겠습니다.

두개의 테이블에서 countrycode에 대응하는 id번호를 찾아 아래의 id 공백에 삽입하려고 합니다.

ATABLE
  id_nation  countrycode  countrycode2   countryname 
   1             AF               AFG                 Afghanistan
   2             AL               ALB                 Albania
   3             DZ               DZA                 Algeria
   4             AD               AND                Andorra


BTABLE
  ipfrom             ipto         id_nation  countrycode 
  33996344         33996351                  GB     
  50331648         69956103                  US     
  69956104         69956111                  BM      

삽질구문 :

1. Select * From ATABLE Left Join BTABLE On ATABLE.countrycode = 'BTABLE.countrycode';
2. UPDATE BTABLE SET id_nation =  (SELECT id_nation FROM ATABLE WHERE ATABLE.countrycode = 'BTABLE.countrycode' LIMIT 1); 
위의 2개 구문이 잘 안되는군요.

이 글에 대한 댓글이 총 4건 있습니다.
UPDATE ATABLE a,BTABLE b SET b.id_nation = a.id_nation WHERE a.contrycode = b.contrycode;

버전이 낮으면 업데이트에서 조인이 되지 않을 수 있습니다.
구자성(next1009)님이 2008-08-11 08:22에 작성한 댓글입니다.

서버 : MySql 5.0.51a, 클라이언트 5.0.51b

phpMyAdmin 2.11.7

모두 최신버전입니다.


구자성님의 Query문에 오류는 없지만 반응이 전혀 없군요.

김성준님이 2008-08-11 09:19에 작성한 댓글입니다. Edit

반응이 없다면

ATABLE의 countrycode에 index가 있는지 확인을 해보세요.

우욱님이 2008-08-11 13:28에 작성한 댓글입니다. Edit

답변 감사합니다.

사실은 현재 홈페이지에 ip country분류기능을 넣어 쓰고 있는데, ip to country 분류목록을 업데이트하고자 합니다만 언급하신 인데스는 아예 설정되어 있지가 않습니다.

궁여지책으로 id_nation이나 countrycode로 인덱스를 만들어봐도 안되는군요.

원래, ATABLE의 id_nation은 auto increment로 부여되어 1-202까지 분류되고, BTABLE에서 ATABLE의 id_nation 번호를 뽑아 국가명을 참조하는 데이터베이스입니다.


현재의 ip분류목록이 너무 낡은 것이라서 미분류 ip가 많아서요.

愚居는 http://free69.hosting.paran.com/modules.php?name=CountryStats&op=YearlyStats&year=2005 이옵니다만 폐쇄했다가 2008년에 다시 리모델링 중입니다요.


CREATE TABLE IF NOT EXISTS `stats_country_index` (

  `id_nation` smallint(6) NOT NULL auto_increment,

  `countrycode` varchar(10) NOT NULL,

  `countrycode2` varchar(10) NOT NULL default '',

  `countryname` varchar(60) NOT NULL default '',

  PRIMARY KEY  (`id_nation`),

  KEY `countrycode` (`countrycode`)

) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

INSERT INTO `stats_country_index` (`id_nation`, `countrycode`, `countrycode2`, `countryname`) VALUES

('1', 'AF', 'AFG', 'Afghanistan\r'),

('2', 'AL', 'ALB', 'Albania\r'),

('3', 'DZ', 'DZA', 'Algeria\r'),

('4', 'AD', 'AND', 'Andorra\r'),

('5', 'AO', 'AGO', 'Angola\r');

CREATE TABLE IF NOT EXISTS `stats_country_ip_index` (

  `ipfrom` varchar(30) NOT NULL default '',

  `ipto` varchar(30) NOT NULL default '',

  `id_nation` varchar(60) NOT NULL,

  `countrycode` varchar(60) NOT NULL default '',

  KEY `id_nation` (`id_nation`(6))

) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `stats_country_ip_index` (`ipfrom`, `ipto`, `id_nation`, `countrycode`) VALUES

(' 33996344  ', ' 33996351  ', '', ' GB  '),

(' 50331648  ', ' 69956103  ', '', ' US  '),

(' 69956104  ', ' 69956111  ', '', ' BM  '),

(' 69956112  ', ' 83886079  ', '', ' US  '),

(' 94585424  ', ' 94585439  ', '', ' SE  ');

김성준님이 2008-08-11 15:34에 작성한 댓글입니다.
이 댓글은 2008-08-13 09:35에 마지막으로 수정되었습니다. Edit
[Top]
No.
제목
작성자
작성일
조회
27731mysql 자료를 csv파일로 만들었는데요. 계속 변환해서 쓰고 싶습니다.
양혁기
2008-08-11
4653
27730my.cnf 파일 설정
이성식
2008-08-11
4212
27729row level lock 질문 입니다.
김지은
2008-08-11
3566
27728어려운 JOIN Query 질문입니다. [4]
김성준
2008-08-10
4465
27727character set, collate 관련문제 같습니다.
장민하
2008-08-09
3992
27726Slow query log 를 남길때 해당 웹파일명을 같이..
구롱
2008-08-07
3625
27725mysql 한글문제 질문입니다.
유일조
2008-08-07
4065
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.020초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다