MySQL 中的不同结果集和 MariaDB 使用 GROUP BY 和 ORDER BY
Different ResultSet in MySQL and MariaDB Using GROUP BY and ORDER BY
我有一个 table:voj_submission
具有以下结构:
CREATE TABLE IF NOT EXISTS `voj_submissions` (
`submission_id` bigint(20) NOT NULL,
`problem_id` bigint(20) NOT NULL,
`uid` bigint(20) NOT NULL,
`submission_judge_result` varchar(8) DEFAULT NULL,
) ENGINE=InnoDB AUTO_INCREMENT=1004 DEFAULT CHARSET=utf8;
而 table 中的数据可能是这样的:
SubmissionID ProblemID UserID JudgeResult
1000 1000 User1 AC
1001 1000 User1 WA
1002 1000 User2 CE
1003 1001 User1 AC
而下面SQL想得到最新的问题判断结果:
SELECT submission_id, problem_id, submission_judge_result
FROM (
SELECT * FROM
voj_submissions
ORDER BY submission_id DESC
) s
WHERE uid = 'User1'
AND problem_id >= 1000
AND problem_id < 1010
GROUP BY problem_id
在MySQL 5.6中,它可以正常工作,returns结果集如下:
SubmissionID ProblemID JudgeResult
1001 1000 WA
1003 1001 AC
但在 MariaDB 10.0.14 中,它 returns:
SubmissionID ProblemID JudgeResult
1000 1000 AC
1003 1001 AC
这意味着 ORDER BY DESC
没有在 MariaDB 中执行。
我该如何解决这个问题?
参考:
- https://mariadb.com/kb/en/mariadb/group-by-trick-has-been-optimized-away/
- MySQL order by before group by
正如@bluefeet 所说post:
Using an ORDER BY in a subquery is not the best solution to this problem.
The best solution to get the max(post_date) by author is to use a subquery to return the max date and then join that to your table on both the post_author and the max date.
所以解决方案应该是:
SELECT submission_id, s1.problem_id, submission_judge_result
FROM voj_submissions s1
INNER JOIN (
SELECT MAX(submission_id) AS max_submission_id, problem_id
FROM voj_submissions
WHERE uid = 1000
AND problem_id >= 1000
AND problem_id < 1010
GROUP BY problem_id
) s2
ON s1.problem_id = s2.problem_id
AND s1.submission_id = s2.max_submission_id
这在 MariaDB 和 MySQL 中都有效。
我有一个 table:voj_submission
具有以下结构:
CREATE TABLE IF NOT EXISTS `voj_submissions` (
`submission_id` bigint(20) NOT NULL,
`problem_id` bigint(20) NOT NULL,
`uid` bigint(20) NOT NULL,
`submission_judge_result` varchar(8) DEFAULT NULL,
) ENGINE=InnoDB AUTO_INCREMENT=1004 DEFAULT CHARSET=utf8;
而 table 中的数据可能是这样的:
SubmissionID ProblemID UserID JudgeResult
1000 1000 User1 AC
1001 1000 User1 WA
1002 1000 User2 CE
1003 1001 User1 AC
而下面SQL想得到最新的问题判断结果:
SELECT submission_id, problem_id, submission_judge_result
FROM (
SELECT * FROM
voj_submissions
ORDER BY submission_id DESC
) s
WHERE uid = 'User1'
AND problem_id >= 1000
AND problem_id < 1010
GROUP BY problem_id
在MySQL 5.6中,它可以正常工作,returns结果集如下:
SubmissionID ProblemID JudgeResult
1001 1000 WA
1003 1001 AC
但在 MariaDB 10.0.14 中,它 returns:
SubmissionID ProblemID JudgeResult
1000 1000 AC
1003 1001 AC
这意味着 ORDER BY DESC
没有在 MariaDB 中执行。
我该如何解决这个问题?
参考:
- https://mariadb.com/kb/en/mariadb/group-by-trick-has-been-optimized-away/
- MySQL order by before group by
正如@bluefeet 所说post:
Using an ORDER BY in a subquery is not the best solution to this problem.
The best solution to get the max(post_date) by author is to use a subquery to return the max date and then join that to your table on both the post_author and the max date.
所以解决方案应该是:
SELECT submission_id, s1.problem_id, submission_judge_result
FROM voj_submissions s1
INNER JOIN (
SELECT MAX(submission_id) AS max_submission_id, problem_id
FROM voj_submissions
WHERE uid = 1000
AND problem_id >= 1000
AND problem_id < 1010
GROUP BY problem_id
) s2
ON s1.problem_id = s2.problem_id
AND s1.submission_id = s2.max_submission_id
这在 MariaDB 和 MySQL 中都有效。