Mysql:在组中选择唯一值?
共有3栏-类别、材质和品牌。老实说,它不是来自单个表的列,而是来自使用联接的8个其他表的查询结果Mysql:在组中选择唯一值?,mysql,sql,group-by,distinct,Mysql,Sql,Group By,Distinct,共有3栏-类别、材质和品牌。老实说,它不是来自单个表的列,而是来自使用联接的8个其他表的查询结果 | category | material | brand | ----------------------------------------------| | engines | product 1 | abb | -> unique for category "engines" | en
| 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 |
如果我使用GROUPBY语句GROUPBY CONCAT,每300个结果的查询时间超过10秒,这并不能让我高兴
任何人都知道如何获得唯一的值团队内部
UPD:
尝试:
你可以这样试试
选择子字符串new1,0,charindex'/',new1作为类别“product*”
作为材料,子字符串new1,charindex'/',new1+1,lennew1作为品牌,
从表中选择不同类别+'/'+品牌作为新1
临时工
发布你的问题,首先,也许它只需要一个小的更正或类似的,如果没有,那么我们将有一个更好的了解你的问题有愚蠢的选择。。。左连接。。。左连接。。。左连接。。。这是第一个表格的结果。从第一个表中获取第二个表的主要用途;为什么要将驱动器->e+h排除在最终结果之外?你真的想在“材料”列中“产品”旁边的*吗?我应该得到唯一的类别URL,每个产品引用一个品牌和无限数量的类别。产品的标题一点也不重要。您应该尽量避免在联接中使用复杂的条件,尤其是在列上使用函数:在CONCAT'taxonomy/term/'上,th0.tid=url\u alias.sourceHm,我首先尝试了您的解决方案。它执行约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))
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