SQL查询 - 我需要从查询中删除重复

时间:2016-03-16 17:24:14

标签: mysql

如何仅选择3行:

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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