GROUP列mysql选择最短名称

时间:2017-12-13 19:14:52

标签: mysql

嗨我有以下格式的表格

ProductName                    CCode        MCode
Samsumg TV LED JPN             MNCC         N11
Samsung TV LED                 MNCC         N11
Samsung TV LED US              MNCC         N11   

Output I am looking for is (Shortest Name)

Samsung TV LED      MNCC       N11 (1 row)

1 个答案:

答案 0 :(得分:1)

select ProductName, CCode, MCode 
from mytable a
join (select CCode, min(length(ProductName)) as min_length
        from mytable
      group by CCode) b on a.CCode=b.CCode
where length(a.product_name) = b.min_length;

这有点尴尬,但它确实有效。我不知道您的搜索条件是否应包含MCode以及您的示例并未说明CCode和MCode因产品而异的情况。