2016-03-16 231 views
-1

刪除重複如何選擇唯一行只有3個:這樣SQL查詢 - 我需要從查詢

63518 | 3 | 338 | 338 - Turks and .... | 0.0245 | 
63519 | 3 | 338 | 338 - Jamica   | 0.0235 | 
63522 | 3 | 338 | 338 - Haiti   | 0.0285 | 


SELECT 
    r.id, 
    r.client_id, 
    mcc.mcc AS mcc, 
    CONCAT(mcc.mcc, " - ", mcc.country_name) AS mcc_text, 
    r.rate, 
    mcc.dial_code, 
    r.active 
FROM rates r 
    INNER JOIN clients c 
    ON c.id = r.client_id 
    INNER JOIN mcc 
    ON mcc.mcc = r.mcc  
WHERE r.client_id = 3 AND r.mcc = 338 AND r.mnc = 50 AND r.active = 1 

enter image description here

我試圖用 .... GROUP BY r.id 但mcc_text是錯誤的 enter image description here

+0

使用組由ID後where子句結束 – Abhishekkumar

+0

@Abhishekkumar,我加入PIC,它不worki(( – AxOn

+0

@ shawnt00,我不明白,如意集團是mcc_text = dial_code = id – AxOn

回答

0

試試這個:

SELECT DISTINCT 
    r.id, 
    r.client_id, 
    mcc.mcc AS mcc, 
    CONCAT(mcc.mcc, " - ", mcc.country_name) AS mcc_text, 
    r.rate, 
    mcc.dial_code, 
    r.active 
FROM rates r 
    INNER JOIN clients c 
    ON c.id = r.client_id 
    INNER JOIN mcc 
    ON mcc.mcc = r.mcc  
WHERE r.client_id = 3 AND r.mcc = 338 AND r.mnc = 50 AND r.active = 1 
+0

結果一樣就可以圖片 – AxOn

+0

使用Distinct不會像ID和ID一樣時速率和撥號碼不同。 – Abhishekkumar

0

嘗試此查詢這可能工作

SELECT DISTINCT 
    r.id, 
    r.client_id, 
    mcc.mcc AS mcc, 
    CONCAT(mcc.mcc, " - ", mcc.country_name) AS mcc_text,  
    (SELECT rate FROM rates WHERE rates.id=r.id)AS rate, 
    mcc.dial_code, 
    r.active 
FROM rates r 
    INNER JOIN clients c 
    ON c.id = r.client_id 
    INNER JOIN mcc 
    ON mcc.mcc = r.mcc  
WHERE r.client_id = 3 AND r.mcc = 338 AND r.mnc = 50 AND r.active = 1 
GROUP BY r.id 
+0

20:46:01注意:1276. SELECT#2的字段或引用'smpp.r.mcc'在SELECT#1中解析 – AxOn

+0

嘗試編輯的一個.. – Abhishekkumar