我正在加载大量数据来创建提要。在此过程中必须有一个左连接来添加额外的数据。所以您知道对性能的要求很高。
不需要按主 ID 排序 - 因此没有必要,但需要的是对左连接部分进行排序。
考虑这个例子:
SELECT p.*, atts.name
FROM products as p
LEFT JOIN attributes as atts ON p.product_id = atts.product_id
现在我需要添加类似的内容
ORDER BY ??DEFAULT_PRODUCT_ORDER??, atts.position ASC
我尝试搜索这个问题,但我发现的最接近的是以 group by/max 答案结尾...
使用:MySQL/MariaDB
编辑:我还考虑过通过子查询预处理 SQL 中的属性,对它们进行排序并将它们连接为 1 列 - 但这不会太高效,不是吗?
最佳答案
在不知道您到底在寻找什么的情况下,您可以尝试一些方法
SELECT p.*, group_concat(atts.name)
FROM products as p
LEFT JOIN attributes as atts ON p.product_id = atts.product_id
Group BY /*all columns not included in aggregate (noc necc for mysql but other dbs like mssql get crazy about that*/
关于mysql - SQL 保留默认顺序但对左连接应用子顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41575497/