diff --git a/mysql-test/r/bdb.result b/mysql-test/r/bdb.result
index ac14f89162200e8a163709a67f8403a3df6000ca..a564fd1045cf95a82baa03bb87ff0c913c90734e 100644
--- a/mysql-test/r/bdb.result
+++ b/mysql-test/r/bdb.result
@@ -1289,6 +1289,25 @@ SELECT id FROM t1 WHERE (list_id = 1) AND (term = "letterd");
 id
 4
 DROP TABLE t1;
+create table t1 (a int, key(a)) engine=bdb;
+create table t2 (b int, key(b)) engine=bdb;
+insert into t1 values (1),(1),(2),(3),(4);
+insert into t2 values (1),(5),(6),(7);
+delete from t1 where (a in (select b from t2));
+select count(*) from t1;
+count(*)
+3
+insert into t1 set a=(select b from t2);
+ERROR 21000: Subquery returns more than 1 row
+select count(*) from t1;
+count(*)
+3
+update t1 set a = a + 1 where (a in (select b from t2));
+select count(*) from t1;
+count(*)
+3
+drop table t1, t2;
+End of 4.1 tests
 create temporary table t1 (a int, primary key(a)) engine=bdb;
 select * from t1;
 a
diff --git a/mysql-test/t/bdb.test b/mysql-test/t/bdb.test
index 72b3ee89ed533c3d8be2cdfdfe258078e9ef1741..d3068b29e28cacb242c09603413b454dc1eaac65 100644
--- a/mysql-test/t/bdb.test
+++ b/mysql-test/t/bdb.test
@@ -938,7 +938,25 @@ SELECT id FROM t1 WHERE (list_id = 1) AND (term = "lettera");
 SELECT id FROM t1 WHERE (list_id = 1) AND (term = "letterd");
 DROP TABLE t1;
 
-# End of 4.1 tests
+#
+# Bug #15536: Crash when DELETE with subquery using BDB tables
+#
+create table t1 (a int, key(a)) engine=bdb;
+create table t2 (b int, key(b)) engine=bdb;
+insert into t1 values (1),(1),(2),(3),(4);
+insert into t2 values (1),(5),(6),(7);
+delete from t1 where (a in (select b from t2));
+select count(*) from t1;
+# INSERT also blows up
+--error 1242
+insert into t1 set a=(select b from t2);
+select count(*) from t1;
+# UPDATE also blows up
+update t1 set a = a + 1 where (a in (select b from t2));
+select count(*) from t1;
+drop table t1, t2;
+
+--echo End of 4.1 tests
 
 #
 # alter temp table
diff --git a/sql/share/charsets/latin5.xml b/sql/share/charsets/latin5.xml
index 67e5873c50385bb8b2408a530e55bdf97c67d17e..5004f0458895e556ce77b80e1b9af75fef2fe66e 100644
--- a/sql/share/charsets/latin5.xml
+++ b/sql/share/charsets/latin5.xml
@@ -112,11 +112,6 @@
 
 
 <collation name="latin5_turkish_ci">
-<!--
-# Note: all accented characters are compared separately (this
-# is different from the default latin1 character set, where
-# e.g.  a = ä = á, etc.).
--->
 <map>
  00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
  10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
@@ -130,10 +125,10 @@
  9C 9D 9E 9F A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB
  AC AD AE AF B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB
  BC BD BE BF C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB
- CC CD CE CF D0 D1 D2 44 D3 D4 D5 D6 D7 D8 D9 DA
- 49 DB DC DD DE DF 53 E0 E1 E2 E3 E4 5B 4C 58 E5
- CC CD CE CF D0 D1 D2 44 D3 D4 D5 D6 D7 D8 D9 DA
- 49 DB DC DD DE DF 53 FA E1 E2 E3 E4 5B 4B 58 FF
+ 41 41 41 41 41 41 41 44 46 46 46 46 4C 4C 4C 4C
+ 49 51 52 52 52 52 53 E0 52 5A 5A 5A 5B 4C 58 57
+ 41 41 41 41 41 41 41 44 46 46 46 46 4C 4C 4C 4C
+ 49 51 52 52 52 52 53 FA 52 5A 5A 5A 5B 4B 58 5F
 </map>
 </collation>
 
diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc
index 92fc7e4bfd22ba0fa779bd994c81460b4cfde1af..393e81847257628c88dc02011a7a3d32a05edbf4 100644
--- a/sql/sql_delete.cc
+++ b/sql/sql_delete.cc
@@ -293,6 +293,7 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
     if (!transactional_table)
       thd->options|=OPTION_STATUS_NO_TRANS_UPDATE;
   }
+  free_underlaid_joins(thd, select_lex);
   if (transactional_table)
   {
     if (ha_autocommit_or_rollback(thd,error >= 0))
@@ -304,7 +305,6 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
     mysql_unlock_tables(thd, thd->lock);
     thd->lock=0;
   }
-  free_underlaid_joins(thd, select_lex);
   if (error < 0)
   {
     thd->row_count_func= deleted;
diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc
index 8903f28be11d341d1051fa4c33a4ac278b6dc163..e5ea296afab2960218017554ec3c16e827ec45e4 100644
--- a/sql/sql_insert.cc
+++ b/sql/sql_insert.cc
@@ -257,7 +257,7 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list,
   */
   bool log_on= (thd->options & OPTION_BIN_LOG) ||
     (!(thd->security_ctx->master_access & SUPER_ACL));
-  bool transactional_table;
+  bool transactional_table, joins_freed= FALSE;
   uint value_count;
   ulong counter = 1;
   ulonglong id;
@@ -513,6 +513,9 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list,
     thd->row_count++;
   }
 
+  free_underlaid_joins(thd, &thd->lex->select_lex);
+  joins_freed= TRUE;
+
   /*
     Now all rows are inserted.  Time to update logs and sends response to
     user
@@ -611,7 +614,6 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list,
     thd->row_count_func= info.copied+info.deleted+info.updated;
     ::send_ok(thd, (ulong) thd->row_count_func, id, buff);
   }
-  free_underlaid_joins(thd, &thd->lex->select_lex);
   thd->abort_on_warning= 0;
   DBUG_RETURN(FALSE);
 
@@ -620,7 +622,8 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list,
   if (lock_type == TL_WRITE_DELAYED)
     end_delayed_insert(thd);
 #endif
-  free_underlaid_joins(thd, &thd->lex->select_lex);
+  if (!joins_freed)
+    free_underlaid_joins(thd, &thd->lex->select_lex);
   thd->abort_on_warning= 0;
   DBUG_RETURN(TRUE);
 }
diff --git a/sql/sql_update.cc b/sql/sql_update.cc
index 6ec895bacb1d57689f9892bf9b05b4e512de7916..a86d1b57190c587d97fc867aea4483aea1ce1fb7 100644
--- a/sql/sql_update.cc
+++ b/sql/sql_update.cc
@@ -527,6 +527,7 @@ int mysql_update(THD *thd,
     if (!transactional_table)
       thd->options|=OPTION_STATUS_NO_TRANS_UPDATE;
   }
+  free_underlaid_joins(thd, select_lex);
   if (transactional_table)
   {
     if (ha_autocommit_or_rollback(thd, error >= 0))
@@ -539,7 +540,6 @@ int mysql_update(THD *thd,
     thd->lock=0;
   }
 
-  free_underlaid_joins(thd, select_lex);
   if (error < 0)
   {
     char buff[STRING_BUFFER_USUAL_SIZE];