diff --git a/mysql-test/r/type_datetime_hires.result b/mysql-test/r/type_datetime_hires.result
index ba1fe4adedb..203e45b86cb 100644
--- a/mysql-test/r/type_datetime_hires.result
+++ b/mysql-test/r/type_datetime_hires.result
@@ -338,3 +338,15 @@ select * from t1;
 a	b
 2010-01-02 03:04:05.000000	2010-01-02 03:04:05
 drop table t1;
+#
+# MDEV-4651 Crash in my_decimal2decimal in a ORDER BY query
+#
+SET @@time_zone='+00:00';
+CREATE TABLE t1 (a DATETIME(4) NOT NULL);
+INSERT INTO t1 VALUES ('2001-01-01 00:00:00'),('2000-00-00 00:00:00');
+SELECT UNIX_TIMESTAMP(a) FROM t1 ORDER BY 1;
+UNIX_TIMESTAMP(a)
+NULL
+978307200.0000
+DROP TABLE t1;
+SET @@time_zone=DEFAULT;
diff --git a/mysql-test/r/view.result b/mysql-test/r/view.result
index 576752f65c9..d6321cae92b 100644
--- a/mysql-test/r/view.result
+++ b/mysql-test/r/view.result
@@ -4863,6 +4863,18 @@ a
 1
 drop view v1;
 drop table t1,t2;
+#
+# MDEV-4593: p_s: crash in simplify_joins with delete using subselect
+# from view
+#
+create table `t1`(`a` int);
+create table `t2`(`a` int);
+create or replace view `v1` as select `a` from `t1`;
+prepare s from "delete  from `t2` order by (select 1 from `v1`)";
+execute s;
+deallocate prepare s;
+drop view v1;
+drop tables t1,t2;
 # -----------------------------------------------------------------
 # -- End of 5.3 tests.
 # -----------------------------------------------------------------
diff --git a/mysql-test/t/type_datetime_hires.test b/mysql-test/t/type_datetime_hires.test
index 74f686d4157..a62c227f563 100644
--- a/mysql-test/t/type_datetime_hires.test
+++ b/mysql-test/t/type_datetime_hires.test
@@ -69,3 +69,12 @@ alter table t1 modify b datetime, modify a datetime(6);
 select * from t1;
 drop table t1;
 
+--echo #
+--echo # MDEV-4651 Crash in my_decimal2decimal in a ORDER BY query
+--echo #
+SET @@time_zone='+00:00';
+CREATE TABLE t1 (a DATETIME(4) NOT NULL);
+INSERT INTO t1 VALUES ('2001-01-01 00:00:00'),('2000-00-00 00:00:00');
+SELECT UNIX_TIMESTAMP(a) FROM t1 ORDER BY 1;
+DROP TABLE t1;
+SET @@time_zone=DEFAULT;
diff --git a/mysql-test/t/view.test b/mysql-test/t/view.test
index 8b10e53f06d..404780a3368 100644
--- a/mysql-test/t/view.test
+++ b/mysql-test/t/view.test
@@ -4794,6 +4794,20 @@ WHERE a = alias.a );
 drop view v1;
 drop table t1,t2;
 
+--echo #
+--echo # MDEV-4593: p_s: crash in simplify_joins with delete using subselect
+--echo # from view
+--echo #
+
+create table `t1`(`a` int);
+create table `t2`(`a` int);
+create or replace view `v1` as select `a` from `t1`;
+prepare s from "delete  from `t2` order by (select 1 from `v1`)";
+execute s;
+deallocate prepare s;
+drop view v1;
+drop tables t1,t2;
+
 --echo # -----------------------------------------------------------------
 --echo # -- End of 5.3 tests.
 --echo # -----------------------------------------------------------------
diff --git a/sql/sql_derived.cc b/sql/sql_derived.cc
index ff5df31d2e2..2992bb0da6e 100644
--- a/sql/sql_derived.cc
+++ b/sql/sql_derived.cc
@@ -87,7 +87,16 @@ mysql_handle_derived(LEX *lex, uint phases)
 	 sl && !res;
 	 sl= sl->next_select_in_list())
     {
-      for (TABLE_LIST *cursor= sl->get_table_list();
+      TABLE_LIST *cursor= sl->get_table_list();
+      /*
+        DT_MERGE_FOR_INSERT is not needed for views/derived tables inside
+        subqueries. Views and derived tables of subqueries should be
+        processed normally.
+      */
+      if (phases == DT_MERGE_FOR_INSERT &&
+          cursor && cursor->top_table()->select_lex != &lex->select_lex)
+        continue;
+      for (;
 	   cursor && !res;
 	   cursor= cursor->next_local)
       {