Mysql:在组中选择唯一值?

时间:2012-07-31 18:30:59

标签: mysql sql group-by distinct

共有3列 - 类别,材料和品牌。说实话,它不是来自单个表的列,而是来自另外8个使用连接的表的查询结果;)

    |   category  |   material   | brand          |
    ----------------------------------------------|
    |  engines    |  product 1   | abb            | -> unique for category "engines"
    |  engines    |  product 2   | wika           | -> unique for category "engines"
    |  engines    |  product 3   | allen-bradley  | -> unique for category "engines"
    |  engines    |  product 5   | wika           |
    |  engines    |  product 6   | e+h            | -> unique for category "engines"
    |  drives     |  product 7   | abb            | -> unique for category "drives"
    |  drives     |  product 8   | wika           | -> unique for category "drives"
    |  drives     |  product 9   | allen-bradley  | -> unique for category "drives"
    |  drives     |  product 10  | e+h            | -> unique for category "drives"
    |  drives     |  product 11  | e+h            | 

结果我需要这样的smt:

    |   category  |   material   | brand          | concat(category, brand) |
    ----------------------------------------------|-------------------------|
    |  engines    |  product *   | abb            | engines/abb             |
    |  engines    |  product *   | wika           | engines/wika            |
    |  engines    |  product *   | allen-bradley  | engines/allen-brandley  |
    |  engines    |  product *   | e+h            | engines/e+h             |
    |  drives     |  product *   | abb            | drives/abb              |
    |  drives     |  product *   | wika           | drives/wika             |  
    |  drives     |  product *   | allen-bradley  | drives/allen-bradley    |
    |  drives     |  product *   | e+h            | drives/e+h              |

如果我使用“group by”语句(GROUP BY CONCAT()),每300个结果的查询时间超过10秒,并不会让我满意。

任何人都知道如何在群组中获取唯一值?

UPD:

SELECT *, CONCAT(url_alias.alias, '/', LOWER(brand_alias.field_brand_path_value)) as real_url 
FROM `taxonomy_term_hierarchy` as th0
LEFT JOIN `taxonomy_term_hierarchy` as th1 ON th0.tid = th1.parent
LEFT JOIN `taxonomy_term_hierarchy` as th2 ON th1.tid = th2.parent
LEFT JOIN `taxonomy_term_hierarchy` as th3 ON th2.tid = th3.parent
LEFT JOIN `taxonomy_term_hierarchy` as th4 ON th3.tid = th4.parent

LEFT JOIN field_data_field_cat_reference as cat_reference ON    
cat_reference.field_cat_reference_tid IN (th0.tid, th1.tid, th2.tid, th3.tid, th4.tid)
LEFT JOIN node n ON cat_reference.entity_id = n.nid
LEFT JOIN field_data_field_brand_reference as brand_reference ON n.nid = brand_reference.entity_id
LEFT JOIN taxonomy_term_data as td_brand ON brand_reference.field_brand_reference_tid = td_brand.tid
LEFT JOIN field_data_field_brand_path as brand_alias ON td_brand.tid = brand_alias.entity_id
LEFT JOIN url_alias ON CONCAT('taxonomy/term/', th0.tid) = url_alias.source
WHERE 1
GROUP BY CONCAT(url_alias.alias, '/', LOWER(brand_alias.field_brand_path_value))

2 个答案:

答案 0 :(得分:0)

尝试:

SELECT   a.category, 
         CONCAT(SUBSTRING_INDEX(a.material, ' ', 1), ' *') AS material,
         brand,
         CONCAT(a.category, '/', a.brand) AS concattedval
FROM     (
             [Your Sub-Select Query]
         ) a
GROUP BY a.category, 
         material, 
         a.brand

答案 1 :(得分:0)

你可以这样尝试

  

选择子字符串(new1,0,charindex('/',new1))作为类别,'product *'   作为Material,substring(new1,charindex('/',new1)+ 1,len(new1))作为品牌,   new1 from(从表中选择不同的类别+'/'+品牌作为new1)   温度