summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mysql-test/r/select_found.result15
-rw-r--r--mysql-test/t/select_found.test10
-rw-r--r--sql/sql_select.cc2
3 files changed, 26 insertions, 1 deletions
diff --git a/mysql-test/r/select_found.result b/mysql-test/r/select_found.result
index 92758fa134b..7b38515cf70 100644
--- a/mysql-test/r/select_found.result
+++ b/mysql-test/r/select_found.result
@@ -348,3 +348,18 @@ select found_rows();
found_rows()
75
drop table t1;
+create table t1(c1 int);
+insert into t1 values(1),(2),(3),(4),(5);
+select * from t1 order by c1 limit 2,1;
+c1
+3
+select found_rows();
+found_rows()
+3
+select sql_calc_found_rows * from t1 order by c1 limit 2,1;
+c1
+3
+select found_rows();
+found_rows()
+5
+drop table t1;
diff --git a/mysql-test/t/select_found.test b/mysql-test/t/select_found.test
index 88940eaf2b8..33613697722 100644
--- a/mysql-test/t/select_found.test
+++ b/mysql-test/t/select_found.test
@@ -277,3 +277,13 @@ select sql_calc_found_rows * from t1 ignore index (i) where i = 0 order by v lim
select found_rows();
drop table t1;
+#
+# MDEV-9390 Function found_rows() gives incorrect result where the previous SELECT contains ORDER BY clause
+#
+create table t1(c1 int);
+insert into t1 values(1),(2),(3),(4),(5);
+select * from t1 order by c1 limit 2,1;
+select found_rows();
+select sql_calc_found_rows * from t1 order by c1 limit 2,1;
+select found_rows();
+drop table t1;
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 18259a27bc7..55caa6d1ab9 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -20813,7 +20813,7 @@ create_sort_index(THD *thd, JOIN *join, ORDER *order,
select, filesort_limit, 0,
&examined_rows, &found_rows);
table->sort.found_records= filesort_retval;
- tab->records= found_rows; // For SQL_CALC_ROWS
+ tab->records= join->select_options & OPTION_FOUND_ROWS ? found_rows : filesort_retval;
if (quick_created)
{