2016-08-17 1 views
0

먼저 SQL 쿼리 작성시 초보자라고 말하고 싶습니다. 나는이 오류에 대한 철저한 답을 찾았으며 많은 답변을 얻었지만 아무도 도움이되지 못하거나 해결책을 적용하는 방법을 모르겠다.sql MySQL 오류 (1241) 피연산자는 1 열을 포함해야합니다.

여기에 필자의 도전 과제 인 애플리케이션 테이블이 있습니다. 예를 들어 (dl_number, parent_id, person_id) 고유 한 열과 함께 신청자 레코드를 저장합니다. parent_id는 개별 신청자 기록 레코드의 트랙을 자신의 첫 번째 레코드와 함께 유지하며 각 신청자는 고유 한 dl_number를 갖지만 일부 이유로 dl_number (s)는 고유하지 않으므로 일부 레코드와 함께 레코드를 식별해야합니다. dl_number (s) 변경.

다음은 [SQL 오류 (1241) 피연산자에 1 개의 열이 있어야 함] 오류가 발생하는 SQL 쿼리입니다.

SELECT id,application_id,dl_number,surname,firstname,othername,birth_date,status_id,expiry_date,person_id,COUNT(DISTINCT(dl_number,parent_id,birth_date)) AS NumOccurrences 
FROM tbl_dl_application 
WHERE status_id > 1 
GROUP BY dl_number,parent_id,birth_date 
HAVING NumOccurrences > 1 

는이 문제를 해결하는 방법에 대한 도움이나 더 좋은 방법은이 문제를 해결하기 바랍니다.

Sample table and expected result

+0

는 일부 샘플 테이블 데이터, 및 예상 결과를 추가! – jarlh

+0

2 개의 필드로 그룹화하고 훨씬 더 많이 선택했습니다. – Whencesoever

+1

이 'COUNT (DISTINCT (dl_number, parent_id, birth_date))'의 원인이 될 수 있습니다. –

답변

0

되는 중복 정말 그 방법을 사용하는 기능이 아닙니다. SELECT DISTICT column1, column2 FROM table을 사용하면 고유 행만 가져 오거나 비슷하게 SELECT column, count(DISTINCT anothercolumn) FROM table GROUP BY column을 사용하여 그룹 내에서 고유 행을 가져올 수 있습니다.

내가 이해하는대로 문제 : 테이블에서 중복 된 것을 찾습니다. 중복은이 세 열의 동일한 값인 dl_n‌​umber, parent_idbirth‌​_date으로 정의됩니다.

id이 테이블의 기본 키라고 가정합니다. 그렇지 않은 경우 t2.id <> t.id 조건을 행을 고유하게 식별하는 조건으로 바꿉니다. 당신은 단지 중복 그룹이 무엇인지 알고 싶어하는 경우

이 작동합니다 :

SELECT dl_n‌​umber, parent_id, birth‌​_date, count(*) as NumOccurences -- You can only add aggregation functions here, not another column unless you group by it. 
FROM tbl_dl_application t 
WHERE status_id > 1 -- I don't know what this is but it should do no harm. 
GROUP BY dl_n‌​umber, parent_id, birth‌​_date 
HAVING count(*)>1 

을하지만, 각 중복 행의 세부 사항을 알고 싶다면,이 쿼리는 당신에게 그것을 줄 것이다 :

SELECT * 
FROM tbl_dl_application t 
WHERE 
    status_id > 1 -- I don't know what this is but it should do no harm. 
    AND EXISTS (
     SELECT 1 
     FROM tbl_dl_application t2 
     WHERE 
      t2.dl_number = t.dl_number 
      AND t2.parent_id = t.parent_id 
      AND t2.birth_date = t.birth_date 
      AND t2.id <> t.id 
    ) 
ORDER BY dl_n‌​umber, parent_id, birth‌​_date, id; -- So you have your duplicates nicely next to each other. 

귀하의 목적을 오해 한 경우 더 설명하거나 해결 방법이 불투명한지 묻습니다.

0
**You have to use only one column while use to DISTINCT function. You used this three field dl_number,parent_id,birth_date. Just use 1 filed from these 3. Then query will run.** 

예를 들면.

SELECT id,application_id,dl_number,surname,firstname,othername,birth_date,status_id,expiry_date,person_id,COUNT(DISTINCT(parent_id)) AS NumOccurrences 
FROM tbl_dl_application 
WHERE status_id > 1 
GROUP BY dl_number,parent_id,birth_date 
HAVING NumOccurrences > 1 
+0

사실이 아닙니다. http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_count-distinct를 참조하십시오. – jirka