2012-10-26 101 views
1

我是使用SQL的新用戶。 我做了一個查詢,我正在獲取重複的記錄。以下是我的查詢,然後是輸出。我想這個問題是我設置條件('WHERE ...')的地方。查詢輸出中的重複記錄

任何意見可以理解的輸出

SELECT 
    lmm.authorised_quota.fishery_type_code AS fishery, 
    lmm.authorised_quota.season_end AS season, 
    lmm.authorised_quota.authorised_licence_id AS licen_id_auth, 
    lmm.licences.licence_id AS licence_id_lic, 
    lmm.licences.certificate_number AS certificate_number, 
    lmm.certificates.certificate_number AS certificate_number, 
    lmm.certificates.client_id AS client_id, 
    lmm.clients.client_id AS client_id, 
    lmm.vessel_owners.client_id AS client_id, 
    lmm.vessel_owners.vessel_id AS vessel_id, 
    lmm.vessels.vessel_id AS vessel_id, 
    lmm.vessels.overall_length AS over_length, 
    lmm.vessels.beam_length AS beam_length, 
    lmm.vessels.gross_tonnage AS gross_tonnage, 
    lmm.vessels.port_code AS port_code, 
    lmm.ports.port_code AS port_code, 
    lmm.ports.port_name AS port_name, 
    lmm.ports.state AS state 
    FROM lmm.authorised_quota, 
    lmm.licences, 
    lmm.certificates, 
    lmm.clients, 
    lmm.vessel_owners, 
    lmm.vessels, 
    lmm.ports 
WHERE lmm.authorised_quota.fishery_type_code = 'RLQ' 
    AND lmm.ports.state = 'TAS' 
    AND lmm.authorised_quota.seASon_end = '28/feb/99' 
    AND lmm.authorised_quota.authorised_licence_id = lmm.licences.licence_id 
    AND lmm.licences.certificate_number = lmm.certificates.certificate_number 
    AND lmm.certificates.client_id = lmm.clients.client_id 
    AND lmm.clients.client_id = lmm.vessel_owners.client_id 
    AND lmm.vessel_owners.vessel_id = lmm.vessels.vessel_id 
    AND lmm.vessels.port_code = lmm.ports.port_code 
ORDER BY 
     lmm.clients.client_id, 
     lmm.vessel_owners.vessel_id; 

部分:

CLIENT_ID CLIENT_ID_1 CLIENT_ID_2 VESSEL_ID VESSEL_ID_1 OVER_LENGTH BEAM_LENGTH GROSS_TONNAGE PORT_CODE 

    28   28   28   29   29   17.1   4.88   38.5   6120 
    28   28   28   29   29   17.1   4.88   38.5   6120 
    760   760   760   31   31   13.1   4.36   27.1   6080 
    760   760   760   31   31   13.1   4.36   27.1   6080 
    625   625   625   31   31   13.1   4.36   27.1   6080 
    625   625   625   31   31   13.1   4.36   27.1   6080 

回答

2

我看到完全相同的副本。您可以在此上使用DISTINCT

SELECT DISTINCT 
lmm.authorised_quota.fishery_type_code AS fishery, 
lmm.authorised_quota.season_end AS season, 
lmm.authorised_quota.authorised_licence_id AS licen_id_auth, 
lmm.licences.licence_id AS licence_id_lic, 
....... 
+0

嗨約翰DISTINCT記錄,它的工作。非常感謝,拉斐爾 – Rafael

+0

不客氣! gald聽到:D –

0

鑑於您沒有做任何連接,唯一的解釋是您的表中有重複記錄。

如果您希望在查詢中排除這些內容,請使用SELECT關鍵字。

SELECT DISCTINCT...

+0

感謝米奇,我們的建議工作。乾杯,拉斐爾 – Rafael

0

首先,你應該加入他們共享的屬性,即外鍵的表。

FROM 
lmm.authorised_quota aq 
JOIN lmm.licences l 
ON aq.<attr> = l.<attr> ... 

此外,你應該使用正確的JOIN基於表之間的關係(比如左/右/ FULL)。在沒有JOIN的情況下,所有表的笛卡爾積用於應用會導致重複記錄的標準。

其次,如果上面還有連接(這可能是在你的應用成爲可能)後真的重複的記錄,那麼你應該選擇像

SELECT DISTINCT 
lmm.authorised_quota.fishery_type_code AS fishery, 
... 
+0

非常感謝Vikdor。非常翔實的建議。乾杯,拉斐爾 – Rafael