与Group By和sql_mode = only_full_group_by冲突

时间:2018-11-14 23:59:40

标签: mysql

以下是查询:

SELECT c.*, f.file
FROM `category` c
LEFT JOIN
(
    SELECT `file`, catId
    FROM `file`
    ORDER BY sortId
) f
    ON f.catId = c.catId
GROUP BY c.catId
ORDER BY c.sortId

在mysql更新之前,它可以正常工作。我已经在这上面盘旋了一段时间,但似乎找不到解决方法。

这是我尝试的最后一个选择:

SELECT *
FROM `file`
LEFT JOIN `category`
    ON `file`.catId = `category`.catId
GROUP BY `file`.catId
ORDER BY `category`.sortId

任何指导将不胜感激。

1 个答案:

答案 0 :(得分:0)

使用加窗功能,您可以获得最高的排名。 (基于this

SELECT r.*
FROM (
    SELECT c.*, f.file, ROW_NUMBER() OVER (PARTITION BY catId ORDER BY sortId) AS rank
    FROM `file` f
    LEFT JOIN category c
        ON c.catId = c.catId
    ORDER BY f.catId
) AS r
WHERE r.rank = 1 
ORDER BY catId