-1

有 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 秒,这并不让我高兴。

有人知道如何在组内获取唯一值吗?

升级版:

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))
4

2 回答 2

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
于 2012-07-31T18:47:41.403 回答
0

你可以试试这样

选择 substring(new1,0,charindex('/',new1)) 作为类别,'product *' 作为 Material,substring(new1,charindex('/',new1)+1,len(new1)) 作为品牌,new1 from (select distinct Category+'/'+brand as new1 from table) temp

于 2012-08-01T10:27:49.863 回答