diff --git a/mysql-test/r/cache_innodb.result b/mysql-test/r/cache_innodb.result
index 17cfcd69ec36559387839de169ce10d28d2091e1..ea23d507a4bf077a6f794dcf3d20d6358ada333b 100644
--- a/mysql-test/r/cache_innodb.result
+++ b/mysql-test/r/cache_innodb.result
@@ -67,10 +67,10 @@ a
 2
 show status like "Qcache_queries_in_cache";
 Variable_name	Value
-Qcache_queries_in_cache	3
+Qcache_queries_in_cache	6
 show status like "Qcache_hits";
 Variable_name	Value
-Qcache_hits	3
+Qcache_hits	0
 insert into t1 values (3);
 insert into t2 values (3);
 insert into t1 values (4);
@@ -91,14 +91,14 @@ a
 2
 show status like "Qcache_queries_in_cache";
 Variable_name	Value
-Qcache_queries_in_cache	1
+Qcache_queries_in_cache	2
 show status like "Qcache_hits";
 Variable_name	Value
-Qcache_hits	4
+Qcache_hits	1
 commit;
 show status like "Qcache_queries_in_cache";
 Variable_name	Value
-Qcache_queries_in_cache	1
+Qcache_queries_in_cache	2
 drop table t3,t2,t1;
 CREATE TABLE t1 (id int(11) NOT NULL auto_increment, PRIMARY KEY  (id));
 select count(*) from t1;
diff --git a/mysql-test/r/query_cache.result b/mysql-test/r/query_cache.result
index 3c8ff8faddcc524d24db5e470a233bdf5cf633b5..0368fffa7c442d28f867fda67b102e18b227dfcb 100644
--- a/mysql-test/r/query_cache.result
+++ b/mysql-test/r/query_cache.result
@@ -1656,6 +1656,57 @@ Qcache_hits	1
 DROP TABLE t1;
 SET GLOBAL concurrent_insert= @save_concurrent_insert;
 SET GLOBAL query_cache_size= default;
+DROP TABLE IF EXISTS t1;
+FLUSH STATUS;
+SET GLOBAL query_cache_size=1048576;
+CREATE TABLE t1 (a INT);
+INSERT INTO t1 VALUES (1),(2),(3),(4),(5);
+SHOW STATUS LIKE 'Qcache_queries_in_cache';
+Variable_name	Value
+Qcache_queries_in_cache	0
+SELECT * FROM t1;
+a
+1
+2
+3
+4
+5
+BEGIN;
+SELECT * FROM t1;
+a
+1
+2
+3
+4
+5
+COMMIT;
+SHOW STATUS LIKE 'Qcache_queries_in_cache';
+Variable_name	Value
+Qcache_queries_in_cache	2
+SHOW STATUS LIKE "Qcache_hits";
+Variable_name	Value
+Qcache_hits	0
+SELECT * FROM t1;
+a
+1
+2
+3
+4
+5
+BEGIN;
+SELECT * FROM t1;
+a
+1
+2
+3
+4
+5
+COMMIT;
+SHOW STATUS LIKE "Qcache_hits";
+Variable_name	Value
+Qcache_hits	2
+DROP TABLE t1;
+SET GLOBAL query_cache_size= default;
 End of 5.0 tests
 CREATE TABLE t1 (a ENUM('rainbow'));
 INSERT INTO t1 VALUES (),(),(),(),();
diff --git a/mysql-test/t/query_cache.test b/mysql-test/t/query_cache.test
index fe720ee68b768395040f866646dd1cc8aa8fb3f7..6b1ef211fc1a98c5317c65818528506d87e4f1c0 100644
--- a/mysql-test/t/query_cache.test
+++ b/mysql-test/t/query_cache.test
@@ -1261,6 +1261,33 @@ DROP TABLE t1;
 SET GLOBAL concurrent_insert= @save_concurrent_insert;
 SET GLOBAL query_cache_size= default;
 
+#
+# Bug#36326: nested transaction and select
+#
+
+--disable_warnings
+DROP TABLE IF EXISTS t1;
+--enable_warnings
+
+FLUSH STATUS;
+SET GLOBAL query_cache_size=1048576;
+CREATE TABLE t1 (a INT);
+INSERT INTO t1 VALUES (1),(2),(3),(4),(5);
+SHOW STATUS LIKE 'Qcache_queries_in_cache';
+SELECT * FROM t1;
+BEGIN;
+SELECT * FROM t1;
+COMMIT;
+SHOW STATUS LIKE 'Qcache_queries_in_cache';
+SHOW STATUS LIKE "Qcache_hits";
+SELECT * FROM t1;
+BEGIN;
+SELECT * FROM t1;
+COMMIT;
+SHOW STATUS LIKE "Qcache_hits";
+DROP TABLE t1;
+SET GLOBAL query_cache_size= default;
+
 --echo End of 5.0 tests
 
 #
diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h
index c32ebc6d39db55b494449e4021cd7a0de8a18d0e..9ce8077249c4769562e5221186c4fc1378482089 100644
--- a/sql/mysql_priv.h
+++ b/sql/mysql_priv.h
@@ -862,6 +862,8 @@ struct Query_cache_query_flags
   unsigned int client_protocol_41:1;
   unsigned int result_in_binary_protocol:1;
   unsigned int more_results_exists:1;
+  unsigned int in_trans:1;
+  unsigned int autocommit:1;
   unsigned int pkt_nr;
   uint character_set_client_num;
   uint character_set_results_num;
diff --git a/sql/sql_cache.cc b/sql/sql_cache.cc
index 2fca739f667773630135d8310802b5a6d02e9a4b..5e098edbd7f01865a761c09788aad653845d67eb 100644
--- a/sql/sql_cache.cc
+++ b/sql/sql_cache.cc
@@ -1001,6 +1001,8 @@ void Query_cache::store_query(THD *thd, TABLE_LIST *tables_used)
     flags.result_in_binary_protocol= (unsigned int) thd->protocol->type();
     flags.more_results_exists= test(thd->server_status &
                                     SERVER_MORE_RESULTS_EXISTS);
+    flags.in_trans= test(thd->server_status & SERVER_STATUS_IN_TRANS);
+    flags.autocommit= test(thd->server_status & SERVER_STATUS_AUTOCOMMIT);
     flags.pkt_nr= net->pkt_nr;
     flags.character_set_client_num=
       thd->variables.character_set_client->number;
@@ -1022,7 +1024,7 @@ void Query_cache::store_query(THD *thd, TABLE_LIST *tables_used)
 long %d, 4.1: %d, bin_proto: %d, more results %d, pkt_nr: %d, \
 CS client: %u, CS result: %u, CS conn: %u, limit: %lu, TZ: 0x%lx, \
 sql mode: 0x%lx, sort len: %lu, conncat len: %lu, div_precision: %lu, \
-def_week_frmt: %lu",                          
+def_week_frmt: %lu, in_trans: %d, autocommit: %d",
                           (int)flags.client_long_flag,
                           (int)flags.client_protocol_41,
                           (int)flags.result_in_binary_protocol,
@@ -1037,7 +1039,10 @@ def_week_frmt: %lu",
                           flags.max_sort_length,
                           flags.group_concat_max_len,
                           flags.div_precision_increment,
-                          flags.default_week_format));
+                          flags.default_week_format,
+                          (int)flags.in_trans,
+                          (int)flags.autocommit));
+
     /*
      Make InnoDB to release the adaptive hash index latch before
      acquiring the query cache mutex.
@@ -1269,6 +1274,8 @@ Query_cache::send_result_to_client(THD *thd, char *sql, uint query_length)
   flags.result_in_binary_protocol= (unsigned int)thd->protocol->type();
   flags.more_results_exists= test(thd->server_status &
                                   SERVER_MORE_RESULTS_EXISTS);
+  flags.in_trans= test(thd->server_status & SERVER_STATUS_IN_TRANS);
+  flags.autocommit= test(thd->server_status & SERVER_STATUS_AUTOCOMMIT);
   flags.pkt_nr= thd->net.pkt_nr;
   flags.character_set_client_num= thd->variables.character_set_client->number;
   flags.character_set_results_num=
@@ -1288,7 +1295,7 @@ Query_cache::send_result_to_client(THD *thd, char *sql, uint query_length)
 long %d, 4.1: %d, bin_proto: %d, more results %d, pkt_nr: %d, \
 CS client: %u, CS result: %u, CS conn: %u, limit: %lu, TZ: 0x%lx, \
 sql mode: 0x%lx, sort len: %lu, conncat len: %lu, div_precision: %lu, \
-def_week_frmt: %lu",                          
+def_week_frmt: %lu, in_trans: %d, autocommit: %d",
                           (int)flags.client_long_flag,
                           (int)flags.client_protocol_41,
                           (int)flags.result_in_binary_protocol,
@@ -1303,7 +1310,9 @@ def_week_frmt: %lu",
                           flags.max_sort_length,
                           flags.group_concat_max_len,
                           flags.div_precision_increment,
-                          flags.default_week_format));
+                          flags.default_week_format,
+                          (int)flags.in_trans,
+                          (int)flags.autocommit));
   memcpy((uchar *)(sql + (tot_length - QUERY_CACHE_FLAGS_SIZE)),
 	 (uchar*) &flags, QUERY_CACHE_FLAGS_SIZE);
   query_block = (Query_cache_block *)  hash_search(&queries, (uchar*) sql,
diff --git a/tests/mysql_client_test.c b/tests/mysql_client_test.c
index 5f04dc48b4c24172bd209f14217470ac9eef2e8f..4f30d43f53f5cbd1efcc6ed1756771dea6f6958d 100644
--- a/tests/mysql_client_test.c
+++ b/tests/mysql_client_test.c
@@ -17712,6 +17712,63 @@ static void test_bug40365(void)
 
   DBUG_VOID_RETURN;
 }
+
+
+/**
+  Bug#36326: nested transaction and select
+*/
+
+#ifdef HAVE_QUERY_CACHE
+
+static void test_bug36326()
+{
+  int rc;
+
+  DBUG_ENTER("test_bug36326");
+  myheader("test_bug36326");
+
+  rc= mysql_autocommit(mysql, TRUE);
+  myquery(rc);
+  rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
+  myquery(rc);
+  rc= mysql_query(mysql, "CREATE  TABLE t1 (a INTEGER)");
+  myquery(rc);
+  rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1)");
+  myquery(rc);
+  rc= mysql_query(mysql, "SET GLOBAL query_cache_type = 1");
+  myquery(rc);
+  rc= mysql_query(mysql, "SET GLOBAL query_cache_size = 1048576");
+  myquery(rc);
+  DIE_UNLESS(!(mysql->server_status & SERVER_STATUS_IN_TRANS));
+  DIE_UNLESS(mysql->server_status & SERVER_STATUS_AUTOCOMMIT);
+  rc= mysql_query(mysql, "BEGIN");
+  myquery(rc);
+  DIE_UNLESS(mysql->server_status & SERVER_STATUS_IN_TRANS);
+  rc= mysql_query(mysql, "SELECT * FROM t1");
+  myquery(rc);
+  rc= my_process_result(mysql);
+  DIE_UNLESS(rc == 1);
+  rc= mysql_rollback(mysql);
+  myquery(rc);
+  rc= mysql_query(mysql, "ROLLBACK");
+  myquery(rc);
+  DIE_UNLESS(!(mysql->server_status & SERVER_STATUS_IN_TRANS));
+  rc= mysql_query(mysql, "SELECT * FROM t1");
+  myquery(rc);
+  DIE_UNLESS(!(mysql->server_status & SERVER_STATUS_IN_TRANS));
+  rc= my_process_result(mysql);
+  DIE_UNLESS(rc == 1);
+  rc= mysql_query(mysql, "DROP TABLE t1");
+  myquery(rc);
+  rc= mysql_query(mysql, "SET GLOBAL query_cache_size = 0");
+  myquery(rc);
+
+  DBUG_VOID_RETURN;
+}
+
+#endif
+
+
 /*
   Read and parse arguments and MySQL options from my.cnf
 */
@@ -18024,6 +18081,9 @@ static struct my_tests_st my_tests[]= {
   { "test_bug40365", test_bug40365 },
 #ifdef HAVE_SPATIAL
   { "test_bug37956", test_bug37956 },
+#endif
+#ifdef HAVE_QUERY_CACHE
+  { "test_bug36326", test_bug36326 },
 #endif
   { 0, 0 }
 };