2012-04-20 3 views
2

저는 취업 면접에서 질문을 받으면 SQL에 대해 조금 놀고 있습니다. 내 친구는 최근에 인터뷰에서 다음 질문을했고 그는 그것을 얻을 수 없었고 나는 SQL을 알면서도 잘 모르는 직장에서 누군가에게 물었다. 너희들이 나를 위해이 문제에 답하고 어떻게 작동하는지 설명 할 수 있겠습니까? 부디? 표준화 된 열을 별도의 레코드로 선택 하시겠습니까?

* 문제

*

데이터베이스 정규화 (또는 정상화의 부족)는 종종 개발자를위한 도전을 제시한다. 하나 이상의 쉼표로 구분 @ rockauto.com 이메일 주소에있을 수 있습니다, 고유 직원 ID로 식별

EmployeeID 
EmployeeName 
EmailAddresses 

모든 직원 :

세 개의 필드를 포함 직원의 데이터베이스 테이블을 고려 EmailAddresses 필드.

데이터베이스 테이블 아래에 정의됩니다

INSERT INTO Employees (EmployeeID, EmployeeName, EmailAddresses) VALUES 
('1', 'Bill', '[email protected]'), 
('2', 'Fred', '[email protected],[email protected]'), 
('3', 'Fred', '[email protected]'), 
('4', 'Joe', '[email protected],[email protected]'); 

당신의 작업은 다음과 같이 표시됩니다 하나의 MySQL의 SELECT 쿼리를 작성하는 것입니다 : 테스트 목적으로

CREATE TABLE Employees 
(
    EmployeeID int UNSIGNED NOT NULL PRIMARY KEY, 
    EmployeeName varchar(50) NOT NULL, 
    EmailAddresses varchar(40) NOT NULL , 
    PRIMARY KEY(EmployeeID) 
); 

, 여기에 몇 가지 샘플 데이터입니다 위의 샘플 데이터에 대한 출력 :

Employee EmailAddress 
Bill [email protected] 
Fred (2) [email protected] 
Fred (2) [email protected] 
Fred (3) [email protected] 
Joe  [email protected] 
Joe  [email protected] 

같은 이름을 가진 사람이 두 명 이상 (이 경우 "Fred")이므로 EmployeeID는 괄호 안에 포함됩니다.

귀하의 검색어는 MySQL 버전 5.1.41 호환 구문으로 작성해야합니다. ORDER BY EmployeeID ASC "

이 문제의 경우 단일 SQL SELECT 쿼리를 제출해야합니다. 귀하의 질의는 합리적인 시간 내에 1000 개의 레코드 테이블을 처리 할 수 ​​있어야합니다.

+9

제 의견으로는 ... 이것은 끔찍한 인터뷰 질문입니다. – bobwienholt

+0

우리는 PHP를 사용하여 select 문을 구문 분석 할 수 있습니까? 아니면 해당 형식으로 직접 쿼리를 가져와야합니까? – squarephoenix

+2

이 데이터베이스 구조를 정규화하는 방법이 더 좋은 질문이 될 것입니다. ^^ –

답변

1

이메일이 10000 개 미만인 경우에만 허용됩니다.

select 
     if(t1.c > 1, concat(e.employeename, ' (', e.employeeid, ')'), e.employeename) as Employee, 
     replace(substring(substring_index(e.EmailAddresses, ',', n.row), length(substring_index(e.EmailAddresses, ',', n.row - 1)) + 1), ',', '') EmailAddress 
from 
     (select employeename, count(*) as c from Employees group by employeename) as t1, 
     (select EmployeeID, length(EmailAddresses) - length(replace(EmailAddresses,',','')) + 1 as emails from Employees) as t2, 
     (SELECT @row := @row + 1 as row FROM 
     (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) x, 
     (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) x2, 
     (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) x3, 
     (select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) x4, 
     (SELECT @row:=0) as ff) as n, 
     Employees e 
where 
     e.employeename = t1.employeename and 
     e.employeeid = t2.employeeid and 
     n.row <= t2.emails 
order by e.employeeid; 

편집 : 덜 쓸모없는 숫자와

생성 :

select 
     if(t1.c > 1, concat(e.EmployeeName, ' (', e.EmployeeID, ')'), e.EmployeeName) as Employee, 
     replace(substring(substring_index(e.EmailAddresses, ',', n.row), length(substring_index(e.EmailAddresses, ',', n.row - 1)) + 1), ',', '') as EmailAddress 
from 
     (select EmployeeName, count(*) as c from Employees group by EmployeeName) as t1, 
     (select EmployeeID, length(EmailAddresses) - length(replace(EmailAddresses,',','')) + 1 as emails from Employees) as t2, 
     (select `1` as row from (select 1 union all select 2 union all select 3 union all select 4) x) as n, 
     Employees e 
where 
     e.EmployeeName = t1.EmployeeName and 
     e.EmployeeID = t2.EmployeeID and 
     n.row <= t2.emails 
order by e.EmployeeID; 

그리고 우리는 무엇을 배웠는가? 불쌍한 데이터베이스 디자인 결과가 끔찍한 쿼리. 그리고 당신은 사람들이 가난한 데이터베이스 디자인을하기 때문에 아마도 지원되는 SQL을 가지고 할 수 있습니다. :)

+0

이메일 필드는 40 문자 길이이므로 거기에 10 이벤트가 맞지 않을 것입니다. –

+0

좋은 지적 ... 숫자 생성은 상당히 단순화 될 수 있습니다. 분명히 MySQL에서 행의 매개 변수화 된 수를 생성하는 방법이 없습니다. – Toni

관련 문제