You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4348 lines
155 KiB

14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
Merge with 5.1-microseconds A lot of small fixes and new test cases. client/mysqlbinlog.cc: Cast removed client/mysqltest.cc: Added missing DBUG_RETURN include/my_pthread.h: set_timespec_time_nsec() now only takes one argument mysql-test/t/date_formats.test: Remove --disable_ps_protocl as now also ps supports microseconds mysys/my_uuid.c: Changed to use my_interval_timer() instead of my_getsystime() mysys/waiting_threads.c: Changed to use my_hrtime() sql/field.h: Added bool special_const_compare() for fields that may convert values before compare (like year) sql/field_conv.cc: Added test to get optimal copying of identical temporal values. sql/item.cc: Return that item_int is equal if it's positive, even if unsigned flag is different. Fixed Item_cache_str::save_in_field() to have identical null check as other similar functions Added proper NULL check to Item_cache_int::save_in_field() sql/item_cmpfunc.cc: Don't call convert_constant_item() if there is nothing that is worth converting. Simplified test when years should be converted sql/item_sum.cc: Mark cache values in Item_sum_hybrid as not constants to ensure they are not replaced by other cache values in compare_datetime() sql/item_timefunc.cc: Changed sec_to_time() to take a my_decimal argument to ensure we don't loose any sub seconds. Added Item_temporal_func::get_time() (This simplifies some things) sql/mysql_priv.h: Added Lazy_string_decimal() sql/mysqld.cc: Added my_decimal constants max_seconds_for_time_type, time_second_part_factor sql/table.cc: Changed expr_arena to be of type CONVENTIONAL_EXECUTION to ensure that we don't loose any items that are created by fix_fields() sql/tztime.cc: TIME_to_gmt_sec() now sets *in_dst_time_gap in case of errors This is needed to be able to detect if timestamp is 0 storage/maria/lockman.c: Changed from my_getsystime() to set_timespec_time_nsec() storage/maria/ma_loghandler.c: Changed from my_getsystime() to my_hrtime() storage/maria/ma_recovery.c: Changed from my_getsystime() to mmicrosecond_interval_timer() storage/maria/unittest/trnman-t.c: Changed from my_getsystime() to mmicrosecond_interval_timer() storage/xtradb/handler/ha_innodb.cc: Added support for new time,datetime and timestamp unittest/mysys/thr_template.c: my_getsystime() -> my_interval_timer() unittest/mysys/waiting_threads-t.c: my_getsystime() -> my_interval_timer()
15 years ago
Merge with 5.1-microseconds A lot of small fixes and new test cases. client/mysqlbinlog.cc: Cast removed client/mysqltest.cc: Added missing DBUG_RETURN include/my_pthread.h: set_timespec_time_nsec() now only takes one argument mysql-test/t/date_formats.test: Remove --disable_ps_protocl as now also ps supports microseconds mysys/my_uuid.c: Changed to use my_interval_timer() instead of my_getsystime() mysys/waiting_threads.c: Changed to use my_hrtime() sql/field.h: Added bool special_const_compare() for fields that may convert values before compare (like year) sql/field_conv.cc: Added test to get optimal copying of identical temporal values. sql/item.cc: Return that item_int is equal if it's positive, even if unsigned flag is different. Fixed Item_cache_str::save_in_field() to have identical null check as other similar functions Added proper NULL check to Item_cache_int::save_in_field() sql/item_cmpfunc.cc: Don't call convert_constant_item() if there is nothing that is worth converting. Simplified test when years should be converted sql/item_sum.cc: Mark cache values in Item_sum_hybrid as not constants to ensure they are not replaced by other cache values in compare_datetime() sql/item_timefunc.cc: Changed sec_to_time() to take a my_decimal argument to ensure we don't loose any sub seconds. Added Item_temporal_func::get_time() (This simplifies some things) sql/mysql_priv.h: Added Lazy_string_decimal() sql/mysqld.cc: Added my_decimal constants max_seconds_for_time_type, time_second_part_factor sql/table.cc: Changed expr_arena to be of type CONVENTIONAL_EXECUTION to ensure that we don't loose any items that are created by fix_fields() sql/tztime.cc: TIME_to_gmt_sec() now sets *in_dst_time_gap in case of errors This is needed to be able to detect if timestamp is 0 storage/maria/lockman.c: Changed from my_getsystime() to set_timespec_time_nsec() storage/maria/ma_loghandler.c: Changed from my_getsystime() to my_hrtime() storage/maria/ma_recovery.c: Changed from my_getsystime() to mmicrosecond_interval_timer() storage/maria/unittest/trnman-t.c: Changed from my_getsystime() to mmicrosecond_interval_timer() storage/xtradb/handler/ha_innodb.cc: Added support for new time,datetime and timestamp unittest/mysys/thr_template.c: my_getsystime() -> my_interval_timer() unittest/mysys/waiting_threads-t.c: my_getsystime() -> my_interval_timer()
15 years ago
Back-ported the patch of the mysql-5.6 code line that fixed several defects in the greedy optimization: 1) The greedy optimizer calculated the 'compare-cost' (CPU-cost) for iterating over the partial plan result at each level in the query plan as 'record_count / (double) TIME_FOR_COMPARE' This cost was only used locally for 'best' calculation at each level, and *not* accumulated into the total cost for the query plan. This fix added the 'CPU-cost' of processing 'current_record_count' records at each level to 'current_read_time' *before* it is used as 'accumulated cost' argument to recursive best_extension_by_limited_search() calls. This ensured that the cost of a huge join-fanout early in the QEP was correctly reflected in the cost of the final QEP. To get identical cost for a 'best' optimized query and a straight_join with the same join order, the same change was also applied to optimize_straight_join() and get_partial_join_cost() 2) Furthermore to get equal cost for 'best' optimized query and a straight_join the new code substrcated the same '0.001' in optimize_straight_join() as it had been already done in best_extension_by_limited_search() 3) When best_extension_by_limited_search() aggregated the 'best' plan a plan was 'best' by the check : 'if ((search_depth == 1) || (current_read_time < join->best_read))' The term '(search_depth == 1' incorrectly caused a new best plan to be collected whenever the specified 'search_depth' was reached - even if this partial query plan was more expensive than what we had already found.
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
Back-ported the patch of the mysql-5.6 code line that fixed several defects in the greedy optimization: 1) The greedy optimizer calculated the 'compare-cost' (CPU-cost) for iterating over the partial plan result at each level in the query plan as 'record_count / (double) TIME_FOR_COMPARE' This cost was only used locally for 'best' calculation at each level, and *not* accumulated into the total cost for the query plan. This fix added the 'CPU-cost' of processing 'current_record_count' records at each level to 'current_read_time' *before* it is used as 'accumulated cost' argument to recursive best_extension_by_limited_search() calls. This ensured that the cost of a huge join-fanout early in the QEP was correctly reflected in the cost of the final QEP. To get identical cost for a 'best' optimized query and a straight_join with the same join order, the same change was also applied to optimize_straight_join() and get_partial_join_cost() 2) Furthermore to get equal cost for 'best' optimized query and a straight_join the new code substrcated the same '0.001' in optimize_straight_join() as it had been already done in best_extension_by_limited_search() 3) When best_extension_by_limited_search() aggregated the 'best' plan a plan was 'best' by the check : 'if ((search_depth == 1) || (current_read_time < join->best_read))' The term '(search_depth == 1' incorrectly caused a new best plan to be collected whenever the specified 'search_depth' was reached - even if this partial query plan was more expensive than what we had already found.
14 years ago
Back-ported the patch of the mysql-5.6 code line that fixed several defects in the greedy optimization: 1) The greedy optimizer calculated the 'compare-cost' (CPU-cost) for iterating over the partial plan result at each level in the query plan as 'record_count / (double) TIME_FOR_COMPARE' This cost was only used locally for 'best' calculation at each level, and *not* accumulated into the total cost for the query plan. This fix added the 'CPU-cost' of processing 'current_record_count' records at each level to 'current_read_time' *before* it is used as 'accumulated cost' argument to recursive best_extension_by_limited_search() calls. This ensured that the cost of a huge join-fanout early in the QEP was correctly reflected in the cost of the final QEP. To get identical cost for a 'best' optimized query and a straight_join with the same join order, the same change was also applied to optimize_straight_join() and get_partial_join_cost() 2) Furthermore to get equal cost for 'best' optimized query and a straight_join the new code substrcated the same '0.001' in optimize_straight_join() as it had been already done in best_extension_by_limited_search() 3) When best_extension_by_limited_search() aggregated the 'best' plan a plan was 'best' by the check : 'if ((search_depth == 1) || (current_read_time < join->best_read))' The term '(search_depth == 1' incorrectly caused a new best plan to be collected whenever the specified 'search_depth' was reached - even if this partial query plan was more expensive than what we had already found.
14 years ago
14 years ago
  1. drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t11,t12;
  2. select (select 2);
  3. (select 2)
  4. 2
  5. explain extended select (select 2);
  6. id select_type table type possible_keys key key_len ref rows filtered Extra
  7. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  8. Warnings:
  9. Note 1249 Select 2 was reduced during optimization
  10. Note 1003 select 2 AS `(select 2)`
  11. SELECT (SELECT 1) UNION SELECT (SELECT 2);
  12. (SELECT 1)
  13. 1
  14. 2
  15. explain extended SELECT (SELECT 1) UNION SELECT (SELECT 2);
  16. id select_type table type possible_keys key key_len ref rows filtered Extra
  17. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  18. 3 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  19. NULL UNION RESULT <union1,3> ALL NULL NULL NULL NULL NULL NULL
  20. Warnings:
  21. Note 1249 Select 2 was reduced during optimization
  22. Note 1249 Select 4 was reduced during optimization
  23. Note 1003 select 1 AS `(SELECT 1)` union select 2 AS `(SELECT 2)`
  24. SELECT (SELECT (SELECT 0 UNION SELECT 0));
  25. (SELECT (SELECT 0 UNION SELECT 0))
  26. 0
  27. explain extended SELECT (SELECT (SELECT 0 UNION SELECT 0));
  28. id select_type table type possible_keys key key_len ref rows filtered Extra
  29. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  30. 3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  31. 4 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  32. NULL UNION RESULT <union3,4> ALL NULL NULL NULL NULL NULL NULL
  33. Warnings:
  34. Note 1249 Select 2 was reduced during optimization
  35. Note 1003 select (select 0 union select 0) AS `(SELECT (SELECT 0 UNION SELECT 0))`
  36. SELECT (SELECT 1 FROM (SELECT 1) as b HAVING a=1) as a;
  37. ERROR 42S22: Reference 'a' not supported (forward reference in item list)
  38. SELECT (SELECT 1 FROM (SELECT 1) as b HAVING b=1) as a,(SELECT 1 FROM (SELECT 1) as c HAVING a=1) as b;
  39. ERROR 42S22: Reference 'b' not supported (forward reference in item list)
  40. SELECT (SELECT 1),MAX(1) FROM (SELECT 1) as a;
  41. (SELECT 1) MAX(1)
  42. 1 1
  43. SELECT (SELECT a) as a;
  44. ERROR 42S22: Reference 'a' not supported (forward reference in item list)
  45. EXPLAIN EXTENDED SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1;
  46. id select_type table type possible_keys key key_len ref rows filtered Extra
  47. 1 PRIMARY <derived2> system NULL NULL NULL NULL 1 100.00
  48. 3 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  49. 2 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  50. Warnings:
  51. Note 1276 Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1
  52. Note 1276 Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1
  53. Note 1003 select 1 AS `1` from (select 1 AS `a`) `b` having (<expr_cache><1>((select 1)) = 1)
  54. SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1;
  55. 1
  56. 1
  57. SELECT (SELECT 1), a;
  58. ERROR 42S22: Unknown column 'a' in 'field list'
  59. SELECT 1 as a FROM (SELECT 1) as b HAVING (SELECT a)=1;
  60. a
  61. 1
  62. SELECT 1 FROM (SELECT (SELECT a) b) c;
  63. ERROR 42S22: Unknown column 'a' in 'field list'
  64. SELECT * FROM (SELECT 1 as id) b WHERE id IN (SELECT * FROM (SELECT 1 as id) c ORDER BY id);
  65. id
  66. 1
  67. SELECT * FROM (SELECT 1) a WHERE 1 IN (SELECT 1,1);
  68. ERROR 21000: Operand should contain 1 column(s)
  69. SELECT 1 IN (SELECT 1);
  70. 1 IN (SELECT 1)
  71. 1
  72. SELECT 1 FROM (SELECT 1 as a) b WHERE 1 IN (SELECT (SELECT a));
  73. 1
  74. 1
  75. select (SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE(1));
  76. ERROR HY000: Incorrect usage of PROCEDURE and subquery
  77. SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE((SELECT 1));
  78. ERROR HY000: Incorrect parameters to procedure 'ANALYSE'
  79. SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NULL;
  80. ERROR 42S22: Unknown column 'a' in 'field list'
  81. SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NOT NULL;
  82. ERROR 42S22: Unknown column 'a' in 'field list'
  83. SELECT (SELECT 1,2,3) = ROW(1,2,3);
  84. (SELECT 1,2,3) = ROW(1,2,3)
  85. 1
  86. SELECT (SELECT 1,2,3) = ROW(1,2,1);
  87. (SELECT 1,2,3) = ROW(1,2,1)
  88. 0
  89. SELECT (SELECT 1,2,3) < ROW(1,2,1);
  90. (SELECT 1,2,3) < ROW(1,2,1)
  91. 0
  92. SELECT (SELECT 1,2,3) > ROW(1,2,1);
  93. (SELECT 1,2,3) > ROW(1,2,1)
  94. 1
  95. SELECT (SELECT 1,2,3) = ROW(1,2,NULL);
  96. (SELECT 1,2,3) = ROW(1,2,NULL)
  97. NULL
  98. SELECT ROW(1,2,3) = (SELECT 1,2,3);
  99. ROW(1,2,3) = (SELECT 1,2,3)
  100. 1
  101. SELECT ROW(1,2,3) = (SELECT 1,2,1);
  102. ROW(1,2,3) = (SELECT 1,2,1)
  103. 0
  104. SELECT ROW(1,2,3) < (SELECT 1,2,1);
  105. ROW(1,2,3) < (SELECT 1,2,1)
  106. 0
  107. SELECT ROW(1,2,3) > (SELECT 1,2,1);
  108. ROW(1,2,3) > (SELECT 1,2,1)
  109. 1
  110. SELECT ROW(1,2,3) = (SELECT 1,2,NULL);
  111. ROW(1,2,3) = (SELECT 1,2,NULL)
  112. NULL
  113. SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a');
  114. (SELECT 1.5,2,'a') = ROW(1.5,2,'a')
  115. 1
  116. SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b');
  117. (SELECT 1.5,2,'a') = ROW(1.5,2,'b')
  118. 0
  119. SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b');
  120. (SELECT 1.5,2,'a') = ROW('1.5b',2,'b')
  121. 0
  122. Warnings:
  123. Warning 1292 Truncated incorrect DOUBLE value: '1.5b'
  124. SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a');
  125. (SELECT 'b',2,'a') = ROW(1.5,2,'a')
  126. 0
  127. SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a');
  128. (SELECT 1.5,2,'a') = ROW(1.5,'2','a')
  129. 1
  130. SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a');
  131. (SELECT 1.5,'c','a') = ROW(1.5,2,'a')
  132. 0
  133. SELECT (SELECT * FROM (SELECT 'test' a,'test' b) a);
  134. ERROR 21000: Operand should contain 1 column(s)
  135. SELECT 1 as a,(SELECT a+a) b,(SELECT b);
  136. a b (SELECT b)
  137. 1 2 2
  138. create table t1 (a int);
  139. create table t2 (a int, b int);
  140. create table t3 (a int);
  141. create table t4 (a int not null, b int not null);
  142. insert into t1 values (2);
  143. insert into t2 values (1,7),(2,7);
  144. insert into t4 values (4,8),(3,8),(5,9);
  145. select (select a from t1 where t1.a = a1) as a2, (select b from t2 where t2.b=a2) as a1;
  146. ERROR 42S22: Reference 'a1' not supported (forward reference in item list)
  147. select (select a from t1 where t1.a=t2.a), a from t2;
  148. (select a from t1 where t1.a=t2.a) a
  149. NULL 1
  150. 2 2
  151. select (select a from t1 where t1.a=t2.b), a from t2;
  152. (select a from t1 where t1.a=t2.b) a
  153. NULL 1
  154. NULL 2
  155. select (select a from t1), a, (select 1 union select 2 limit 1) from t2;
  156. (select a from t1) a (select 1 union select 2 limit 1)
  157. 2 1 1
  158. 2 2 1
  159. select (select a from t3), a from t2;
  160. (select a from t3) a
  161. NULL 1
  162. NULL 2
  163. select * from t2 where t2.a=(select a from t1);
  164. a b
  165. 2 7
  166. insert into t3 values (6),(7),(3);
  167. select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1);
  168. a b
  169. 1 7
  170. 2 7
  171. (select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1)) union (select * from t4 order by a limit 2) limit 3;
  172. a b
  173. 1 7
  174. 2 7
  175. 3 8
  176. (select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1)) union (select * from t4 where t4.b=(select max(t2.a)*4 from t2) order by a);
  177. a b
  178. 1 7
  179. 2 7
  180. 4 8
  181. 3 8
  182. explain extended (select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1)) union (select * from t4 where t4.b=(select max(t2.a)*4 from t2) order by a);
  183. id select_type table type possible_keys key key_len ref rows filtered Extra
  184. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
  185. 2 SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using filesort
  186. 3 UNION t4 ALL NULL NULL NULL NULL 3 100.00 Using where
  187. 4 SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00
  188. NULL UNION RESULT <union1,3> ALL NULL NULL NULL NULL NULL NULL
  189. Warnings:
  190. Note 1003 (select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t2` where (`test`.`t2`.`b` = (select `test`.`t3`.`a` from `test`.`t3` order by 1 desc limit 1))) union (select `test`.`t4`.`a` AS `a`,`test`.`t4`.`b` AS `b` from `test`.`t4` where (`test`.`t4`.`b` = (select (max(`test`.`t2`.`a`) * 4) from `test`.`t2`)) order by `a`)
  191. select (select a from t3 where a<t2.a*4 order by 1 desc limit 1), a from t2;
  192. (select a from t3 where a<t2.a*4 order by 1 desc limit 1) a
  193. 3 1
  194. 7 2
  195. select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from
  196. (select * from t2 where a>1) as tt;
  197. (select t3.a from t3 where a<8 order by 1 desc limit 1) a
  198. 7 2
  199. set @tmp_optimizer_switch=@@optimizer_switch;
  200. set optimizer_switch='derived_merge=off,derived_with_keys=off';
  201. explain extended select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from
  202. (select * from t2 where a>1) as tt;
  203. id select_type table type possible_keys key key_len ref rows filtered Extra
  204. 1 PRIMARY <derived3> ALL NULL NULL NULL NULL 2 100.00
  205. 3 DERIVED t2 ALL NULL NULL NULL NULL 2 100.00 Using where
  206. 2 SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using filesort
  207. Warnings:
  208. Note 1003 select (select `test`.`t3`.`a` from `test`.`t3` where (`test`.`t3`.`a` < 8) order by 1 desc limit 1) AS `(select t3.a from t3 where a<8 order by 1 desc limit 1)`,`tt`.`a` AS `a` from (select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t2` where (`test`.`t2`.`a` > 1)) `tt`
  209. set optimizer_switch=@tmp_optimizer_switch;
  210. select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3) order by 1 desc limit 1);
  211. a
  212. 2
  213. select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3 where t3.a > t1.a) order by 1 desc limit 1);
  214. a
  215. 2
  216. select * from t1 where t1.a=(select t2.a from t2 where t2.b=(select max(a) from t3 where t3.a < t1.a) order by 1 desc limit 1);
  217. a
  218. select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
  219. b (select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)
  220. 8 7.5000
  221. 8 4.5000
  222. 9 7.5000
  223. explain extended select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
  224. id select_type table type possible_keys key key_len ref rows filtered Extra
  225. 1 PRIMARY t4 ALL NULL NULL NULL NULL 3 100.00
  226. 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00
  227. 3 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
  228. Warnings:
  229. Note 1276 Field or reference 'test.t4.a' of SELECT #3 was resolved in SELECT #1
  230. Note 1003 select `test`.`t4`.`b` AS `b`,<expr_cache><`test`.`t4`.`a`>((select avg((`test`.`t2`.`a` + (select min(`test`.`t3`.`a`) from `test`.`t3` where (`test`.`t3`.`a` >= `test`.`t4`.`a`)))) from `test`.`t2`)) AS `(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)` from `test`.`t4`
  231. select * from t3 where exists (select * from t2 where t2.b=t3.a);
  232. a
  233. 7
  234. select * from t3 where not exists (select * from t2 where t2.b=t3.a);
  235. a
  236. 6
  237. 3
  238. select * from t3 where a in (select b from t2);
  239. a
  240. 7
  241. select * from t3 where a not in (select b from t2);
  242. a
  243. 6
  244. 3
  245. select * from t3 where a = some (select b from t2);
  246. a
  247. 7
  248. select * from t3 where a <> any (select b from t2);
  249. a
  250. 6
  251. 3
  252. select * from t3 where a = all (select b from t2);
  253. a
  254. 7
  255. select * from t3 where a <> all (select b from t2);
  256. a
  257. 6
  258. 3
  259. insert into t2 values (100, 5);
  260. select * from t3 where a < any (select b from t2);
  261. a
  262. 6
  263. 3
  264. select * from t3 where a < all (select b from t2);
  265. a
  266. 3
  267. select * from t3 where a >= any (select b from t2);
  268. a
  269. 6
  270. 7
  271. explain extended select * from t3 where a >= any (select b from t2);
  272. id select_type table type possible_keys key key_len ref rows filtered Extra
  273. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
  274. 2 SUBQUERY t2 ALL NULL NULL NULL NULL 3 100.00
  275. Warnings:
  276. Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(`test`.`t3`.`a`,((select min(`test`.`t2`.`b`) from `test`.`t2`) <= <cache>(`test`.`t3`.`a`))))
  277. select * from t3 where a >= all (select b from t2);
  278. a
  279. 7
  280. delete from t2 where a=100;
  281. select * from t3 where a in (select a,b from t2);
  282. ERROR 21000: Operand should contain 1 column(s)
  283. select * from t3 where a in (select * from t2);
  284. ERROR 21000: Operand should contain 1 column(s)
  285. insert into t4 values (12,7),(1,7),(10,9),(9,6),(7,6),(3,9),(1,10);
  286. select b,max(a) as ma from t4 group by b having b < (select max(t2.a) from t2 where t2.b=t4.b);
  287. b ma
  288. insert into t2 values (2,10);
  289. select b,max(a) as ma from t4 group by b having ma < (select max(t2.a) from t2 where t2.b=t4.b);
  290. b ma
  291. 10 1
  292. delete from t2 where a=2 and b=10;
  293. select b,max(a) as ma from t4 group by b having b >= (select max(t2.a) from t2 where t2.b=t4.b);
  294. b ma
  295. 7 12
  296. create table t5 (a int);
  297. select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
  298. (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
  299. NULL 1
  300. 2 2
  301. insert into t5 values (5);
  302. select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
  303. (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
  304. NULL 1
  305. 2 2
  306. insert into t5 values (2);
  307. select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
  308. (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
  309. NULL 1
  310. 2 2
  311. explain extended select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
  312. id select_type table type possible_keys key key_len ref rows filtered Extra
  313. 1 PRIMARY t2 ALL NULL NULL NULL NULL # 100.00
  314. 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL # 100.00 Using where
  315. 3 DEPENDENT UNION t5 ALL NULL NULL NULL NULL # 100.00 Using where
  316. NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL # NULL
  317. Warnings:
  318. Note 1276 Field or reference 'test.t2.a' of SELECT #2 was resolved in SELECT #1
  319. Note 1276 Field or reference 'test.t2.a' of SELECT #3 was resolved in SELECT #1
  320. Note 1003 select <expr_cache><`test`.`t2`.`a`>((select `test`.`t1`.`a` from `test`.`t1` where (`test`.`t1`.`a` = `test`.`t2`.`a`) union select `test`.`t5`.`a` from `test`.`t5` where (`test`.`t5`.`a` = `test`.`t2`.`a`))) AS `(select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a)`,`test`.`t2`.`a` AS `a` from `test`.`t2`
  321. select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2;
  322. ERROR 21000: Subquery returns more than 1 row
  323. create table t6 (patient_uq int, clinic_uq int, index i1 (clinic_uq));
  324. create table t7( uq int primary key, name char(25));
  325. insert into t7 values(1,"Oblastnaia bolnitsa"),(2,"Bolnitsa Krasnogo Kresta");
  326. insert into t6 values (1,1),(1,2),(2,2),(1,3);
  327. select * from t6 where exists (select * from t7 where uq = clinic_uq);
  328. patient_uq clinic_uq
  329. 1 1
  330. 1 2
  331. 2 2
  332. explain extended select * from t6 where exists (select * from t7 where uq = clinic_uq);
  333. id select_type table type possible_keys key key_len ref rows filtered Extra
  334. 1 PRIMARY t6 ALL NULL NULL NULL NULL 4 100.00 Using where
  335. 2 DEPENDENT SUBQUERY t7 eq_ref PRIMARY PRIMARY 4 test.t6.clinic_uq 1 100.00 Using index
  336. Warnings:
  337. Note 1276 Field or reference 'test.t6.clinic_uq' of SELECT #2 was resolved in SELECT #1
  338. Note 1003 select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t6` where <expr_cache><`test`.`t6`.`clinic_uq`>(exists(select 1 from `test`.`t7` where (`test`.`t7`.`uq` = `test`.`t6`.`clinic_uq`)))
  339. select * from t1 where a= (select a from t2,t4 where t2.b=t4.b);
  340. ERROR 23000: Column 'a' in field list is ambiguous
  341. drop table t1,t2,t3;
  342. CREATE TABLE t3 (a varchar(20),b char(1) NOT NULL default '0');
  343. INSERT INTO t3 VALUES ('W','a'),('A','c'),('J','b');
  344. CREATE TABLE t2 (a varchar(20),b int NOT NULL default '0');
  345. INSERT INTO t2 VALUES ('W','1'),('A','3'),('J','2');
  346. CREATE TABLE t1 (a varchar(20),b date NOT NULL default '0000-00-00');
  347. INSERT INTO t1 VALUES ('W','1732-02-22'),('A','1735-10-30'),('J','1743-04-13');
  348. SELECT * FROM t1 WHERE b = (SELECT MIN(b) FROM t1);
  349. a b
  350. W 1732-02-22
  351. SELECT * FROM t2 WHERE b = (SELECT MIN(b) FROM t2);
  352. a b
  353. W 1
  354. SELECT * FROM t3 WHERE b = (SELECT MIN(b) FROM t3);
  355. a b
  356. W a
  357. CREATE TABLE `t8` (
  358. `pseudo` varchar(35) character set latin1 NOT NULL default '',
  359. `email` varchar(60) character set latin1 NOT NULL default '',
  360. PRIMARY KEY (`pseudo`),
  361. UNIQUE KEY `email` (`email`)
  362. ) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
  363. INSERT INTO t8 (pseudo,email) VALUES ('joce','test');
  364. INSERT INTO t8 (pseudo,email) VALUES ('joce1','test1');
  365. INSERT INTO t8 (pseudo,email) VALUES ('2joce1','2test1');
  366. EXPLAIN EXTENDED SELECT pseudo,(SELECT email FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce')) FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce');
  367. id select_type table type possible_keys key key_len ref rows filtered Extra
  368. 1 PRIMARY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using where; Using index
  369. 4 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using index
  370. 2 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using where
  371. 3 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using index
  372. Warnings:
  373. Note 1003 select `test`.`t8`.`pseudo` AS `pseudo`,(select `test`.`t8`.`email` from `test`.`t8` where (`test`.`t8`.`pseudo` = (select 'joce' from `test`.`t8` where 1))) AS `(SELECT email FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce'))` from `test`.`t8` where (`test`.`t8`.`pseudo` = (select 'joce' from `test`.`t8` where 1))
  374. SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo,email FROM
  375. t8 WHERE pseudo='joce');
  376. ERROR 21000: Operand should contain 1 column(s)
  377. SELECT pseudo FROM t8 WHERE pseudo=(SELECT * FROM t8 WHERE
  378. pseudo='joce');
  379. ERROR 21000: Operand should contain 1 column(s)
  380. SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce');
  381. pseudo
  382. joce
  383. SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo LIKE '%joce%');
  384. ERROR 21000: Subquery returns more than 1 row
  385. drop table if exists t1,t2,t3,t4,t5,t6,t7,t8;
  386. CREATE TABLE `t1` (
  387. `topic` mediumint(8) unsigned NOT NULL default '0',
  388. `date` date NOT NULL default '0000-00-00',
  389. `pseudo` varchar(35) character set latin1 NOT NULL default '',
  390. PRIMARY KEY (`pseudo`,`date`,`topic`),
  391. KEY `topic` (`topic`)
  392. ) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
  393. INSERT INTO t1 (topic,date,pseudo) VALUES
  394. ('43506','2002-10-02','joce'),('40143','2002-08-03','joce');
  395. EXPLAIN EXTENDED SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
  396. id select_type table type possible_keys key key_len ref rows filtered Extra
  397. 1 SIMPLE t1 index NULL PRIMARY 43 NULL 2 100.00 Using where; Using index
  398. Warnings:
  399. Note 1003 select distinct `test`.`t1`.`date` AS `date` from `test`.`t1` where (`test`.`t1`.`date` = '2002-08-03')
  400. EXPLAIN EXTENDED SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
  401. id select_type table type possible_keys key key_len ref rows filtered Extra
  402. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  403. 2 SUBQUERY t1 index NULL PRIMARY 43 NULL 2 100.00 Using where; Using index
  404. Warnings:
  405. Note 1003 select (select distinct `test`.`t1`.`date` from `test`.`t1` where (`test`.`t1`.`date` = '2002-08-03')) AS `(SELECT DISTINCT date FROM t1 WHERE date='2002-08-03')`
  406. SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
  407. date
  408. 2002-08-03
  409. SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
  410. (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03')
  411. 2002-08-03
  412. SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1) UNION ALL SELECT 1;
  413. 1
  414. 1
  415. 1
  416. 1
  417. SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1) UNION SELECT 1;
  418. ERROR 21000: Subquery returns more than 1 row
  419. EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1);
  420. id select_type table type possible_keys key key_len ref rows filtered Extra
  421. 1 PRIMARY t1 index NULL topic 3 NULL 2 100.00 Using index
  422. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  423. 3 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  424. NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
  425. Warnings:
  426. Note 1003 select 1 AS `1` from `test`.`t1` where (1 = (select 1 union select 1))
  427. drop table t1;
  428. CREATE TABLE `t1` (
  429. `numeropost` mediumint(8) unsigned NOT NULL auto_increment,
  430. `maxnumrep` int(10) unsigned NOT NULL default '0',
  431. PRIMARY KEY (`numeropost`),
  432. UNIQUE KEY `maxnumrep` (`maxnumrep`)
  433. ) ENGINE=MyISAM ROW_FORMAT=FIXED;
  434. INSERT INTO t1 (numeropost,maxnumrep) VALUES (40143,1),(43506,2);
  435. CREATE TABLE `t2` (
  436. `mot` varchar(30) NOT NULL default '',
  437. `topic` mediumint(8) unsigned NOT NULL default '0',
  438. `date` date NOT NULL default '0000-00-00',
  439. `pseudo` varchar(35) NOT NULL default '',
  440. PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`)
  441. ) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
  442. INSERT INTO t2 (mot,topic,date,pseudo) VALUES ('joce','40143','2002-10-22','joce'), ('joce','43506','2002-10-22','joce');
  443. select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
  444. a
  445. 40143
  446. SELECT numeropost,maxnumrep FROM t1 WHERE exists (SELECT 1 FROM t2 WHERE (mot='joce') AND date >= '2002-10-21' AND t1.numeropost = t2.topic) ORDER BY maxnumrep DESC LIMIT 0, 20;
  447. numeropost maxnumrep
  448. 43506 2
  449. 40143 1
  450. SELECT (SELECT 1) as a FROM (SELECT 1 FROM t1 HAVING a=1) b;
  451. ERROR 42S22: Unknown column 'a' in 'having clause'
  452. SELECT 1 IN (SELECT 1 FROM t2 HAVING a);
  453. ERROR 42S22: Unknown column 'a' in 'having clause'
  454. SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic);
  455. mot topic date pseudo
  456. joce 40143 2002-10-22 joce
  457. joce 43506 2002-10-22 joce
  458. SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
  459. mot topic date pseudo
  460. SELECT * from t2 where topic IN (SELECT SUM(topic) FROM t1);
  461. mot topic date pseudo
  462. SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic);
  463. mot topic date pseudo
  464. joce 40143 2002-10-22 joce
  465. joce 43506 2002-10-22 joce
  466. SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
  467. mot topic date pseudo
  468. SELECT * from t2 where topic = any (SELECT SUM(topic) FROM t1);
  469. mot topic date pseudo
  470. SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic);
  471. mot topic date pseudo
  472. SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
  473. mot topic date pseudo
  474. joce 40143 2002-10-22 joce
  475. joce 43506 2002-10-22 joce
  476. SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) from t2;
  477. mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100)
  478. joce 40143 2002-10-22 joce 1
  479. joce 43506 2002-10-22 joce 1
  480. SELECT * from t2 where topic = all (SELECT SUM(topic) FROM t2);
  481. mot topic date pseudo
  482. SELECT * from t2 where topic <> any (SELECT SUM(topic) FROM t2);
  483. mot topic date pseudo
  484. joce 40143 2002-10-22 joce
  485. joce 43506 2002-10-22 joce
  486. SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
  487. mot topic date pseudo
  488. joce 40143 2002-10-22 joce
  489. SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
  490. mot topic date pseudo
  491. joce 40143 2002-10-22 joce
  492. SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
  493. mot topic date pseudo
  494. joce 40143 2002-10-22 joce
  495. SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) from t2;
  496. mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000)
  497. joce 40143 2002-10-22 joce 1
  498. joce 43506 2002-10-22 joce 0
  499. drop table t1,t2;
  500. CREATE TABLE `t1` (
  501. `numeropost` mediumint(8) unsigned NOT NULL auto_increment,
  502. `maxnumrep` int(10) unsigned NOT NULL default '0',
  503. PRIMARY KEY (`numeropost`),
  504. UNIQUE KEY `maxnumrep` (`maxnumrep`)
  505. ) ENGINE=MyISAM ROW_FORMAT=FIXED;
  506. INSERT INTO t1 (numeropost,maxnumrep) VALUES (1,0),(2,1);
  507. select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
  508. ERROR 21000: Subquery returns more than 1 row
  509. select numeropost as a FROM t1 ORDER BY (SELECT 1 FROM t1 HAVING a=1);
  510. ERROR 21000: Subquery returns more than 1 row
  511. drop table t1;
  512. create table t1 (a int);
  513. insert into t1 values (1),(2),(3);
  514. (select * from t1) union (select * from t1) order by (select a from t1 limit 1);
  515. a
  516. 1
  517. 2
  518. 3
  519. drop table t1;
  520. CREATE TABLE t1 (field char(1) NOT NULL DEFAULT 'b');
  521. INSERT INTO t1 VALUES ();
  522. SELECT field FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1 FROM (SELECT 1) a HAVING field='b');
  523. ERROR 21000: Subquery returns more than 1 row
  524. drop table t1;
  525. CREATE TABLE `t1` (
  526. `numeropost` mediumint(8) unsigned NOT NULL default '0',
  527. `numreponse` int(10) unsigned NOT NULL auto_increment,
  528. `pseudo` varchar(35) NOT NULL default '',
  529. PRIMARY KEY (`numeropost`,`numreponse`),
  530. UNIQUE KEY `numreponse` (`numreponse`),
  531. KEY `pseudo` (`pseudo`,`numeropost`)
  532. ) ENGINE=MyISAM;
  533. SELECT (SELECT numeropost FROM t1 HAVING numreponse=a),numreponse FROM (SELECT * FROM t1) as a;
  534. ERROR 42S22: Reference 'numreponse' not supported (forward reference in item list)
  535. SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=a) FROM (SELECT * FROM t1) as a;
  536. ERROR 42S22: Unknown column 'a' in 'having clause'
  537. SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=1) FROM (SELECT * FROM t1) as a;
  538. numreponse (SELECT numeropost FROM t1 HAVING numreponse=1)
  539. INSERT INTO t1 (numeropost,numreponse,pseudo) VALUES (1,1,'joce'),(1,2,'joce'),(1,3,'test');
  540. EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1');
  541. id select_type table type possible_keys key key_len ref rows filtered Extra
  542. 1 PRIMARY t1 const PRIMARY,numreponse PRIMARY 7 const,const 1 100.00 Using where; Using index
  543. 2 SUBQUERY t1 ref PRIMARY PRIMARY 3 const 2 100.00 Using index
  544. Warnings:
  545. Note 1003 select `test`.`t1`.`numreponse` AS `numreponse` from `test`.`t1` where ((`test`.`t1`.`numeropost` = '1') and (`test`.`t1`.`numreponse` = (select 1 from `test`.`t1` where (`test`.`t1`.`numeropost` = '1'))))
  546. SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1');
  547. ERROR 21000: Subquery returns more than 1 row
  548. EXPLAIN EXTENDED SELECT MAX(numreponse) FROM t1 WHERE numeropost='1';
  549. id select_type table type possible_keys key key_len ref rows filtered Extra
  550. 1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
  551. Warnings:
  552. Note 1003 select max(`test`.`t1`.`numreponse`) AS `MAX(numreponse)` from `test`.`t1` where (`test`.`t1`.`numeropost` = '1')
  553. EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT MAX(numreponse) FROM t1 WHERE numeropost='1');
  554. id select_type table type possible_keys key key_len ref rows filtered Extra
  555. 1 PRIMARY t1 const PRIMARY,numreponse PRIMARY 7 const,const 1 100.00 Using where; Using index
  556. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
  557. Warnings:
  558. Note 1003 select `test`.`t1`.`numreponse` AS `numreponse` from `test`.`t1` where ((`test`.`t1`.`numeropost` = '1') and (`test`.`t1`.`numreponse` = (select max(`test`.`t1`.`numreponse`) from `test`.`t1` where (`test`.`t1`.`numeropost` = '1'))))
  559. drop table t1;
  560. CREATE TABLE t1 (a int(1));
  561. INSERT INTO t1 VALUES (1);
  562. SELECT 1 FROM (SELECT a FROM t1) b HAVING (SELECT b.a)=1;
  563. 1
  564. 1
  565. drop table t1;
  566. create table t1 (a int NOT NULL, b int, primary key (a));
  567. create table t2 (a int NOT NULL, b int, primary key (a));
  568. insert into t1 values (0, 10),(1, 11),(2, 12);
  569. insert into t2 values (1, 21),(2, 22),(3, 23);
  570. select * from t1;
  571. a b
  572. 0 10
  573. 1 11
  574. 2 12
  575. update t1 set b= (select b from t1);
  576. ERROR HY000: You can't specify target table 't1' for update in FROM clause
  577. update t1 set b= (select b from t2);
  578. ERROR 21000: Subquery returns more than 1 row
  579. update t1 set b= (select b from t2 where t1.a = t2.a);
  580. select * from t1;
  581. a b
  582. 0 NULL
  583. 1 21
  584. 2 22
  585. drop table t1, t2;
  586. create table t1 (a int NOT NULL, b int, primary key (a));
  587. create table t2 (a int NOT NULL, b int, primary key (a));
  588. insert into t1 values (0, 10),(1, 11),(2, 12);
  589. insert into t2 values (1, 21),(2, 12),(3, 23);
  590. select * from t1;
  591. a b
  592. 0 10
  593. 1 11
  594. 2 12
  595. select * from t1 where b = (select b from t2 where t1.a = t2.a);
  596. a b
  597. 2 12
  598. delete from t1 where b = (select b from t1);
  599. ERROR HY000: You can't specify target table 't1' for update in FROM clause
  600. delete from t1 where b = (select b from t2);
  601. ERROR 21000: Subquery returns more than 1 row
  602. delete from t1 where b = (select b from t2 where t1.a = t2.a);
  603. select * from t1;
  604. a b
  605. 0 10
  606. 1 11
  607. drop table t1, t2;
  608. create table t11 (a int NOT NULL, b int, primary key (a));
  609. create table t12 (a int NOT NULL, b int, primary key (a));
  610. create table t2 (a int NOT NULL, b int, primary key (a));
  611. insert into t11 values (0, 10),(1, 11),(2, 12);
  612. insert into t12 values (33, 10),(22, 11),(2, 12);
  613. insert into t2 values (1, 21),(2, 12),(3, 23);
  614. select * from t11;
  615. a b
  616. 0 10
  617. 1 11
  618. 2 12
  619. select * from t12;
  620. a b
  621. 33 10
  622. 22 11
  623. 2 12
  624. delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
  625. ERROR HY000: You can't specify target table 't12' for update in FROM clause
  626. delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
  627. ERROR 21000: Subquery returns more than 1 row
  628. delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
  629. select * from t11;
  630. a b
  631. 0 10
  632. 1 11
  633. select * from t12;
  634. a b
  635. 33 10
  636. 22 11
  637. drop table t11, t12, t2;
  638. CREATE TABLE t1 (x int);
  639. create table t2 (a int);
  640. create table t3 (b int);
  641. insert into t2 values (1);
  642. insert into t3 values (1),(2);
  643. INSERT INTO t1 (x) VALUES ((SELECT x FROM t1));
  644. ERROR HY000: You can't specify target table 't1' for update in FROM clause
  645. INSERT INTO t1 (x) VALUES ((SELECT b FROM t3));
  646. ERROR 21000: Subquery returns more than 1 row
  647. INSERT INTO t1 (x) VALUES ((SELECT a FROM t2));
  648. select * from t1;
  649. x
  650. 1
  651. insert into t2 values (1);
  652. INSERT INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
  653. select * from t1;
  654. x
  655. 1
  656. 2
  657. INSERT INTO t1 (x) select (SELECT SUM(a)+1 FROM t2) FROM t2;
  658. select * from t1;
  659. x
  660. 1
  661. 2
  662. 3
  663. 3
  664. INSERT INTO t1 (x) select (SELECT SUM(x)+2 FROM t1) FROM t2;
  665. select * from t1;
  666. x
  667. 1
  668. 2
  669. 3
  670. 3
  671. 11
  672. 11
  673. INSERT INTO t1 (x) VALUES ((SELECT SUM(x) FROM t2));
  674. ERROR 42S22: Unknown column 'x' in 'field list'
  675. INSERT INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
  676. select * from t1;
  677. x
  678. 1
  679. 2
  680. 3
  681. 3
  682. 11
  683. 11
  684. 2
  685. drop table t1, t2, t3;
  686. CREATE TABLE t1 (x int not null, y int, primary key (x));
  687. create table t2 (a int);
  688. create table t3 (a int);
  689. insert into t2 values (1);
  690. insert into t3 values (1),(2);
  691. select * from t1;
  692. x y
  693. replace into t1 (x, y) VALUES ((SELECT x FROM t1), (SELECT a+1 FROM t2));
  694. ERROR HY000: You can't specify target table 't1' for update in FROM clause
  695. replace into t1 (x, y) VALUES ((SELECT a FROM t3), (SELECT a+1 FROM t2));
  696. ERROR 21000: Subquery returns more than 1 row
  697. replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+1 FROM t2));
  698. select * from t1;
  699. x y
  700. 1 2
  701. replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+2 FROM t2));
  702. select * from t1;
  703. x y
  704. 1 3
  705. replace into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a FROM t2));
  706. select * from t1 order by x;
  707. x y
  708. 1 3
  709. 4 1
  710. replace into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a+1 FROM t2));
  711. select * from t1 order by x;
  712. x y
  713. 1 3
  714. 4 2
  715. replace LOW_PRIORITY into t1 (x, y) VALUES ((SELECT a+1 FROM t2), (SELECT a FROM t2));
  716. select * from t1 order by x;
  717. x y
  718. 1 3
  719. 2 1
  720. 4 2
  721. drop table t1, t2, t3;
  722. SELECT * FROM (SELECT 1) b WHERE 1 IN (SELECT *);
  723. ERROR HY000: No tables used
  724. CREATE TABLE t2 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;
  725. INSERT INTO t2 VALUES (1),(2);
  726. SELECT * FROM t2 WHERE id IN (SELECT 1);
  727. id
  728. 1
  729. EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1);
  730. id select_type table type possible_keys key key_len ref rows filtered Extra
  731. 1 PRIMARY t2 ref id id 5 const 1 100.00 Using index
  732. Warnings:
  733. Note 1249 Select 2 was reduced during optimization
  734. Note 1003 select `test`.`t2`.`id` AS `id` from `test`.`t2` where (`test`.`t2`.`id` = 1)
  735. SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);
  736. id
  737. 1
  738. SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));
  739. id
  740. 2
  741. EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));
  742. id select_type table type possible_keys key key_len ref rows filtered Extra
  743. 1 PRIMARY t2 ref id id 5 const 1 100.00 Using index
  744. Warnings:
  745. Note 1249 Select 3 was reduced during optimization
  746. Note 1249 Select 2 was reduced during optimization
  747. Note 1003 select `test`.`t2`.`id` AS `id` from `test`.`t2` where (`test`.`t2`.`id` = (1 + 1))
  748. EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);
  749. id select_type table type possible_keys key key_len ref rows filtered Extra
  750. 1 PRIMARY t2 index NULL id 5 NULL 2 100.00 Using where; Using index
  751. 2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  752. 3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  753. NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
  754. Warnings:
  755. Note 1003 select `test`.`t2`.`id` AS `id` from `test`.`t2` where <expr_cache><`test`.`t2`.`id`>(<in_optimizer>(`test`.`t2`.`id`,<exists>(select 1 having (<cache>(`test`.`t2`.`id`) = <ref_null_helper>(1)) union select 3 having (<cache>(`test`.`t2`.`id`) = <ref_null_helper>(3)))))
  756. SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 3);
  757. id
  758. SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 2);
  759. id
  760. 2
  761. INSERT INTO t2 VALUES ((SELECT * FROM t2));
  762. ERROR HY000: You can't specify target table 't2' for update in FROM clause
  763. INSERT INTO t2 VALUES ((SELECT id FROM t2));
  764. ERROR HY000: You can't specify target table 't2' for update in FROM clause
  765. SELECT * FROM t2;
  766. id
  767. 1
  768. 2
  769. CREATE TABLE t1 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;
  770. INSERT INTO t1 values (1),(1);
  771. UPDATE t2 SET id=(SELECT * FROM t1);
  772. ERROR 21000: Subquery returns more than 1 row
  773. drop table t2, t1;
  774. create table t1 (a int);
  775. insert into t1 values (1),(2),(3);
  776. select 1 IN (SELECT * from t1);
  777. 1 IN (SELECT * from t1)
  778. 1
  779. select 10 IN (SELECT * from t1);
  780. 10 IN (SELECT * from t1)
  781. 0
  782. select NULL IN (SELECT * from t1);
  783. NULL IN (SELECT * from t1)
  784. NULL
  785. update t1 set a=NULL where a=2;
  786. select 1 IN (SELECT * from t1);
  787. 1 IN (SELECT * from t1)
  788. 1
  789. select 3 IN (SELECT * from t1);
  790. 3 IN (SELECT * from t1)
  791. 1
  792. select 10 IN (SELECT * from t1);
  793. 10 IN (SELECT * from t1)
  794. NULL
  795. select 1 > ALL (SELECT * from t1);
  796. 1 > ALL (SELECT * from t1)
  797. 0
  798. select 10 > ALL (SELECT * from t1);
  799. 10 > ALL (SELECT * from t1)
  800. NULL
  801. select 1 > ANY (SELECT * from t1);
  802. 1 > ANY (SELECT * from t1)
  803. NULL
  804. select 10 > ANY (SELECT * from t1);
  805. 10 > ANY (SELECT * from t1)
  806. 1
  807. drop table t1;
  808. create table t1 (a varchar(20));
  809. insert into t1 values ('A'),('BC'),('DEF');
  810. select 'A' IN (SELECT * from t1);
  811. 'A' IN (SELECT * from t1)
  812. 1
  813. select 'XYZS' IN (SELECT * from t1);
  814. 'XYZS' IN (SELECT * from t1)
  815. 0
  816. select NULL IN (SELECT * from t1);
  817. NULL IN (SELECT * from t1)
  818. NULL
  819. update t1 set a=NULL where a='BC';
  820. select 'A' IN (SELECT * from t1);
  821. 'A' IN (SELECT * from t1)
  822. 1
  823. select 'DEF' IN (SELECT * from t1);
  824. 'DEF' IN (SELECT * from t1)
  825. 1
  826. select 'XYZS' IN (SELECT * from t1);
  827. 'XYZS' IN (SELECT * from t1)
  828. NULL
  829. select 'A' > ALL (SELECT * from t1);
  830. 'A' > ALL (SELECT * from t1)
  831. 0
  832. select 'XYZS' > ALL (SELECT * from t1);
  833. 'XYZS' > ALL (SELECT * from t1)
  834. NULL
  835. select 'A' > ANY (SELECT * from t1);
  836. 'A' > ANY (SELECT * from t1)
  837. NULL
  838. select 'XYZS' > ANY (SELECT * from t1);
  839. 'XYZS' > ANY (SELECT * from t1)
  840. 1
  841. drop table t1;
  842. create table t1 (a float);
  843. insert into t1 values (1.5),(2.5),(3.5);
  844. select 1.5 IN (SELECT * from t1);
  845. 1.5 IN (SELECT * from t1)
  846. 1
  847. select 10.5 IN (SELECT * from t1);
  848. 10.5 IN (SELECT * from t1)
  849. 0
  850. select NULL IN (SELECT * from t1);
  851. NULL IN (SELECT * from t1)
  852. NULL
  853. update t1 set a=NULL where a=2.5;
  854. select 1.5 IN (SELECT * from t1);
  855. 1.5 IN (SELECT * from t1)
  856. 1
  857. select 3.5 IN (SELECT * from t1);
  858. 3.5 IN (SELECT * from t1)
  859. 1
  860. select 10.5 IN (SELECT * from t1);
  861. 10.5 IN (SELECT * from t1)
  862. NULL
  863. select 1.5 > ALL (SELECT * from t1);
  864. 1.5 > ALL (SELECT * from t1)
  865. 0
  866. select 10.5 > ALL (SELECT * from t1);
  867. 10.5 > ALL (SELECT * from t1)
  868. NULL
  869. select 1.5 > ANY (SELECT * from t1);
  870. 1.5 > ANY (SELECT * from t1)
  871. NULL
  872. select 10.5 > ANY (SELECT * from t1);
  873. 10.5 > ANY (SELECT * from t1)
  874. 1
  875. explain extended select (select a+1) from t1;
  876. id select_type table type possible_keys key key_len ref rows filtered Extra
  877. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
  878. Warnings:
  879. Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
  880. Note 1249 Select 2 was reduced during optimization
  881. Note 1003 select (`test`.`t1`.`a` + 1) AS `(select a+1)` from `test`.`t1`
  882. select (select a+1) from t1;
  883. (select a+1)
  884. 2.5
  885. 4.5
  886. NULL
  887. drop table t1;
  888. set @save_optimizer_switch=@@optimizer_switch;
  889. set @@optimizer_switch="partial_match_rowid_merge=off,partial_match_table_scan=off";
  890. CREATE TABLE t1 (a int(11) NOT NULL default '0', PRIMARY KEY (a));
  891. CREATE TABLE t2 (a int(11) default '0', INDEX (a));
  892. INSERT INTO t1 VALUES (1),(2),(3),(4);
  893. INSERT INTO t2 VALUES (1),(2),(3);
  894. SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
  895. a t1.a in (select t2.a from t2)
  896. 1 1
  897. 2 1
  898. 3 1
  899. 4 0
  900. explain extended SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
  901. id select_type table type possible_keys key key_len ref rows filtered Extra
  902. 1 PRIMARY t1 index NULL PRIMARY 4 NULL 4 100.00 Using index
  903. 2 DEPENDENT SUBQUERY t2 index_subquery a a 5 func 2 100.00 Using index
  904. Warnings:
  905. Note 1003 select `test`.`t1`.`a` AS `a`,<expr_cache><`test`.`t1`.`a`>(<in_optimizer>(`test`.`t1`.`a`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`a`) in t2 on a checking NULL having <is_not_null_test>(`test`.`t2`.`a`))))) AS `t1.a in (select t2.a from t2)` from `test`.`t1`
  906. CREATE TABLE t3 (a int(11) default '0');
  907. INSERT INTO t3 VALUES (1),(2),(3);
  908. SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
  909. a t1.a in (select t2.a from t2,t3 where t3.a=t2.a)
  910. 1 1
  911. 2 1
  912. 3 1
  913. 4 0
  914. explain extended SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
  915. id select_type table type possible_keys key key_len ref rows filtered Extra
  916. 1 PRIMARY t1 index NULL PRIMARY 4 NULL 4 100.00 Using index
  917. 2 DEPENDENT SUBQUERY t2 ref_or_null a a 5 func 2 100.00 Using where; Using index
  918. 2 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join)
  919. Warnings:
  920. Note 1003 select `test`.`t1`.`a` AS `a`,<expr_cache><`test`.`t1`.`a`>(<in_optimizer>(`test`.`t1`.`a`,<exists>(select `test`.`t2`.`a` from `test`.`t2` join `test`.`t3` where ((`test`.`t3`.`a` = `test`.`t2`.`a`) and ((<cache>(`test`.`t1`.`a`) = `test`.`t2`.`a`) or isnull(`test`.`t2`.`a`))) having <is_not_null_test>(`test`.`t2`.`a`)))) AS `t1.a in (select t2.a from t2,t3 where t3.a=t2.a)` from `test`.`t1`
  921. drop table t1,t2,t3;
  922. create table t1 (a float);
  923. select 10.5 IN (SELECT * from t1 LIMIT 1);
  924. ERROR 42000: This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
  925. select 10.5 IN (SELECT * from t1 LIMIT 1 UNION SELECT 1.5);
  926. ERROR 42000: This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
  927. drop table t1;
  928. create table t1 (a int, b int, c varchar(10));
  929. create table t2 (a int);
  930. insert into t1 values (1,2,'a'),(2,3,'b'),(3,4,'c');
  931. insert into t2 values (1),(2),(NULL);
  932. select a, (select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a'),(select c from t1 where a=t2.a) from t2;
  933. a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a') (select c from t1 where a=t2.a)
  934. 1 1 a
  935. 2 0 b
  936. NULL NULL NULL
  937. select a, (select a,b,c from t1 where t1.a=t2.a) = ROW(a,3,'b'),(select c from t1 where a=t2.a) from t2;
  938. a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,3,'b') (select c from t1 where a=t2.a)
  939. 1 0 a
  940. 2 1 b
  941. NULL NULL NULL
  942. select a, (select a,b,c from t1 where t1.a=t2.a) = ROW(a,4,'c'),(select c from t1 where a=t2.a) from t2;
  943. a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,4,'c') (select c from t1 where a=t2.a)
  944. 1 0 a
  945. 2 0 b
  946. NULL NULL NULL
  947. drop table t1,t2;
  948. create table t1 (a int, b real, c varchar(10));
  949. insert into t1 values (1, 1, 'a'), (2,2,'b'), (NULL, 2, 'b');
  950. select ROW(1, 1, 'a') IN (select a,b,c from t1);
  951. ROW(1, 1, 'a') IN (select a,b,c from t1)
  952. 1
  953. select ROW(1, 2, 'a') IN (select a,b,c from t1);
  954. ROW(1, 2, 'a') IN (select a,b,c from t1)
  955. 0
  956. select ROW(1, 1, 'a') IN (select b,a,c from t1);
  957. ROW(1, 1, 'a') IN (select b,a,c from t1)
  958. 1
  959. select ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null);
  960. ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null)
  961. 1
  962. select ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null);
  963. ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null)
  964. 0
  965. select ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null);
  966. ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null)
  967. 1
  968. select ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a');
  969. ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a')
  970. 1
  971. select ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a');
  972. ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a')
  973. 0
  974. select ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a');
  975. ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a')
  976. 1
  977. select ROW(1, 1, 'a') IN (select b,a,c from t1 limit 2);
  978. ERROR 42000: This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
  979. drop table t1;
  980. create table t1 (a int);
  981. insert into t1 values (1);
  982. do @a:=(SELECT a from t1);
  983. select @a;
  984. @a
  985. 1
  986. set @a:=2;
  987. set @a:=(SELECT a from t1);
  988. select @a;
  989. @a
  990. 1
  991. drop table t1;
  992. do (SELECT a from t1);
  993. ERROR 42S02: Table 'test.t1' doesn't exist
  994. set @a:=(SELECT a from t1);
  995. ERROR 42S02: Table 'test.t1' doesn't exist
  996. CREATE TABLE t1 (a int, KEY(a));
  997. HANDLER t1 OPEN;
  998. HANDLER t1 READ a=((SELECT 1));
  999. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT 1))' at line 1
  1000. HANDLER t1 CLOSE;
  1001. drop table t1;
  1002. create table t1 (a int);
  1003. create table t2 (b int);
  1004. insert into t1 values (1),(2);
  1005. insert into t2 values (1);
  1006. select a from t1 where a in (select a from t1 where a in (select b from t2));
  1007. a
  1008. 1
  1009. drop table t1, t2;
  1010. create table t1 (a int, b int);
  1011. create table t2 like t1;
  1012. insert into t1 values (1,2),(1,3),(1,4),(1,5);
  1013. insert into t2 values (1,2),(1,3);
  1014. select * from t1 where row(a,b) in (select a,b from t2);
  1015. a b
  1016. 1 2
  1017. 1 3
  1018. drop table t1, t2;
  1019. CREATE TABLE `t1` (`i` int(11) NOT NULL default '0',PRIMARY KEY (`i`)) ENGINE=MyISAM CHARSET=latin1;
  1020. INSERT INTO t1 VALUES (1);
  1021. UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));
  1022. select * from t1;
  1023. i
  1024. 2
  1025. drop table t1;
  1026. CREATE TABLE t1 (a int(1));
  1027. EXPLAIN EXTENDED SELECT (SELECT RAND() FROM t1) FROM t1;
  1028. id select_type table type possible_keys key key_len ref rows filtered Extra
  1029. 1 PRIMARY t1 ALL NULL NULL NULL NULL 0 0.00
  1030. 2 UNCACHEABLE SUBQUERY t1 ALL NULL NULL NULL NULL 0 0.00
  1031. Warnings:
  1032. Note 1003 select (select rand() from `test`.`t1`) AS `(SELECT RAND() FROM t1)` from `test`.`t1`
  1033. EXPLAIN EXTENDED SELECT (SELECT ENCRYPT('test') FROM t1) FROM t1;
  1034. id select_type table type possible_keys key key_len ref rows filtered Extra
  1035. 1 PRIMARY t1 ALL NULL NULL NULL NULL 0 0.00
  1036. 2 UNCACHEABLE SUBQUERY t1 ALL NULL NULL NULL NULL 0 0.00
  1037. Warnings:
  1038. Note 1003 select (select encrypt('test') from `test`.`t1`) AS `(SELECT ENCRYPT('test') FROM t1)` from `test`.`t1`
  1039. EXPLAIN EXTENDED SELECT (SELECT BENCHMARK(1,1) FROM t1) FROM t1;
  1040. id select_type table type possible_keys key key_len ref rows filtered Extra
  1041. 1 PRIMARY t1 ALL NULL NULL NULL NULL 0 0.00
  1042. 2 UNCACHEABLE SUBQUERY t1 ALL NULL NULL NULL NULL 0 0.00
  1043. Warnings:
  1044. Note 1003 select (select benchmark(1,1) from `test`.`t1`) AS `(SELECT BENCHMARK(1,1) FROM t1)` from `test`.`t1`
  1045. drop table t1;
  1046. CREATE TABLE `t1` (
  1047. `mot` varchar(30) character set latin1 NOT NULL default '',
  1048. `topic` mediumint(8) unsigned NOT NULL default '0',
  1049. `date` date NOT NULL default '0000-00-00',
  1050. `pseudo` varchar(35) character set latin1 NOT NULL default '',
  1051. PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`),
  1052. KEY `pseudo` (`pseudo`,`date`,`topic`),
  1053. KEY `topic` (`topic`)
  1054. ) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
  1055. CREATE TABLE `t2` (
  1056. `mot` varchar(30) character set latin1 NOT NULL default '',
  1057. `topic` mediumint(8) unsigned NOT NULL default '0',
  1058. `date` date NOT NULL default '0000-00-00',
  1059. `pseudo` varchar(35) character set latin1 NOT NULL default '',
  1060. PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`),
  1061. KEY `pseudo` (`pseudo`,`date`,`topic`),
  1062. KEY `topic` (`topic`)
  1063. ) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
  1064. CREATE TABLE `t3` (
  1065. `numeropost` mediumint(8) unsigned NOT NULL auto_increment,
  1066. `maxnumrep` int(10) unsigned NOT NULL default '0',
  1067. PRIMARY KEY (`numeropost`),
  1068. UNIQUE KEY `maxnumrep` (`maxnumrep`)
  1069. ) ENGINE=MyISAM CHARSET=latin1;
  1070. INSERT INTO t1 VALUES ('joce','1','','joce'),('test','2','','test');
  1071. Warnings:
  1072. Warning 1265 Data truncated for column 'date' at row 1
  1073. Warning 1265 Data truncated for column 'date' at row 2
  1074. INSERT INTO t2 VALUES ('joce','1','','joce'),('test','2','','test');
  1075. Warnings:
  1076. Warning 1265 Data truncated for column 'date' at row 1
  1077. Warning 1265 Data truncated for column 'date' at row 2
  1078. INSERT INTO t3 VALUES (1,1);
  1079. SELECT DISTINCT topic FROM t2 WHERE NOT EXISTS(SELECT * FROM t3 WHERE
  1080. numeropost=topic);
  1081. topic
  1082. 2
  1083. select * from t1;
  1084. mot topic date pseudo
  1085. joce 1 0000-00-00 joce
  1086. test 2 0000-00-00 test
  1087. DELETE FROM t1 WHERE topic IN (SELECT DISTINCT topic FROM t2 WHERE NOT
  1088. EXISTS(SELECT * FROM t3 WHERE numeropost=topic));
  1089. select * from t1;
  1090. mot topic date pseudo
  1091. joce 1 0000-00-00 joce
  1092. drop table t1, t2, t3;
  1093. SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
  1094. a (SELECT a)
  1095. 1 1
  1096. CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT 1)) a;
  1097. SHOW CREATE TABLE t1;
  1098. Table Create Table
  1099. t1 CREATE TABLE `t1` (
  1100. `a` int(1) NOT NULL DEFAULT '0',
  1101. `(SELECT 1)` int(1) NOT NULL DEFAULT '0'
  1102. ) ENGINE=PBXT DEFAULT CHARSET=latin1
  1103. drop table t1;
  1104. CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
  1105. SHOW CREATE TABLE t1;
  1106. Table Create Table
  1107. t1 CREATE TABLE `t1` (
  1108. `a` int(1) NOT NULL DEFAULT '0',
  1109. `(SELECT a)` int(1) NOT NULL DEFAULT '0'
  1110. ) ENGINE=PBXT DEFAULT CHARSET=latin1
  1111. drop table t1;
  1112. CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a;
  1113. SHOW CREATE TABLE t1;
  1114. Table Create Table
  1115. t1 CREATE TABLE `t1` (
  1116. `a` int(1) NOT NULL DEFAULT '0',
  1117. `(SELECT a+0)` int(3) NOT NULL DEFAULT '0'
  1118. ) ENGINE=PBXT DEFAULT CHARSET=latin1
  1119. drop table t1;
  1120. CREATE TABLE t1 SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a;
  1121. select * from t1;
  1122. a
  1123. 2
  1124. SHOW CREATE TABLE t1;
  1125. Table Create Table
  1126. t1 CREATE TABLE `t1` (
  1127. `a` bigint(20) NOT NULL DEFAULT '0'
  1128. ) ENGINE=PBXT DEFAULT CHARSET=latin1
  1129. drop table t1;
  1130. create table t1 (a int);
  1131. insert into t1 values (1), (2), (3);
  1132. explain extended select a,(select (select rand() from t1 limit 1) from t1 limit 1)
  1133. from t1;
  1134. id select_type table type possible_keys key key_len ref rows filtered Extra
  1135. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
  1136. 2 UNCACHEABLE SUBQUERY t1 ALL NULL NULL NULL NULL 3 100.00
  1137. 3 UNCACHEABLE SUBQUERY t1 ALL NULL NULL NULL NULL 3 100.00
  1138. Warnings:
  1139. Note 1003 select `test`.`t1`.`a` AS `a`,(select (select rand() from `test`.`t1` limit 1) from `test`.`t1` limit 1) AS `(select (select rand() from t1 limit 1) from t1 limit 1)` from `test`.`t1`
  1140. drop table t1;
  1141. select t1.Continent, t2.Name, t2.Population from t1 LEFT JOIN t2 ON t1.Code = t2.Country where t2.Population IN (select max(t2.Population) AS Population from t2, t1 where t2.Country = t1.Code group by Continent);
  1142. ERROR 42S02: Table 'test.t1' doesn't exist
  1143. CREATE TABLE t1 (
  1144. ID int(11) NOT NULL auto_increment,
  1145. name char(35) NOT NULL default '',
  1146. t2 char(3) NOT NULL default '',
  1147. District char(20) NOT NULL default '',
  1148. Population int(11) NOT NULL default '0',
  1149. PRIMARY KEY (ID)
  1150. ) ENGINE=MyISAM;
  1151. INSERT INTO t1 VALUES (130,'Sydney','AUS','New South Wales',3276207);
  1152. INSERT INTO t1 VALUES (131,'Melbourne','AUS','Victoria',2865329);
  1153. INSERT INTO t1 VALUES (132,'Brisbane','AUS','Queensland',1291117);
  1154. CREATE TABLE t2 (
  1155. Code char(3) NOT NULL default '',
  1156. Name char(52) NOT NULL default '',
  1157. Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
  1158. Region char(26) NOT NULL default '',
  1159. SurfaceArea float(10,2) NOT NULL default '0.00',
  1160. IndepYear smallint(6) default NULL,
  1161. Population int(11) NOT NULL default '0',
  1162. LifeExpectancy float(3,1) default NULL,
  1163. GNP float(10,2) default NULL,
  1164. GNPOld float(10,2) default NULL,
  1165. LocalName char(45) NOT NULL default '',
  1166. GovernmentForm char(45) NOT NULL default '',
  1167. HeadOfState char(60) default NULL,
  1168. Capital int(11) default NULL,
  1169. Code2 char(2) NOT NULL default '',
  1170. PRIMARY KEY (Code)
  1171. ) ENGINE=MyISAM;
  1172. INSERT INTO t2 VALUES ('AUS','Australia','Oceania','Australia and New Zealand',7741220.00,1901,18886000,79.8,351182.00,392911.00,'Australia','Constitutional Monarchy, Federation','Elisabeth II',135,'AU');
  1173. INSERT INTO t2 VALUES ('AZE','Azerbaijan','Asia','Middle East',86600.00,1991,7734000,62.9,4127.00,4100.00,'Azärbaycan','Federal Republic','Heydär Äliyev',144,'AZ');
  1174. select t2.Continent, t1.Name, t1.Population from t2 LEFT JOIN t1 ON t2.Code = t1.t2 where t1.Population IN (select max(t1.Population) AS Population from t1, t2 where t1.t2 = t2.Code group by Continent);
  1175. Continent Name Population
  1176. Oceania Sydney 3276207
  1177. drop table t1, t2;
  1178. CREATE TABLE `t1` (
  1179. `id` mediumint(8) unsigned NOT NULL auto_increment,
  1180. `pseudo` varchar(35) character set latin1 NOT NULL default '',
  1181. PRIMARY KEY (`id`),
  1182. UNIQUE KEY `pseudo` (`pseudo`)
  1183. ) ENGINE=MyISAM PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
  1184. INSERT INTO t1 (pseudo) VALUES ('test');
  1185. SELECT 0 IN (SELECT 1 FROM t1 a);
  1186. 0 IN (SELECT 1 FROM t1 a)
  1187. 0
  1188. EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
  1189. id select_type table type possible_keys key key_len ref rows filtered Extra
  1190. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  1191. 2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  1192. Warnings:
  1193. Note 1003 select <in_optimizer>(0,<exists>(select 1 from `test`.`t1` `a` where (0 = 1))) AS `0 IN (SELECT 1 FROM t1 a)`
  1194. INSERT INTO t1 (pseudo) VALUES ('test1');
  1195. SELECT 0 IN (SELECT 1 FROM t1 a);
  1196. 0 IN (SELECT 1 FROM t1 a)
  1197. 0
  1198. EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
  1199. id select_type table type possible_keys key key_len ref rows filtered Extra
  1200. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  1201. 2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  1202. Warnings:
  1203. Note 1003 select <in_optimizer>(0,<exists>(select 1 from `test`.`t1` `a` where (0 = 1))) AS `0 IN (SELECT 1 FROM t1 a)`
  1204. drop table t1;
  1205. CREATE TABLE `t1` (
  1206. `i` int(11) NOT NULL default '0',
  1207. PRIMARY KEY (`i`)
  1208. ) ENGINE=MyISAM CHARSET=latin1;
  1209. INSERT INTO t1 VALUES (1);
  1210. UPDATE t1 SET i=i+(SELECT MAX(i) FROM (SELECT 1) t) WHERE i=(SELECT MAX(i));
  1211. UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));
  1212. UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t);
  1213. ERROR 42S22: Unknown column 't.i' in 'field list'
  1214. select * from t1;
  1215. i
  1216. 3
  1217. drop table t1;
  1218. CREATE TABLE t1 (
  1219. id int(11) default NULL
  1220. ) ENGINE=MyISAM CHARSET=latin1;
  1221. INSERT INTO t1 VALUES (1),(1),(2),(2),(1),(3);
  1222. CREATE TABLE t2 (
  1223. id int(11) default NULL,
  1224. name varchar(15) default NULL
  1225. ) ENGINE=MyISAM CHARSET=latin1;
  1226. INSERT INTO t2 VALUES (4,'vita'), (1,'vita'), (2,'vita'), (1,'vita');
  1227. update t1, t2 set t2.name='lenka' where t2.id in (select id from t1);
  1228. select * from t2;
  1229. id name
  1230. 4 vita
  1231. 1 lenka
  1232. 2 lenka
  1233. 1 lenka
  1234. drop table t1,t2;
  1235. create table t1 (a int, unique index indexa (a));
  1236. insert into t1 values (-1), (-4), (-2), (NULL);
  1237. select -10 IN (select a from t1 FORCE INDEX (indexa));
  1238. -10 IN (select a from t1 FORCE INDEX (indexa))
  1239. NULL
  1240. drop table t1;
  1241. create table t1 (id int not null auto_increment primary key, salary int, key(salary));
  1242. insert into t1 (salary) values (100),(1000),(10000),(10),(500),(5000),(50000);
  1243. explain extended SELECT id FROM t1 where salary = (SELECT MAX(salary) FROM t1);
  1244. id select_type table type possible_keys key key_len ref rows filtered Extra
  1245. 1 PRIMARY t1 ref salary salary 5 const 0 0.00 Using where
  1246. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
  1247. Warnings:
  1248. Note 1003 select `test`.`t1`.`id` AS `id` from `test`.`t1` where (`test`.`t1`.`salary` = (select max(`test`.`t1`.`salary`) from `test`.`t1`))
  1249. drop table t1;
  1250. CREATE TABLE t1 (
  1251. ID int(10) unsigned NOT NULL auto_increment,
  1252. SUB_ID int(3) unsigned NOT NULL default '0',
  1253. REF_ID int(10) unsigned default NULL,
  1254. REF_SUB int(3) unsigned default '0',
  1255. PRIMARY KEY (ID,SUB_ID),
  1256. UNIQUE KEY t1_PK (ID,SUB_ID),
  1257. KEY t1_FK (REF_ID,REF_SUB),
  1258. KEY t1_REFID (REF_ID)
  1259. ) ENGINE=MyISAM CHARSET=cp1251;
  1260. INSERT INTO t1 VALUES (1,0,NULL,NULL),(2,0,NULL,NULL);
  1261. SELECT DISTINCT REF_ID FROM t1 WHERE ID= (SELECT DISTINCT REF_ID FROM t1 WHERE ID=2);
  1262. REF_ID
  1263. DROP TABLE t1;
  1264. create table t1 (a int, b int);
  1265. create table t2 (a int, b int);
  1266. insert into t1 values (1,0), (2,0), (3,0);
  1267. insert into t2 values (1,1), (2,1), (3,1), (2,2);
  1268. update ignore t1 set b=(select b from t2 where t1.a=t2.a);
  1269. Warnings:
  1270. Warning 1242 Subquery returns more than 1 row
  1271. select * from t1;
  1272. a b
  1273. 1 1
  1274. 2 NULL
  1275. 3 1
  1276. drop table t1, t2;
  1277. CREATE TABLE `t1` (
  1278. `id` mediumint(8) unsigned NOT NULL auto_increment,
  1279. `pseudo` varchar(35) NOT NULL default '',
  1280. `email` varchar(60) NOT NULL default '',
  1281. PRIMARY KEY (`id`),
  1282. UNIQUE KEY `email` (`email`),
  1283. UNIQUE KEY `pseudo` (`pseudo`)
  1284. ) ENGINE=MyISAM CHARSET=latin1 PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
  1285. INSERT INTO t1 (id,pseudo,email) VALUES (1,'test','test'),(2,'test1','test1');
  1286. SELECT pseudo as a, pseudo as b FROM t1 GROUP BY (SELECT a) ORDER BY (SELECT id*1);
  1287. a b
  1288. test test
  1289. test1 test1
  1290. drop table if exists t1;
  1291. (SELECT 1 as a) UNION (SELECT 1) ORDER BY (SELECT a+0);
  1292. a
  1293. 1
  1294. create table t1 (a int not null, b int, primary key (a));
  1295. create table t2 (a int not null, primary key (a));
  1296. create table t3 (a int not null, b int, primary key (a));
  1297. insert into t1 values (1,10), (2,20), (3,30), (4,40);
  1298. insert into t2 values (2), (3), (4), (5);
  1299. insert into t3 values (10,3), (20,4), (30,5);
  1300. select * from t2 where t2.a in (select a from t1);
  1301. a
  1302. 2
  1303. 3
  1304. 4
  1305. explain extended select * from t2 where t2.a in (select a from t1);
  1306. id select_type table type possible_keys key key_len ref rows filtered Extra
  1307. 1 PRIMARY t2 index PRIMARY PRIMARY 4 NULL 4 100.00 Using index
  1308. 1 PRIMARY t1 index PRIMARY PRIMARY 4 NULL 4 75.00 Using where; Using index; Using join buffer (flat, BNL join)
  1309. Warnings:
  1310. Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t1` join `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)
  1311. select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
  1312. a
  1313. 2
  1314. 4
  1315. explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
  1316. id select_type table type possible_keys key key_len ref rows filtered Extra
  1317. 1 PRIMARY t2 index PRIMARY PRIMARY 4 NULL 4 100.00 Using index
  1318. 1 PRIMARY t1 ALL PRIMARY NULL NULL NULL 4 75.00 Using where; Using join buffer (flat, BNL join)
  1319. Warnings:
  1320. Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t1` join `test`.`t2` where ((`test`.`t1`.`a` = `test`.`t2`.`a`) and (`test`.`t1`.`b` <> 30))
  1321. select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
  1322. a
  1323. 2
  1324. 3
  1325. explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
  1326. id select_type table type possible_keys key key_len ref rows filtered Extra
  1327. 1 PRIMARY t2 index PRIMARY PRIMARY 4 NULL 4 100.00 Using index
  1328. 1 PRIMARY t1 ALL PRIMARY NULL NULL NULL 4 75.00 Using where; Using join buffer (flat, BNL join)
  1329. 1 PRIMARY t3 eq_ref PRIMARY PRIMARY 4 test.t1.b 1 100.00 Using index
  1330. Warnings:
  1331. Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t1` join `test`.`t3` join `test`.`t2` where ((`test`.`t1`.`a` = `test`.`t2`.`a`) and (`test`.`t3`.`a` = `test`.`t1`.`b`))
  1332. drop table t1, t2, t3;
  1333. create table t1 (a int, b int, index a (a,b));
  1334. create table t2 (a int, index a (a));
  1335. create table t3 (a int, b int, index a (a));
  1336. insert into t1 values (1,10), (2,20), (3,30), (4,40);
  1337. insert into t2 values (2), (3), (4), (5);
  1338. insert into t3 values (10,3), (20,4), (30,5);
  1339. SET @save_optimizer_switch=@@optimizer_switch;
  1340. SET optimizer_switch='semijoin_with_cache=off';
  1341. select * from t2 where t2.a in (select a from t1);
  1342. a
  1343. 2
  1344. 3
  1345. 4
  1346. explain extended select * from t2 where t2.a in (select a from t1);
  1347. id select_type table type possible_keys key key_len ref rows filtered Extra
  1348. 1 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index
  1349. 1 PRIMARY t1 ref a a 5 test.t2.a 1 100.00 Using index; FirstMatch(t2)
  1350. Warnings:
  1351. Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where (`test`.`t1`.`a` = `test`.`t2`.`a`)
  1352. select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
  1353. a
  1354. 2
  1355. 4
  1356. explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
  1357. id select_type table type possible_keys key key_len ref rows filtered Extra
  1358. 1 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index
  1359. 1 PRIMARY t1 ref a a 5 test.t2.a 1 100.00 Using where; Using index; FirstMatch(t2)
  1360. Warnings:
  1361. Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where ((`test`.`t1`.`a` = `test`.`t2`.`a`) and (`test`.`t1`.`b` <> 30))
  1362. select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
  1363. a
  1364. 2
  1365. 3
  1366. explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
  1367. id select_type table type possible_keys key key_len ref rows filtered Extra
  1368. 1 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index
  1369. 1 PRIMARY t1 ref a a 5 test.t2.a 1 100.00 Using where; Using index
  1370. 1 PRIMARY t3 ref a a 5 test.t1.b 1 100.00 Using index; FirstMatch(t2)
  1371. Warnings:
  1372. Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1` join `test`.`t3`) where ((`test`.`t1`.`a` = `test`.`t2`.`a`) and (`test`.`t3`.`a` = `test`.`t1`.`b`))
  1373. insert into t1 values (3,31);
  1374. select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
  1375. a
  1376. 2
  1377. 3
  1378. 4
  1379. select * from t2 where t2.a in (select a from t1 where t1.b <> 30 and t1.b <> 31);
  1380. a
  1381. 2
  1382. 4
  1383. explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
  1384. id select_type table type possible_keys key key_len ref rows filtered Extra
  1385. 1 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index
  1386. 1 PRIMARY t1 ref a a 5 test.t2.a 1 100.00 Using where; Using index; FirstMatch(t2)
  1387. Warnings:
  1388. Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where ((`test`.`t1`.`a` = `test`.`t2`.`a`) and (`test`.`t1`.`b` <> 30))
  1389. SET optimizer_switch=@save_optimizer_switch;
  1390. drop table t1, t2, t3;
  1391. create table t1 (a int, b int);
  1392. create table t2 (a int, b int);
  1393. create table t3 (a int, b int);
  1394. insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10);
  1395. insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1);
  1396. insert into t3 values (3,3), (2,2), (1,1);
  1397. select a,(select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1) from t3;
  1398. a (select count(distinct t1.b) as sum from t1,t2 where t1.a=t2.a and t2.b > 0 and t1.a <= t3.b group by t1.a order by sum limit 1)
  1399. 3 1
  1400. 2 2
  1401. 1 2
  1402. drop table t1,t2,t3;
  1403. create table t1 (s1 int);
  1404. create table t2 (s1 int);
  1405. insert into t1 values (1);
  1406. insert into t2 values (1);
  1407. select * from t1 where exists (select s1 from t2 having max(t2.s1)=t1.s1);
  1408. s1
  1409. 1
  1410. drop table t1,t2;
  1411. create table t1 (s1 int);
  1412. create table t2 (s1 int);
  1413. insert into t1 values (1);
  1414. insert into t2 values (1);
  1415. update t1 set s1 = s1 + 1 where 1 = (select x.s1 as A from t2 WHERE t2.s1 > t1.s1 order by A);
  1416. ERROR 42S22: Unknown column 'x.s1' in 'field list'
  1417. DROP TABLE t1, t2;
  1418. CREATE TABLE t1 (s1 CHAR(5) COLLATE latin1_german1_ci,
  1419. s2 CHAR(5) COLLATE latin1_swedish_ci);
  1420. INSERT INTO t1 VALUES ('z','?');
  1421. select * from t1 where s1 > (select max(s2) from t1);
  1422. ERROR HY000: Illegal mix of collations (latin1_german1_ci,IMPLICIT) and (latin1_swedish_ci,IMPLICIT) for operation '>'
  1423. select * from t1 where s1 > any (select max(s2) from t1);
  1424. ERROR HY000: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (latin1_german1_ci,IMPLICIT) for operation '<'
  1425. drop table t1;
  1426. create table t1(toid int,rd int);
  1427. create table t2(userid int,pmnew int,pmtotal int);
  1428. insert into t2 values(1,0,0),(2,0,0);
  1429. insert into t1 values(1,0),(1,0),(1,0),(1,12),(1,15),(1,123),(1,12312),(1,12312),(1,123),(2,0),(2,0),(2,1),(2,2);
  1430. select userid,pmtotal,pmnew, (select count(rd) from t1 where toid=t2.userid) calc_total, (select count(rd) from t1 where rd=0 and toid=t2.userid) calc_new from t2 where userid in (select distinct toid from t1);
  1431. userid pmtotal pmnew calc_total calc_new
  1432. 1 0 0 9 3
  1433. 2 0 0 4 2
  1434. drop table t1, t2;
  1435. create table t1 (s1 char(5));
  1436. select (select 'a','b' from t1 union select 'a','b' from t1) from t1;
  1437. ERROR 21000: Operand should contain 1 column(s)
  1438. insert into t1 values ('tttt');
  1439. select * from t1 where ('a','b')=(select 'a','b' from t1 union select 'a','b' from t1);
  1440. s1
  1441. tttt
  1442. explain extended (select * from t1);
  1443. id select_type table type possible_keys key key_len ref rows filtered Extra
  1444. 1 SIMPLE t1 ALL NULL NULL NULL NULL 1 100.00
  1445. Warnings:
  1446. Note 1003 (select `test`.`t1`.`s1` AS `s1` from `test`.`t1`)
  1447. (select * from t1);
  1448. s1
  1449. tttt
  1450. drop table t1;
  1451. create table t1 (s1 char(5), index s1(s1));
  1452. create table t2 (s1 char(5), index s1(s1));
  1453. insert into t1 values ('a1'),('a2'),('a3');
  1454. insert into t2 values ('a1'),('a2');
  1455. select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
  1456. s1 s1 NOT IN (SELECT s1 FROM t2)
  1457. a1 0
  1458. a2 0
  1459. a3 1
  1460. select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
  1461. s1 s1 = ANY (SELECT s1 FROM t2)
  1462. a1 1
  1463. a2 1
  1464. a3 0
  1465. select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
  1466. s1 s1 <> ALL (SELECT s1 FROM t2)
  1467. a1 0
  1468. a2 0
  1469. a3 1
  1470. select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
  1471. s1 s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2')
  1472. a1 0
  1473. a2 1
  1474. a3 1
  1475. explain extended select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
  1476. id select_type table type possible_keys key key_len ref rows filtered Extra
  1477. 1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index
  1478. 2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Full scan on NULL key
  1479. Warnings:
  1480. Note 1003 select `test`.`t1`.`s1` AS `s1`,(not(<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(<is_not_null_test>(`test`.`t2`.`s1`)))))))) AS `s1 NOT IN (SELECT s1 FROM t2)` from `test`.`t1`
  1481. explain extended select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
  1482. id select_type table type possible_keys key key_len ref rows filtered Extra
  1483. 1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index
  1484. 2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Full scan on NULL key
  1485. Warnings:
  1486. Note 1003 select `test`.`t1`.`s1` AS `s1`,<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(<is_not_null_test>(`test`.`t2`.`s1`)))))) AS `s1 = ANY (SELECT s1 FROM t2)` from `test`.`t1`
  1487. explain extended select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
  1488. id select_type table type possible_keys key key_len ref rows filtered Extra
  1489. 1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index
  1490. 2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Full scan on NULL key
  1491. Warnings:
  1492. Note 1003 select `test`.`t1`.`s1` AS `s1`,(not(<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL having trigcond(<is_not_null_test>(`test`.`t2`.`s1`)))))))) AS `s1 <> ALL (SELECT s1 FROM t2)` from `test`.`t1`
  1493. explain extended select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
  1494. id select_type table type possible_keys key key_len ref rows filtered Extra
  1495. 1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index
  1496. 2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Using where; Full scan on NULL key
  1497. Warnings:
  1498. Note 1003 select `test`.`t1`.`s1` AS `s1`,(not(<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,<exists>(<index_lookup>(<cache>(`test`.`t1`.`s1`) in t2 on s1 checking NULL where (`test`.`t2`.`s1` < 'a2') having trigcond(<is_not_null_test>(`test`.`t2`.`s1`)))))))) AS `s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2')` from `test`.`t1`
  1499. drop table t1,t2;
  1500. create table t2 (a int, b int not null);
  1501. create table t3 (a int);
  1502. insert into t3 values (6),(7),(3);
  1503. select * from t3 where a >= all (select b from t2);
  1504. a
  1505. 6
  1506. 7
  1507. 3
  1508. explain extended select * from t3 where a >= all (select b from t2);
  1509. id select_type table type possible_keys key key_len ref rows filtered Extra
  1510. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
  1511. 2 SUBQUERY t2 ALL NULL NULL NULL NULL 0 0.00
  1512. Warnings:
  1513. Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <not>(<in_optimizer>(`test`.`t3`.`a`,((select max(`test`.`t2`.`b`) from `test`.`t2`) > <cache>(`test`.`t3`.`a`))))
  1514. select * from t3 where a >= some (select b from t2);
  1515. a
  1516. explain extended select * from t3 where a >= some (select b from t2);
  1517. id select_type table type possible_keys key key_len ref rows filtered Extra
  1518. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
  1519. 2 SUBQUERY t2 ALL NULL NULL NULL NULL 0 0.00
  1520. Warnings:
  1521. Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(`test`.`t3`.`a`,((select min(`test`.`t2`.`b`) from `test`.`t2`) <= <cache>(`test`.`t3`.`a`))))
  1522. select * from t3 where a >= all (select b from t2 group by 1);
  1523. a
  1524. 6
  1525. 7
  1526. 3
  1527. explain extended select * from t3 where a >= all (select b from t2 group by 1);
  1528. id select_type table type possible_keys key key_len ref rows filtered Extra
  1529. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
  1530. 2 SUBQUERY t2 ALL NULL NULL NULL NULL 0 0.00
  1531. Warnings:
  1532. Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <not>(<in_optimizer>(`test`.`t3`.`a`,((select max(`test`.`t2`.`b`) from `test`.`t2`) > <cache>(`test`.`t3`.`a`))))
  1533. select * from t3 where a >= some (select b from t2 group by 1);
  1534. a
  1535. explain extended select * from t3 where a >= some (select b from t2 group by 1);
  1536. id select_type table type possible_keys key key_len ref rows filtered Extra
  1537. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
  1538. 2 SUBQUERY t2 ALL NULL NULL NULL NULL 0 0.00
  1539. Warnings:
  1540. Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(`test`.`t3`.`a`,((select min(`test`.`t2`.`b`) from `test`.`t2`) <= <cache>(`test`.`t3`.`a`))))
  1541. select * from t3 where NULL >= any (select b from t2);
  1542. a
  1543. explain extended select * from t3 where NULL >= any (select b from t2);
  1544. id select_type table type possible_keys key key_len ref rows filtered Extra
  1545. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00
  1546. 2 SUBQUERY t2 ALL NULL NULL NULL NULL 0 0.00
  1547. Warnings:
  1548. Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(NULL,((select min(`test`.`t2`.`b`) from `test`.`t2`) <= NULL)))
  1549. select * from t3 where NULL >= any (select b from t2 group by 1);
  1550. a
  1551. explain extended select * from t3 where NULL >= any (select b from t2 group by 1);
  1552. id select_type table type possible_keys key key_len ref rows filtered Extra
  1553. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00
  1554. 2 SUBQUERY t2 ALL NULL NULL NULL NULL 0 0.00
  1555. Warnings:
  1556. Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(NULL,((select min(`test`.`t2`.`b`) from `test`.`t2`) <= NULL)))
  1557. select * from t3 where NULL >= some (select b from t2);
  1558. a
  1559. explain extended select * from t3 where NULL >= some (select b from t2);
  1560. id select_type table type possible_keys key key_len ref rows filtered Extra
  1561. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00
  1562. 2 SUBQUERY t2 ALL NULL NULL NULL NULL 0 0.00
  1563. Warnings:
  1564. Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(NULL,((select min(`test`.`t2`.`b`) from `test`.`t2`) <= NULL)))
  1565. select * from t3 where NULL >= some (select b from t2 group by 1);
  1566. a
  1567. explain extended select * from t3 where NULL >= some (select b from t2 group by 1);
  1568. id select_type table type possible_keys key key_len ref rows filtered Extra
  1569. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00
  1570. 2 SUBQUERY t2 ALL NULL NULL NULL NULL 0 0.00
  1571. Warnings:
  1572. Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(NULL,((select min(`test`.`t2`.`b`) from `test`.`t2`) <= NULL)))
  1573. insert into t2 values (2,2), (2,1), (3,3), (3,1);
  1574. select * from t3 where a > all (select max(b) from t2 group by a);
  1575. a
  1576. 6
  1577. 7
  1578. explain extended select * from t3 where a > all (select max(b) from t2 group by a);
  1579. id select_type table type possible_keys key key_len ref rows filtered Extra
  1580. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
  1581. 2 SUBQUERY t2 ALL NULL NULL NULL NULL 4 100.00 Using temporary
  1582. Warnings:
  1583. Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <not>(<in_optimizer>(`test`.`t3`.`a`,(<max>(select max(`test`.`t2`.`b`) from `test`.`t2` group by `test`.`t2`.`a`) >= <cache>(`test`.`t3`.`a`))))
  1584. drop table t2, t3;
  1585. CREATE TABLE `t1` ( `id` mediumint(9) NOT NULL auto_increment, `taskid` bigint(20) NOT NULL default '0', `dbid` int(11) NOT NULL default '0', `create_date` datetime NOT NULL default '0000-00-00 00:00:00', `last_update` datetime NOT NULL default '0000-00-00 00:00:00', PRIMARY KEY (`id`)) ENGINE=MyISAM CHARSET=latin1 AUTO_INCREMENT=3 ;
  1586. INSERT INTO `t1` (`id`, `taskid`, `dbid`, `create_date`,`last_update`) VALUES (1, 1, 15, '2003-09-29 10:31:36', '2003-09-29 10:31:36'), (2, 1, 21, now(), now());
  1587. CREATE TABLE `t2` (`db_id` int(11) NOT NULL auto_increment,`name` varchar(200) NOT NULL default '',`primary_uid` smallint(6) NOT NULL default '0',`secondary_uid` smallint(6) NOT NULL default '0',PRIMARY KEY (`db_id`),UNIQUE KEY `name_2` (`name`),FULLTEXT KEY `name` (`name`)) ENGINE=MyISAM CHARSET=latin1 AUTO_INCREMENT=2147483647;
  1588. INSERT INTO `t2` (`db_id`, `name`, `primary_uid`, `secondary_uid`) VALUES (18, 'Not Set 1', 0, 0),(19, 'Valid', 1, 2),(20, 'Valid 2', 1, 2),(21, 'Should Not Return', 1, 2),(26, 'Not Set 2', 0, 0),(-1, 'ALL DB\'S', 0, 0);
  1589. CREATE TABLE `t3` (`taskgenid` mediumint(9) NOT NULL auto_increment,`dbid` int(11) NOT NULL default '0',`taskid` int(11) NOT NULL default '0',`mon` tinyint(4) NOT NULL default '1',`tues` tinyint(4) NOT NULL default '1',`wed` tinyint(4) NOT NULL default '1',`thur` tinyint(4) NOT NULL default '1',`fri` tinyint(4) NOT NULL default '1',`sat` tinyint(4) NOT NULL default '0',`sun` tinyint(4) NOT NULL default '0',`how_often` smallint(6) NOT NULL default '1',`userid` smallint(6) NOT NULL default '0',`active` tinyint(4) NOT NULL default '1',PRIMARY KEY (`taskgenid`)) ENGINE=MyISAM CHARSET=latin1 AUTO_INCREMENT=2 ;
  1590. INSERT INTO `t3` (`taskgenid`, `dbid`, `taskid`, `mon`, `tues`,`wed`, `thur`, `fri`, `sat`, `sun`, `how_often`, `userid`, `active`) VALUES (1,-1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1);
  1591. CREATE TABLE `t4` (`task_id` smallint(6) NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
  1592. INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status');
  1593. select dbid, name, (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01') from t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND t4.task_id = taskid;
  1594. dbid name (date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01')
  1595. -1 Valid 1
  1596. -1 Valid 2 1
  1597. -1 Should Not Return 0
  1598. SELECT dbid, name FROM t3 a, t2 b, t4 WHERE dbid = - 1 AND primary_uid = '1' AND ((date_format(now() , '%Y-%m-%d') - INTERVAL how_often DAY) >= ifnull((SELECT date_format(max(create_date),'%Y-%m-%d') FROM t1 WHERE dbid = b.db_id AND taskid = a.taskgenid), '1950-01-01')) AND t4.task_id = taskid;
  1599. dbid name
  1600. -1 Valid
  1601. -1 Valid 2
  1602. drop table t1,t2,t3,t4;
  1603. CREATE TABLE t1 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1;
  1604. INSERT INTO t1 VALUES (1),(5);
  1605. CREATE TABLE t2 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1;
  1606. INSERT INTO t2 VALUES (2),(6);
  1607. select * from t1 where (1,2,6) in (select * from t2);
  1608. ERROR 21000: Operand should contain 3 column(s)
  1609. DROP TABLE t1,t2;
  1610. create table t1 (s1 int);
  1611. insert into t1 values (1);
  1612. insert into t1 values (2);
  1613. set sort_buffer_size = (select s1 from t1);
  1614. ERROR 21000: Subquery returns more than 1 row
  1615. do (select * from t1);
  1616. Warnings:
  1617. Error 1242 Subquery returns more than 1 row
  1618. drop table t1;
  1619. create table t1 (s1 char);
  1620. insert into t1 values ('e');
  1621. select * from t1 where 'f' > any (select s1 from t1);
  1622. s1
  1623. e
  1624. select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
  1625. s1
  1626. e
  1627. explain extended select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
  1628. id select_type table type possible_keys key key_len ref rows filtered Extra
  1629. 1 PRIMARY t1 ALL NULL NULL NULL NULL 1 100.00
  1630. 2 SUBQUERY t1 ALL NULL NULL NULL NULL 1 100.00
  1631. 3 UNION t1 ALL NULL NULL NULL NULL 1 100.00
  1632. NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
  1633. Warnings:
  1634. Note 1003 select `test`.`t1`.`s1` AS `s1` from `test`.`t1` where <nop>(<in_optimizer>('f',(<min>(select `test`.`t1`.`s1` from `test`.`t1` union select `test`.`t1`.`s1` from `test`.`t1`) < 'f')))
  1635. drop table t1;
  1636. CREATE TABLE t1 (number char(11) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
  1637. INSERT INTO t1 VALUES ('69294728265'),('18621828126'),('89356874041'),('95895001874');
  1638. CREATE TABLE t2 (code char(5) NOT NULL default '',UNIQUE KEY code (code)) ENGINE=MyISAM CHARSET=latin1;
  1639. INSERT INTO t2 VALUES ('1'),('1226'),('1245'),('1862'),('18623'),('1874'),('1967'),('6');
  1640. select c.number as phone,(select p.code from t2 p where c.number like concat(p.code, '%') order by length(p.code) desc limit 1) as code from t1 c;
  1641. phone code
  1642. 69294728265 6
  1643. 18621828126 1862
  1644. 89356874041 NULL
  1645. 95895001874 NULL
  1646. drop table t1, t2;
  1647. create table t1 (s1 int);
  1648. create table t2 (s1 int);
  1649. select * from t1 where (select count(*) from t2 where t1.s2) = 1;
  1650. ERROR 42S22: Unknown column 't1.s2' in 'where clause'
  1651. select * from t1 where (select count(*) from t2 group by t1.s2) = 1;
  1652. ERROR 42S22: Unknown column 't1.s2' in 'group statement'
  1653. select count(*) from t2 group by t1.s2;
  1654. ERROR 42S22: Unknown column 't1.s2' in 'group statement'
  1655. drop table t1, t2;
  1656. CREATE TABLE t1(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC VARCHAR(20) DEFAULT NULL,PRIMARY KEY (COLA, COLB));
  1657. CREATE TABLE t2(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC CHAR(1) NOT NULL,PRIMARY KEY (COLA));
  1658. INSERT INTO t1 VALUES (1,1,'1A3240'), (1,2,'4W2365');
  1659. INSERT INTO t2 VALUES (100, 200, 'C');
  1660. SELECT DISTINCT COLC FROM t1 WHERE COLA = (SELECT COLA FROM t2 WHERE COLB = 200 AND COLC ='C' LIMIT 1);
  1661. COLC
  1662. DROP TABLE t1, t2;
  1663. CREATE TABLE t1 (a int(1));
  1664. INSERT INTO t1 VALUES (1),(1),(1),(1),(1),(2),(3),(4),(5);
  1665. SELECT DISTINCT (SELECT a) FROM t1 LIMIT 100;
  1666. (SELECT a)
  1667. 1
  1668. 2
  1669. 3
  1670. 4
  1671. 5
  1672. DROP TABLE t1;
  1673. create table t1 (a int, b decimal(13, 3));
  1674. insert into t1 values (1, 0.123);
  1675. select a, (select max(b) from t1) into outfile "subselect.out.file.1" from t1;
  1676. delete from t1;
  1677. load data infile "subselect.out.file.1" into table t1;
  1678. select * from t1;
  1679. a b
  1680. 1 0.123
  1681. drop table t1;
  1682. CREATE TABLE `t1` (
  1683. `id` int(11) NOT NULL auto_increment,
  1684. `id_cns` tinyint(3) unsigned NOT NULL default '0',
  1685. `tipo` enum('','UNO','DUE') NOT NULL default '',
  1686. `anno_dep` smallint(4) unsigned zerofill NOT NULL default '0000',
  1687. `particolare` mediumint(8) unsigned NOT NULL default '0',
  1688. `generale` mediumint(8) unsigned NOT NULL default '0',
  1689. `bis` tinyint(3) unsigned NOT NULL default '0',
  1690. PRIMARY KEY (`id`),
  1691. UNIQUE KEY `idx_cns_gen_anno` (`anno_dep`,`id_cns`,`generale`,`particolare`),
  1692. UNIQUE KEY `idx_cns_par_anno` (`id_cns`,`anno_dep`,`tipo`,`particolare`,`bis`)
  1693. );
  1694. INSERT INTO `t1` VALUES (1,16,'UNO',1987,2048,9681,0),(2,50,'UNO',1987,1536,13987,0),(3,16,'UNO',1987,2432,14594,0),(4,16,'UNO',1987,1792,13422,0),(5,16,'UNO',1987,1025,10240,0),(6,16,'UNO',1987,1026,7089,0);
  1695. CREATE TABLE `t2` (
  1696. `id` tinyint(3) unsigned NOT NULL auto_increment,
  1697. `max_anno_dep` smallint(6) unsigned NOT NULL default '0',
  1698. PRIMARY KEY (`id`)
  1699. );
  1700. INSERT INTO `t2` VALUES (16,1987),(50,1990),(51,1990);
  1701. SELECT cns.id, cns.max_anno_dep, cns.max_anno_dep = (SELECT s.anno_dep FROM t1 AS s WHERE s.id_cns = cns.id ORDER BY s.anno_dep DESC LIMIT 1) AS PIPPO FROM t2 AS cns;
  1702. id max_anno_dep PIPPO
  1703. 16 1987 1
  1704. 50 1990 0
  1705. 51 1990 NULL
  1706. DROP TABLE t1, t2;
  1707. create table t1 (a int);
  1708. insert into t1 values (1), (2), (3);
  1709. SET SQL_SELECT_LIMIT=1;
  1710. select sum(a) from (select * from t1) as a;
  1711. sum(a)
  1712. 6
  1713. select 2 in (select * from t1);
  1714. 2 in (select * from t1)
  1715. 1
  1716. SET SQL_SELECT_LIMIT=default;
  1717. drop table t1;
  1718. CREATE TABLE t1 (a int, b int, INDEX (a));
  1719. INSERT INTO t1 VALUES (1, 1), (1, 2), (1, 3);
  1720. SELECT * FROM t1 WHERE a = (SELECT MAX(a) FROM t1 WHERE a = 1) ORDER BY b;
  1721. a b
  1722. 1 1
  1723. 1 2
  1724. 1 3
  1725. DROP TABLE t1;
  1726. create table t1(val varchar(10));
  1727. insert into t1 values ('aaa'), ('bbb'),('eee'),('mmm'),('ppp');
  1728. select count(*) from t1 as w1 where w1.val in (select w2.val from t1 as w2 where w2.val like 'm%') and w1.val in (select w3.val from t1 as w3 where w3.val like 'e%');
  1729. count(*)
  1730. 0
  1731. drop table t1;
  1732. create table t1 (id int not null, text varchar(20) not null default '', primary key (id));
  1733. insert into t1 (id, text) values (1, 'text1'), (2, 'text2'), (3, 'text3'), (4, 'text4'), (5, 'text5'), (6, 'text6'), (7, 'text7'), (8, 'text8'), (9, 'text9'), (10, 'text10'), (11, 'text11'), (12, 'text12');
  1734. select * from t1 where id not in (select id from t1 where id < 8);
  1735. id text
  1736. 8 text8
  1737. 9 text9
  1738. 10 text10
  1739. 11 text11
  1740. 12 text12
  1741. select * from t1 as tt where not exists (select id from t1 where id < 8 and (id = tt.id or id is null) having id is not null);
  1742. id text
  1743. 8 text8
  1744. 9 text9
  1745. 10 text10
  1746. 11 text11
  1747. 12 text12
  1748. explain extended select * from t1 where id not in (select id from t1 where id < 8);
  1749. id select_type table type possible_keys key key_len ref rows filtered Extra
  1750. 1 PRIMARY t1 ALL NULL NULL NULL NULL 12 100.00 Using where
  1751. 2 DEPENDENT SUBQUERY t1 unique_subquery PRIMARY PRIMARY 4 func 1 100.00 Using index; Using where
  1752. Warnings:
  1753. Note 1003 select `test`.`t1`.`id` AS `id`,`test`.`t1`.`text` AS `text` from `test`.`t1` where (not(<expr_cache><`test`.`t1`.`id`>(<in_optimizer>(`test`.`t1`.`id`,<exists>(<primary_index_lookup>(<cache>(`test`.`t1`.`id`) in t1 on PRIMARY where ((`test`.`t1`.`id` < 8) and (<cache>(`test`.`t1`.`id`) = `test`.`t1`.`id`))))))))
  1754. explain extended select * from t1 as tt where not exists (select id from t1 where id < 8 and (id = tt.id or id is null) having id is not null);
  1755. id select_type table type possible_keys key key_len ref rows filtered Extra
  1756. 1 PRIMARY tt ALL NULL NULL NULL NULL 12 100.00 Using where
  1757. 2 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.tt.id 1 100.00 Using where; Using index
  1758. Warnings:
  1759. Note 1276 Field or reference 'test.tt.id' of SELECT #2 was resolved in SELECT #1
  1760. Note 1003 select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where (not(<expr_cache><`test`.`tt`.`id`>(exists(select `test`.`t1`.`id` from `test`.`t1` where ((`test`.`t1`.`id` < 8) and (`test`.`t1`.`id` = `test`.`tt`.`id`)) having (`test`.`t1`.`id` is not null)))))
  1761. insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001');
  1762. create table t2 (id int not null, text varchar(20) not null default '', primary key (id));
  1763. insert into t2 (id, text) values (1, 'text1'), (2, 'text2'), (3, 'text3'), (4, 'text4'), (5, 'text5'), (6, 'text6'), (7, 'text7'), (8, 'text8'), (9, 'text9'), (10, 'text10'), (11, 'text1'), (12, 'text2'), (13, 'text3'), (14, 'text4'), (15, 'text5'), (16, 'text6'), (17, 'text7'), (18, 'text8'), (19, 'text9'), (20, 'text10'),(21, 'text1'), (22, 'text2'), (23, 'text3'), (24, 'text4'), (25, 'text5'), (26, 'text6'), (27, 'text7'), (28, 'text8'), (29, 'text9'), (30, 'text10'), (31, 'text1'), (32, 'text2'), (33, 'text3'), (34, 'text4'), (35, 'text5'), (36, 'text6'), (37, 'text7'), (38, 'text8'), (39, 'text9'), (40, 'text10'), (41, 'text1'), (42, 'text2'), (43, 'text3'), (44, 'text4'), (45, 'text5'), (46, 'text6'), (47, 'text7'), (48, 'text8'), (49, 'text9'), (50, 'text10');
  1764. select * from t1 a left join t2 b on (a.id=b.id or b.id is null) join t1 c on (if(isnull(b.id), 1000, b.id)=c.id);
  1765. id text id text id text
  1766. 1 text1 1 text1 1 text1
  1767. 2 text2 2 text2 2 text2
  1768. 3 text3 3 text3 3 text3
  1769. 4 text4 4 text4 4 text4
  1770. 5 text5 5 text5 5 text5
  1771. 6 text6 6 text6 6 text6
  1772. 7 text7 7 text7 7 text7
  1773. 8 text8 8 text8 8 text8
  1774. 9 text9 9 text9 9 text9
  1775. 10 text10 10 text10 10 text10
  1776. 11 text11 11 text1 11 text11
  1777. 12 text12 12 text2 12 text12
  1778. 1000 text1000 NULL NULL 1000 text1000
  1779. 1001 text1001 NULL NULL 1000 text1000
  1780. explain extended select * from t1 a left join t2 b on (a.id=b.id or b.id is null) join t1 c on (if(isnull(b.id), 1000, b.id)=c.id);
  1781. id select_type table type possible_keys key key_len ref rows filtered Extra
  1782. 1 SIMPLE a ALL NULL NULL NULL NULL 14 100.00
  1783. 1 SIMPLE b eq_ref PRIMARY PRIMARY 4 test.a.id 2 100.00
  1784. 1 SIMPLE c eq_ref PRIMARY PRIMARY 4 func 1 100.00 Using where
  1785. Warnings:
  1786. Note 1003 select `test`.`a`.`id` AS `id`,`test`.`a`.`text` AS `text`,`test`.`b`.`id` AS `id`,`test`.`b`.`text` AS `text`,`test`.`c`.`id` AS `id`,`test`.`c`.`text` AS `text` from `test`.`t1` `a` left join `test`.`t2` `b` on(((`test`.`b`.`id` = `test`.`a`.`id`) or isnull(`test`.`b`.`id`))) join `test`.`t1` `c` where (if(isnull(`test`.`b`.`id`),1000,`test`.`b`.`id`) = `test`.`c`.`id`)
  1787. drop table t1,t2;
  1788. set @@optimizer_switch=@save_optimizer_switch;
  1789. create table t1 (a int);
  1790. insert into t1 values (1);
  1791. explain select benchmark(1000, (select a from t1 where a=sha(rand())));
  1792. id select_type table type possible_keys key key_len ref rows Extra
  1793. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  1794. 2 UNCACHEABLE SUBQUERY t1 ALL NULL NULL NULL NULL 1 Using where
  1795. drop table t1;
  1796. create table t1(id int);
  1797. create table t2(id int);
  1798. create table t3(flag int);
  1799. select (select * from t3 where id not null) from t1, t2;
  1800. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'null) from t1, t2' at line 1
  1801. drop table t1,t2,t3;
  1802. CREATE TABLE t1 (id INT);
  1803. CREATE TABLE t2 (id INT);
  1804. INSERT INTO t1 VALUES (1), (2);
  1805. INSERT INTO t2 VALUES (1);
  1806. SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);
  1807. id c
  1808. 1 1
  1809. 2 0
  1810. SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);
  1811. id c
  1812. 1 1
  1813. 2 0
  1814. SELECT t1.id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id) ORDER BY t1.id;
  1815. id c
  1816. 1 1
  1817. 2 0
  1818. SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id) ORDER BY id;
  1819. id c
  1820. 1 1
  1821. 2 0
  1822. DROP TABLE t1,t2;
  1823. CREATE TABLE t1 ( a int, b int );
  1824. INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
  1825. SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
  1826. a
  1827. 3
  1828. SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
  1829. a
  1830. 1
  1831. SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
  1832. a
  1833. 2
  1834. SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
  1835. a
  1836. 2
  1837. 3
  1838. SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
  1839. a
  1840. 1
  1841. 2
  1842. SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
  1843. a
  1844. 1
  1845. 3
  1846. SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
  1847. a
  1848. 3
  1849. SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
  1850. a
  1851. 1
  1852. SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
  1853. a
  1854. 2
  1855. SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
  1856. a
  1857. 2
  1858. 3
  1859. SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
  1860. a
  1861. 1
  1862. 2
  1863. SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
  1864. a
  1865. 1
  1866. 3
  1867. ALTER TABLE t1 ADD INDEX (a);
  1868. SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
  1869. a
  1870. 3
  1871. SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
  1872. a
  1873. 1
  1874. SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
  1875. a
  1876. 2
  1877. SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
  1878. a
  1879. 2
  1880. 3
  1881. SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
  1882. a
  1883. 1
  1884. 2
  1885. SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
  1886. a
  1887. 1
  1888. 3
  1889. SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
  1890. a
  1891. 3
  1892. SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
  1893. a
  1894. 1
  1895. SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
  1896. a
  1897. 2
  1898. SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
  1899. a
  1900. 2
  1901. 3
  1902. SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
  1903. a
  1904. 1
  1905. 2
  1906. SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
  1907. a
  1908. 1
  1909. 3
  1910. SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2);
  1911. a
  1912. 3
  1913. SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2);
  1914. a
  1915. 1
  1916. SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2);
  1917. a
  1918. 2
  1919. SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2);
  1920. a
  1921. 2
  1922. 3
  1923. SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2);
  1924. a
  1925. 1
  1926. 2
  1927. SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2);
  1928. a
  1929. 1
  1930. 3
  1931. SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2);
  1932. a
  1933. 3
  1934. SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2);
  1935. a
  1936. 1
  1937. SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2);
  1938. a
  1939. 2
  1940. SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2);
  1941. a
  1942. 2
  1943. 3
  1944. SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2);
  1945. a
  1946. 1
  1947. 2
  1948. SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2);
  1949. a
  1950. 1
  1951. 3
  1952. SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  1953. a
  1954. 3
  1955. SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  1956. a
  1957. 1
  1958. SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  1959. a
  1960. 2
  1961. SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  1962. a
  1963. 2
  1964. 3
  1965. SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  1966. a
  1967. 1
  1968. 2
  1969. SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  1970. a
  1971. 1
  1972. 3
  1973. SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  1974. a
  1975. 3
  1976. SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  1977. a
  1978. 1
  1979. SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  1980. a
  1981. 2
  1982. SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  1983. a
  1984. 2
  1985. 3
  1986. SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  1987. a
  1988. 1
  1989. 2
  1990. SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  1991. a
  1992. 1
  1993. 3
  1994. SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  1995. a
  1996. 3
  1997. SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  1998. a
  1999. 1
  2000. SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2001. a
  2002. 2
  2003. SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2004. a
  2005. 2
  2006. 3
  2007. SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2008. a
  2009. 1
  2010. 2
  2011. SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2012. a
  2013. 1
  2014. 3
  2015. SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2016. a
  2017. 3
  2018. SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2019. a
  2020. 1
  2021. SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2022. a
  2023. 2
  2024. SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2025. a
  2026. 2
  2027. 3
  2028. SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2029. a
  2030. 1
  2031. 2
  2032. SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2033. a
  2034. 1
  2035. 3
  2036. SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a FROM t1 WHERE b = 2);
  2037. ERROR 21000: Operand should contain 1 column(s)
  2038. SELECT a FROM t1 WHERE a > ANY (SELECT a,2 FROM t1 WHERE b = 2);
  2039. ERROR 21000: Operand should contain 1 column(s)
  2040. SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a,2 FROM t1 WHERE b = 2);
  2041. ERROR 21000: Operand should contain 1 column(s)
  2042. SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a FROM t1 WHERE b = 2);
  2043. ERROR 21000: Operand should contain 1 column(s)
  2044. SELECT a FROM t1 WHERE a > ALL (SELECT a,2 FROM t1 WHERE b = 2);
  2045. ERROR 21000: Operand should contain 1 column(s)
  2046. SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a,2 FROM t1 WHERE b = 2);
  2047. ERROR 21000: Operand should contain 1 column(s)
  2048. SELECT a FROM t1 WHERE (1,2) = ALL (SELECT a,2 FROM t1 WHERE b = 2);
  2049. ERROR 21000: Operand should contain 1 column(s)
  2050. SELECT a FROM t1 WHERE (1,2) <> ANY (SELECT a,2 FROM t1 WHERE b = 2);
  2051. ERROR 21000: Operand should contain 1 column(s)
  2052. SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a FROM t1 WHERE b = 2);
  2053. ERROR 21000: Operand should contain 2 column(s)
  2054. SELECT a FROM t1 WHERE a = ANY (SELECT a,2 FROM t1 WHERE b = 2);
  2055. ERROR 21000: Operand should contain 1 column(s)
  2056. SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a,2 FROM t1 WHERE b = 2);
  2057. a
  2058. SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a FROM t1 WHERE b = 2);
  2059. ERROR 21000: Operand should contain 2 column(s)
  2060. SELECT a FROM t1 WHERE a <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
  2061. ERROR 21000: Operand should contain 1 column(s)
  2062. SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
  2063. a
  2064. 1
  2065. 2
  2066. 3
  2067. SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2);
  2068. a
  2069. 2
  2070. SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2);
  2071. a
  2072. 1
  2073. 3
  2074. SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2);
  2075. a
  2076. 2
  2077. SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2);
  2078. a
  2079. 1
  2080. 3
  2081. SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2 UNION SELECT a,1 FROM t1 WHERE b = 2);
  2082. a
  2083. 2
  2084. SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2 UNION SELECT a,1 FROM t1 WHERE b = 2);
  2085. a
  2086. 1
  2087. 3
  2088. SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2 UNION SELECT a,1 FROM t1 HAVING a = 2);
  2089. a
  2090. 2
  2091. SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2 UNION SELECT a,1 FROM t1 HAVING a = 2);
  2092. a
  2093. 1
  2094. 3
  2095. SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 group by a);
  2096. a
  2097. 3
  2098. SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 group by a);
  2099. a
  2100. 1
  2101. SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 group by a);
  2102. a
  2103. 2
  2104. SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 group by a);
  2105. a
  2106. 2
  2107. 3
  2108. SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 group by a);
  2109. a
  2110. 1
  2111. 2
  2112. SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 group by a);
  2113. a
  2114. 1
  2115. 3
  2116. SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 group by a);
  2117. a
  2118. 3
  2119. SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 group by a);
  2120. a
  2121. 1
  2122. SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 group by a);
  2123. a
  2124. 2
  2125. SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 group by a);
  2126. a
  2127. 2
  2128. 3
  2129. SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 group by a);
  2130. a
  2131. 1
  2132. 2
  2133. SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 group by a);
  2134. a
  2135. 1
  2136. 3
  2137. SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 group by a HAVING a = 2);
  2138. a
  2139. 3
  2140. SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 group by a HAVING a = 2);
  2141. a
  2142. 1
  2143. SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 group by a HAVING a = 2);
  2144. a
  2145. 2
  2146. SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 group by a HAVING a = 2);
  2147. a
  2148. 2
  2149. 3
  2150. SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 group by a HAVING a = 2);
  2151. a
  2152. 1
  2153. 2
  2154. SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 group by a HAVING a = 2);
  2155. a
  2156. 1
  2157. 3
  2158. SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 group by a HAVING a = 2);
  2159. a
  2160. 3
  2161. SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 group by a HAVING a = 2);
  2162. a
  2163. 1
  2164. SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 group by a HAVING a = 2);
  2165. a
  2166. 2
  2167. SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 group by a HAVING a = 2);
  2168. a
  2169. 2
  2170. 3
  2171. SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 group by a HAVING a = 2);
  2172. a
  2173. 1
  2174. 2
  2175. SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 group by a HAVING a = 2);
  2176. a
  2177. 1
  2178. 3
  2179. SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-') from t1 a;
  2180. concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-')
  2181. 0-
  2182. 0-
  2183. 1-
  2184. SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-') from t1 a;
  2185. concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-')
  2186. 1-
  2187. 0-
  2188. 0-
  2189. SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-') from t1 a;
  2190. concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-')
  2191. 0-
  2192. 1-
  2193. 0-
  2194. DROP TABLE t1;
  2195. CREATE TABLE t1 ( a double, b double );
  2196. INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
  2197. SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2e0);
  2198. a
  2199. 3
  2200. SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2e0);
  2201. a
  2202. 1
  2203. SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2e0);
  2204. a
  2205. 2
  2206. SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2e0);
  2207. a
  2208. 2
  2209. 3
  2210. SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2e0);
  2211. a
  2212. 1
  2213. 2
  2214. SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2e0);
  2215. a
  2216. 1
  2217. 3
  2218. SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2e0);
  2219. a
  2220. 3
  2221. SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2e0);
  2222. a
  2223. 1
  2224. SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2e0);
  2225. a
  2226. 2
  2227. SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2e0);
  2228. a
  2229. 2
  2230. 3
  2231. SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2e0);
  2232. a
  2233. 1
  2234. 2
  2235. SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2e0);
  2236. a
  2237. 1
  2238. 3
  2239. DROP TABLE t1;
  2240. CREATE TABLE t1 ( a char(1), b char(1));
  2241. INSERT INTO t1 VALUES ('1','1'),('2','2'),('3','3');
  2242. SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = '2');
  2243. a
  2244. 3
  2245. SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = '2');
  2246. a
  2247. 1
  2248. SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = '2');
  2249. a
  2250. 2
  2251. SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = '2');
  2252. a
  2253. 2
  2254. 3
  2255. SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = '2');
  2256. a
  2257. 1
  2258. 2
  2259. SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = '2');
  2260. a
  2261. 1
  2262. 3
  2263. SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = '2');
  2264. a
  2265. 3
  2266. SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = '2');
  2267. a
  2268. 1
  2269. SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = '2');
  2270. a
  2271. 2
  2272. SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = '2');
  2273. a
  2274. 2
  2275. 3
  2276. SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = '2');
  2277. a
  2278. 1
  2279. 2
  2280. SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = '2');
  2281. a
  2282. 1
  2283. 3
  2284. DROP TABLE t1;
  2285. create table t1 (a int, b int);
  2286. insert into t1 values (1,2),(3,4);
  2287. select * from t1 up where exists (select * from t1 where t1.a=up.a);
  2288. a b
  2289. 1 2
  2290. 3 4
  2291. explain extended select * from t1 up where exists (select * from t1 where t1.a=up.a);
  2292. id select_type table type possible_keys key key_len ref rows filtered Extra
  2293. 1 PRIMARY up ALL NULL NULL NULL NULL 2 100.00 Using where
  2294. 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using where
  2295. Warnings:
  2296. Note 1276 Field or reference 'test.up.a' of SELECT #2 was resolved in SELECT #1
  2297. Note 1003 select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` where <expr_cache><`test`.`up`.`a`>(exists(select 1 from `test`.`t1` where (`test`.`t1`.`a` = `test`.`up`.`a`)))
  2298. drop table t1;
  2299. CREATE TABLE t1 (t1_a int);
  2300. INSERT INTO t1 VALUES (1);
  2301. CREATE TABLE t2 (t2_a int, t2_b int, PRIMARY KEY (t2_a, t2_b));
  2302. INSERT INTO t2 VALUES (1, 1), (1, 2);
  2303. SELECT * FROM t1, t2 table2 WHERE t1_a = 1 AND table2.t2_a = 1
  2304. HAVING table2.t2_b = (SELECT MAX(t2_b) FROM t2 WHERE t2_a = table2.t2_a);
  2305. t1_a t2_a t2_b
  2306. 1 1 2
  2307. DROP TABLE t1, t2;
  2308. CREATE TABLE t1 (id int(11) default NULL,name varchar(10) default NULL);
  2309. INSERT INTO t1 VALUES (1,'Tim'),(2,'Rebecca'),(3,NULL);
  2310. CREATE TABLE t2 (id int(11) default NULL, pet varchar(10) default NULL);
  2311. INSERT INTO t2 VALUES (1,'Fido'),(2,'Spot'),(3,'Felix');
  2312. SELECT a.*, b.* FROM (SELECT * FROM t1) AS a JOIN t2 as b on a.id=b.id;
  2313. id name id pet
  2314. 1 Tim 1 Fido
  2315. 2 Rebecca 2 Spot
  2316. 3 NULL 3 Felix
  2317. drop table t1,t2;
  2318. CREATE TABLE t1 ( a int, b int );
  2319. CREATE TABLE t2 ( c int, d int );
  2320. INSERT INTO t1 VALUES (1,2), (2,3), (3,4);
  2321. SELECT a AS abc, b FROM t1 outr WHERE b =
  2322. (SELECT MIN(b) FROM t1 WHERE a=outr.a);
  2323. abc b
  2324. 1 2
  2325. 2 3
  2326. 3 4
  2327. INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b =
  2328. (SELECT MIN(b) FROM t1 WHERE a=outr.a);
  2329. select * from t2;
  2330. c d
  2331. 1 2
  2332. 2 3
  2333. 3 4
  2334. CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b =
  2335. (SELECT MIN(b) FROM t1 WHERE a=outr.a);
  2336. select * from t3;
  2337. abc b
  2338. 1 2
  2339. 2 3
  2340. 3 4
  2341. prepare stmt1 from "INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b = (SELECT MIN(b) FROM t1 WHERE a=outr.a);";
  2342. execute stmt1;
  2343. deallocate prepare stmt1;
  2344. select * from t2;
  2345. c d
  2346. 1 2
  2347. 2 3
  2348. 3 4
  2349. 1 2
  2350. 2 3
  2351. 3 4
  2352. drop table t3;
  2353. prepare stmt1 from "CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b = (SELECT MIN(b) FROM t1 WHERE a=outr.a);";
  2354. execute stmt1;
  2355. select * from t3;
  2356. abc b
  2357. 1 2
  2358. 2 3
  2359. 3 4
  2360. deallocate prepare stmt1;
  2361. DROP TABLE t1, t2, t3;
  2362. CREATE TABLE `t1` ( `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  2363. insert into t1 values (1);
  2364. CREATE TABLE `t2` ( `b` int(11) default NULL, `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  2365. insert into t2 values (1,2);
  2366. select t000.a, count(*) `C` FROM t1 t000 GROUP BY t000.a HAVING count(*) > ALL (SELECT count(*) FROM t2 t001 WHERE t001.a=1);
  2367. a C
  2368. 1 1
  2369. drop table t1,t2;
  2370. create table t1 (a int not null auto_increment primary key, b varchar(40), fulltext(b)) engine=myisam;
  2371. insert into t1 (b) values ('ball'),('ball games'), ('games'), ('foo'), ('foobar'), ('Serg'), ('Sergei'),('Georg'), ('Patrik'),('Hakan');
  2372. create table t2 (a int);
  2373. insert into t2 values (1),(3),(2),(7);
  2374. select a,b from t1 where match(b) against ('Ball') > 0;
  2375. a b
  2376. 1 ball
  2377. 2 ball games
  2378. select a from t2 where a in (select a from t1 where match(b) against ('Ball') > 0);
  2379. a
  2380. 1
  2381. 2
  2382. drop table t1,t2;
  2383. CREATE TABLE t1(`IZAVORGANG_ID` VARCHAR(11) CHARACTER SET latin1 COLLATE latin1_bin,`KUERZEL` VARCHAR(10) CHARACTER SET latin1 COLLATE latin1_bin,`IZAANALYSEART_ID` VARCHAR(11) CHARACTER SET latin1 COLLATE latin1_bin,`IZAPMKZ_ID` VARCHAR(11) CHARACTER SET latin1 COLLATE latin1_bin);
  2384. CREATE INDEX AK01IZAVORGANG ON t1(izaAnalyseart_id,Kuerzel);
  2385. INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000001','601','D0000000001','I0000000001');
  2386. INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000002','602','D0000000001','I0000000001');
  2387. INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000003','603','D0000000001','I0000000001');
  2388. INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000004','101','D0000000001','I0000000001');
  2389. SELECT `IZAVORGANG_ID` FROM t1 WHERE `KUERZEL` IN(SELECT MIN(`KUERZEL`)`Feld1` FROM t1 WHERE `KUERZEL` LIKE'601%'And`IZAANALYSEART_ID`='D0000000001');
  2390. IZAVORGANG_ID
  2391. D0000000001
  2392. drop table t1;
  2393. CREATE TABLE `t1` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY (`aid`,`bid`));
  2394. CREATE TABLE `t2` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY (`aid`,`bid`));
  2395. insert into t1 values (1,1),(1,2),(2,1),(2,2);
  2396. insert into t2 values (1,2),(2,2);
  2397. select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
  2398. aid bid
  2399. 1 1
  2400. 2 1
  2401. alter table t2 drop primary key;
  2402. alter table t2 add key KEY1 (aid, bid);
  2403. select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
  2404. aid bid
  2405. 1 1
  2406. 2 1
  2407. alter table t2 drop key KEY1;
  2408. alter table t2 add primary key (bid, aid);
  2409. select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
  2410. aid bid
  2411. 1 1
  2412. 2 1
  2413. drop table t1,t2;
  2414. CREATE TABLE t1 (howmanyvalues bigint, avalue int);
  2415. INSERT INTO t1 VALUES (1, 1),(2, 1),(2, 2),(3, 1),(3, 2),(3, 3),(4, 1),(4, 2),(4, 3),(4, 4);
  2416. SELECT howmanyvalues, count(*) from t1 group by howmanyvalues;
  2417. howmanyvalues count(*)
  2418. 1 1
  2419. 2 2
  2420. 3 3
  2421. 4 4
  2422. SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues;
  2423. howmanyvalues mycount
  2424. 1 1
  2425. 2 2
  2426. 3 3
  2427. 4 4
  2428. CREATE INDEX t1_howmanyvalues_idx ON t1 (howmanyvalues);
  2429. SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues+1 = a.howmanyvalues+1) as mycount from t1 a group by a.howmanyvalues;
  2430. howmanyvalues mycount
  2431. 1 1
  2432. 2 2
  2433. 3 3
  2434. 4 4
  2435. SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues;
  2436. howmanyvalues mycount
  2437. 1 1
  2438. 2 2
  2439. 3 3
  2440. 4 4
  2441. SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.avalue) as mycount from t1 a group by a.howmanyvalues;
  2442. howmanyvalues mycount
  2443. 1 1
  2444. 2 1
  2445. 3 1
  2446. 4 1
  2447. drop table t1;
  2448. create table t1 (x int);
  2449. select (select b.x from t1 as b where b.x=a.x) from t1 as a where a.x=2 group by a.x;
  2450. (select b.x from t1 as b where b.x=a.x)
  2451. drop table t1;
  2452. CREATE TABLE `t1` ( `master` int(10) unsigned NOT NULL default '0', `map` smallint(6) unsigned NOT NULL default '0', `slave` int(10) unsigned NOT NULL default '0', `access` int(10) unsigned NOT NULL default '0', UNIQUE KEY `access_u` (`master`,`map`,`slave`));
  2453. INSERT INTO `t1` VALUES (1,0,0,700),(1,1,1,400),(1,5,5,400),(1,12,12,400),(1,12,32,400),(4,12,32,400);
  2454. CREATE TABLE `t2` ( `id` int(10) unsigned NOT NULL default '0', `pid` int(10) unsigned NOT NULL default '0', `map` smallint(6) unsigned NOT NULL default '0', `level` tinyint(4) unsigned NOT NULL default '0', `title` varchar(255) default NULL, PRIMARY KEY (`id`,`pid`,`map`), KEY `level` (`level`), KEY `id` (`id`,`map`)) ;
  2455. INSERT INTO `t2` VALUES (6,5,12,7,'a'),(12,0,0,7,'a'),(12,1,0,7,'a'),(12,5,5,7,'a'),(12,5,12,7,'a');
  2456. SELECT b.sc FROM (SELECT (SELECT a.access FROM t1 a WHERE a.map = op.map AND a.slave = op.pid AND a.master = 1) ac FROM t2 op WHERE op.id = 12 AND op.map = 0) b;
  2457. ERROR 42S22: Unknown column 'b.sc' in 'field list'
  2458. SELECT b.ac FROM (SELECT (SELECT a.access FROM t1 a WHERE a.map = op.map AND a.slave = op.pid AND a.master = 1) ac FROM t2 op WHERE op.id = 12 AND op.map = 0) b;
  2459. ac
  2460. 700
  2461. NULL
  2462. drop tables t1,t2;
  2463. create table t1 (a int not null, b int not null, c int, primary key (a,b));
  2464. insert into t1 values (1,1,1), (2,2,2), (3,3,3);
  2465. set @b:= 0;
  2466. explain select sum(a) from t1 where b > @b;
  2467. id select_type table type possible_keys key key_len ref rows Extra
  2468. 1 SIMPLE t1 index NULL PRIMARY 8 NULL 3 Using where; Using index
  2469. set @a:= (select sum(a) from t1 where b > @b);
  2470. explain select a from t1 where c=2;
  2471. id select_type table type possible_keys key key_len ref rows Extra
  2472. 1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using where
  2473. do @a:= (select sum(a) from t1 where b > @b);
  2474. explain select a from t1 where c=2;
  2475. id select_type table type possible_keys key key_len ref rows Extra
  2476. 1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using where
  2477. drop table t1;
  2478. set @got_val= (SELECT 1 FROM (SELECT 'A' as my_col) as T1 ) ;
  2479. create table t1 (a int, b int);
  2480. create table t2 (a int, b int);
  2481. insert into t1 values (1,1),(1,2),(1,3),(2,4),(2,5);
  2482. insert into t2 values (1,3),(2,1);
  2483. select distinct a,b, (select max(b) from t2 where t1.b=t2.a) from t1 order by t1.b;
  2484. a b (select max(b) from t2 where t1.b=t2.a)
  2485. 1 1 3
  2486. 1 2 1
  2487. 1 3 NULL
  2488. 2 4 NULL
  2489. 2 5 NULL
  2490. drop table t1, t2;
  2491. create table t1 (id int);
  2492. create table t2 (id int, body text, fulltext (body)) engine=myisam;
  2493. insert into t1 values(1),(2),(3);
  2494. insert into t2 values (1,'test'), (2,'mysql'), (3,'test'), (4,'test');
  2495. select count(distinct id) from t1 where id in (select id from t2 where match(body) against ('mysql' in boolean mode));
  2496. count(distinct id)
  2497. 1
  2498. drop table t2,t1;
  2499. create table t1 (s1 int,s2 int);
  2500. insert into t1 values (20,15);
  2501. select * from t1 where (('a',null) <=> (select 'a',s2 from t1 where s1 = 0));
  2502. s1 s2
  2503. drop table t1;
  2504. create table t1 (s1 int);
  2505. insert into t1 values (1),(null);
  2506. select * from t1 where s1 < all (select s1 from t1);
  2507. s1
  2508. select s1, s1 < all (select s1 from t1) from t1;
  2509. s1 s1 < all (select s1 from t1)
  2510. 1 0
  2511. NULL NULL
  2512. drop table t1;
  2513. CREATE TABLE t1 (
  2514. Code char(3) NOT NULL default '',
  2515. Name char(52) NOT NULL default '',
  2516. Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
  2517. Region char(26) NOT NULL default '',
  2518. SurfaceArea float(10,2) NOT NULL default '0.00',
  2519. IndepYear smallint(6) default NULL,
  2520. Population int(11) NOT NULL default '0',
  2521. LifeExpectancy float(3,1) default NULL,
  2522. GNP float(10,2) default NULL,
  2523. GNPOld float(10,2) default NULL,
  2524. LocalName char(45) NOT NULL default '',
  2525. GovernmentForm char(45) NOT NULL default '',
  2526. HeadOfState char(60) default NULL,
  2527. Capital int(11) default NULL,
  2528. Code2 char(2) NOT NULL default ''
  2529. ) ENGINE=MyISAM;
  2530. INSERT INTO t1 VALUES ('XXX','Xxxxx','Oceania','Xxxxxx',26.00,0,0,0,0,0,'Xxxxx','Xxxxx','Xxxxx',NULL,'XX');
  2531. INSERT INTO t1 VALUES ('ASM','American Samoa','Oceania','Polynesia',199.00,0,68000,75.1,334.00,NULL,'Amerika Samoa','US Territory','George W. Bush',54,'AS');
  2532. INSERT INTO t1 VALUES ('ATF','French Southern territories','Antarctica','Antarctica',7780.00,0,0,NULL,0.00,NULL,'Terres australes françaises','Nonmetropolitan Territory of France','Jacques Chirac',NULL,'TF');
  2533. INSERT INTO t1 VALUES ('UMI','United States Minor Outlying Islands','Oceania','Micronesia/Caribbean',16.00,0,0,NULL,0.00,NULL,'United States Minor Outlying Islands','Dependent Territory of the US','George W. Bush',NULL,'UM');
  2534. /*!40000 ALTER TABLE t1 ENABLE KEYS */;
  2535. SELECT DISTINCT Continent AS c FROM t1 outr WHERE
  2536. Code <> SOME ( SELECT Code FROM t1 WHERE Continent = outr.Continent AND
  2537. Population < 200);
  2538. c
  2539. Oceania
  2540. drop table t1;
  2541. create table t1 (a1 int);
  2542. create table t2 (b1 int);
  2543. select * from t1 where a2 > any(select b1 from t2);
  2544. ERROR 42S22: Unknown column 'a2' in 'IN/ALL/ANY subquery'
  2545. select * from t1 where a1 > any(select b1 from t2);
  2546. a1
  2547. drop table t1,t2;
  2548. create table t1 (a integer, b integer);
  2549. select (select * from t1) = (select 1,2);
  2550. (select * from t1) = (select 1,2)
  2551. NULL
  2552. select (select 1,2) = (select * from t1);
  2553. (select 1,2) = (select * from t1)
  2554. NULL
  2555. select row(1,2) = ANY (select * from t1);
  2556. row(1,2) = ANY (select * from t1)
  2557. 0
  2558. select row(1,2) != ALL (select * from t1);
  2559. row(1,2) != ALL (select * from t1)
  2560. 1
  2561. drop table t1;
  2562. create table t1 (a integer, b integer);
  2563. select row(1,(2,2)) in (select * from t1 );
  2564. ERROR 21000: Operand should contain 2 column(s)
  2565. select row(1,(2,2)) = (select * from t1 );
  2566. ERROR 21000: Operand should contain 2 column(s)
  2567. select (select * from t1) = row(1,(2,2));
  2568. ERROR 21000: Operand should contain 1 column(s)
  2569. drop table t1;
  2570. create table t1 (a integer);
  2571. insert into t1 values (1);
  2572. select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx ;
  2573. ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
  2574. select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
  2575. ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
  2576. select 1 as xx, 1 = ALL ( select 1 from t1 where 1 = xx );
  2577. xx 1 = ALL ( select 1 from t1 where 1 = xx )
  2578. 1 1
  2579. select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
  2580. ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
  2581. select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx from DUAL;
  2582. ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
  2583. drop table t1;
  2584. CREATE TABLE t1 (
  2585. categoryId int(11) NOT NULL,
  2586. courseId int(11) NOT NULL,
  2587. startDate datetime NOT NULL,
  2588. endDate datetime NOT NULL,
  2589. createDate datetime NOT NULL,
  2590. modifyDate timestamp NOT NULL,
  2591. attributes text NOT NULL
  2592. );
  2593. INSERT INTO t1 VALUES (1,41,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
  2594. (1,86,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
  2595. (1,87,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
  2596. (2,52,'2004-03-15','2004-10-01','2004-03-15','2004-09-17',''),
  2597. (2,53,'2004-03-16','2004-10-01','2004-03-16','2004-09-17',''),
  2598. (2,88,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
  2599. (2,89,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
  2600. (3,51,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
  2601. (5,12,'2004-02-18','2010-01-01','2004-02-18','2004-02-18','');
  2602. CREATE TABLE t2 (
  2603. userId int(11) NOT NULL,
  2604. courseId int(11) NOT NULL,
  2605. date datetime NOT NULL
  2606. );
  2607. INSERT INTO t2 VALUES (5141,71,'2003-11-18'),
  2608. (5141,72,'2003-11-25'),(5141,41,'2004-08-06'),
  2609. (5141,52,'2004-08-06'),(5141,53,'2004-08-06'),
  2610. (5141,12,'2004-08-06'),(5141,86,'2004-10-21'),
  2611. (5141,87,'2004-10-21'),(5141,88,'2004-10-21'),
  2612. (5141,89,'2004-10-22'),(5141,51,'2004-10-26');
  2613. CREATE TABLE t3 (
  2614. groupId int(11) NOT NULL,
  2615. parentId int(11) NOT NULL,
  2616. startDate datetime NOT NULL,
  2617. endDate datetime NOT NULL,
  2618. createDate datetime NOT NULL,
  2619. modifyDate timestamp NOT NULL,
  2620. ordering int(11)
  2621. );
  2622. INSERT INTO t3 VALUES (12,9,'1000-01-01','3999-12-31','2004-01-29','2004-01-29',NULL);
  2623. CREATE TABLE t4 (
  2624. id int(11) NOT NULL,
  2625. groupTypeId int(11) NOT NULL,
  2626. groupKey varchar(50) NOT NULL,
  2627. name text,
  2628. ordering int(11),
  2629. description text,
  2630. createDate datetime NOT NULL,
  2631. modifyDate timestamp NOT NULL
  2632. );
  2633. INSERT INTO t4 VALUES (9,5,'stationer','stationer',0,'Stationer','2004-01-29','2004-01-29'),
  2634. (12,5,'group2','group2',0,'group2','2004-01-29','2004-01-29');
  2635. CREATE TABLE t5 (
  2636. userId int(11) NOT NULL,
  2637. groupId int(11) NOT NULL,
  2638. createDate datetime NOT NULL,
  2639. modifyDate timestamp NOT NULL
  2640. );
  2641. INSERT INTO t5 VALUES (5141,12,'2004-08-06','2004-08-06');
  2642. select
  2643. count(distinct t2.userid) pass,
  2644. groupstuff.*,
  2645. count(t2.courseid) crse,
  2646. t1.categoryid,
  2647. t2.courseid,
  2648. date_format(date, '%b%y') as colhead
  2649. from t2
  2650. join t1 on t2.courseid=t1.courseid
  2651. join
  2652. (
  2653. select
  2654. t5.userid,
  2655. parentid,
  2656. parentgroup,
  2657. childid,
  2658. groupname,
  2659. grouptypeid
  2660. from t5
  2661. join
  2662. (
  2663. select t4.id as parentid,
  2664. t4.name as parentgroup,
  2665. t4.id as childid,
  2666. t4.name as groupname,
  2667. t4.grouptypeid
  2668. from t4
  2669. ) as gin on t5.groupid=gin.childid
  2670. ) as groupstuff on t2.userid = groupstuff.userid
  2671. group by
  2672. groupstuff.groupname, colhead , t2.courseid;
  2673. pass userid parentid parentgroup childid groupname grouptypeid crse categoryid courseid colhead
  2674. 1 5141 12 group2 12 group2 5 1 5 12 Aug04
  2675. 1 5141 12 group2 12 group2 5 1 1 41 Aug04
  2676. 1 5141 12 group2 12 group2 5 1 2 52 Aug04
  2677. 1 5141 12 group2 12 group2 5 1 2 53 Aug04
  2678. 1 5141 12 group2 12 group2 5 1 3 51 Oct04
  2679. 1 5141 12 group2 12 group2 5 1 1 86 Oct04
  2680. 1 5141 12 group2 12 group2 5 1 1 87 Oct04
  2681. 1 5141 12 group2 12 group2 5 1 2 88 Oct04
  2682. 1 5141 12 group2 12 group2 5 1 2 89 Oct04
  2683. drop table t1, t2, t3, t4, t5;
  2684. create table t1 (a int);
  2685. insert into t1 values (1), (2), (3);
  2686. SELECT 1 FROM t1 WHERE (SELECT 1) in (SELECT 1);
  2687. 1
  2688. 1
  2689. 1
  2690. 1
  2691. drop table t1;
  2692. create table t1 (a int);
  2693. create table t2 (a int);
  2694. insert into t1 values (1),(2);
  2695. insert into t2 values (0),(1),(2),(3);
  2696. select a from t2 where a in (select a from t1);
  2697. a
  2698. 1
  2699. 2
  2700. select a from t2 having a in (select a from t1);
  2701. a
  2702. 1
  2703. 2
  2704. prepare stmt1 from "select a from t2 where a in (select a from t1)";
  2705. execute stmt1;
  2706. a
  2707. 1
  2708. 2
  2709. execute stmt1;
  2710. a
  2711. 1
  2712. 2
  2713. deallocate prepare stmt1;
  2714. prepare stmt1 from "select a from t2 having a in (select a from t1)";
  2715. execute stmt1;
  2716. a
  2717. 1
  2718. 2
  2719. execute stmt1;
  2720. a
  2721. 1
  2722. 2
  2723. deallocate prepare stmt1;
  2724. drop table t1, t2;
  2725. create table t1 (a int, b int);
  2726. insert into t1 values (1,2);
  2727. select 1 = (select * from t1);
  2728. ERROR 21000: Operand should contain 1 column(s)
  2729. select (select * from t1) = 1;
  2730. ERROR 21000: Operand should contain 2 column(s)
  2731. select (1,2) = (select a from t1);
  2732. ERROR 21000: Operand should contain 2 column(s)
  2733. select (select a from t1) = (1,2);
  2734. ERROR 21000: Operand should contain 1 column(s)
  2735. select (1,2,3) = (select * from t1);
  2736. ERROR 21000: Operand should contain 3 column(s)
  2737. select (select * from t1) = (1,2,3);
  2738. ERROR 21000: Operand should contain 2 column(s)
  2739. drop table t1;
  2740. CREATE TABLE `t1` (
  2741. `itemid` bigint(20) unsigned NOT NULL auto_increment,
  2742. `sessionid` bigint(20) unsigned default NULL,
  2743. `time` int(10) unsigned NOT NULL default '0',
  2744. `type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT
  2745. NULL default '',
  2746. `data` text collate latin1_general_ci NOT NULL,
  2747. PRIMARY KEY (`itemid`)
  2748. ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
  2749. INSERT INTO `t1` VALUES (1, 1, 1, 'D', '');
  2750. CREATE TABLE `t2` (
  2751. `sessionid` bigint(20) unsigned NOT NULL auto_increment,
  2752. `pid` int(10) unsigned NOT NULL default '0',
  2753. `date` int(10) unsigned NOT NULL default '0',
  2754. `ip` varchar(15) collate latin1_general_ci NOT NULL default '',
  2755. PRIMARY KEY (`sessionid`)
  2756. ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
  2757. INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
  2758. SELECT s.ip, count( e.itemid ) FROM `t1` e JOIN t2 s ON s.sessionid = e.sessionid WHERE e.sessionid = ( SELECT sessionid FROM t2 ORDER BY sessionid DESC LIMIT 1 ) GROUP BY s.ip HAVING count( e.itemid ) >0 LIMIT 0 , 30;
  2759. ip count( e.itemid )
  2760. 10.10.10.1 1
  2761. drop tables t1,t2;
  2762. create table t1 (fld enum('0','1'));
  2763. insert into t1 values ('1');
  2764. select * from (select max(fld) from t1) as foo;
  2765. max(fld)
  2766. 1
  2767. drop table t1;
  2768. set @save_optimizer_switch=@@optimizer_switch;
  2769. set @@optimizer_switch="partial_match_rowid_merge=off,partial_match_table_scan=off";
  2770. SET optimizer_switch='semijoin_with_cache=off';
  2771. CREATE TABLE t1 (one int, two int, flag char(1));
  2772. CREATE TABLE t2 (one int, two int, flag char(1));
  2773. INSERT INTO t1 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N');
  2774. INSERT INTO t2 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N');
  2775. SELECT * FROM t1
  2776. WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t2 WHERE flag = 'N');
  2777. one two flag
  2778. 5 6 N
  2779. 7 8 N
  2780. SELECT * FROM t1
  2781. WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t1 WHERE flag = 'N');
  2782. one two flag
  2783. 5 6 N
  2784. 7 8 N
  2785. insert into t2 values (null,null,'N');
  2786. insert into t2 values (null,3,'0');
  2787. insert into t2 values (null,5,'0');
  2788. insert into t2 values (10,null,'0');
  2789. insert into t1 values (10,3,'0');
  2790. insert into t1 values (10,5,'0');
  2791. insert into t1 values (10,10,'0');
  2792. SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N') as 'test' from t1;
  2793. one two test
  2794. 1 2 NULL
  2795. 2 3 NULL
  2796. 3 4 NULL
  2797. 5 6 1
  2798. 7 8 1
  2799. 10 3 NULL
  2800. 10 5 NULL
  2801. 10 10 NULL
  2802. SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N');
  2803. one two
  2804. 5 6
  2805. 7 8
  2806. SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N' group by one,two) as 'test' from t1;
  2807. one two test
  2808. 1 2 NULL
  2809. 2 3 NULL
  2810. 3 4 NULL
  2811. 5 6 1
  2812. 7 8 1
  2813. 10 3 NULL
  2814. 10 5 NULL
  2815. 10 10 NULL
  2816. SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1;
  2817. one two test
  2818. 1 2 0
  2819. 2 3 NULL
  2820. 3 4 0
  2821. 5 6 0
  2822. 7 8 0
  2823. 10 3 NULL
  2824. 10 5 NULL
  2825. 10 10 NULL
  2826. SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1;
  2827. one two test
  2828. 1 2 0
  2829. 2 3 NULL
  2830. 3 4 0
  2831. 5 6 0
  2832. 7 8 0
  2833. 10 3 NULL
  2834. 10 5 NULL
  2835. 10 10 NULL
  2836. explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1;
  2837. id select_type table type possible_keys key key_len ref rows filtered Extra
  2838. 1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00
  2839. 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where
  2840. Warnings:
  2841. Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`))) and trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`)))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`)))))) AS `test` from `test`.`t1`
  2842. explain extended SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N');
  2843. id select_type table type possible_keys key key_len ref rows filtered Extra
  2844. 1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00
  2845. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1 100.00
  2846. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 9 100.00 Using where
  2847. Warnings:
  2848. Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t2`.`flag` = 'N'))
  2849. explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1;
  2850. id select_type table type possible_keys key key_len ref rows filtered Extra
  2851. 1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00
  2852. 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where
  2853. Warnings:
  2854. Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),<exists>(select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where ((`test`.`t2`.`flag` = '0') and trigcond(((<cache>(`test`.`t1`.`one`) = `test`.`t2`.`one`) or isnull(`test`.`t2`.`one`))) and trigcond(((<cache>(`test`.`t1`.`two`) = `test`.`t2`.`two`) or isnull(`test`.`t2`.`two`)))) having (trigcond(<is_not_null_test>(`test`.`t2`.`one`)) and trigcond(<is_not_null_test>(`test`.`t2`.`two`)))))) AS `test` from `test`.`t1`
  2855. DROP TABLE t1,t2;
  2856. set @@optimizer_switch=@save_optimizer_switch;
  2857. CREATE TABLE t1 (a char(5), b char(5));
  2858. INSERT INTO t1 VALUES (NULL,'aaa'), ('aaa','aaa');
  2859. SELECT * FROM t1 WHERE (a,b) IN (('aaa','aaa'), ('aaa','bbb'));
  2860. a b
  2861. aaa aaa
  2862. DROP TABLE t1;
  2863. CREATE TABLE t1 (a int);
  2864. CREATE TABLE t2 (a int, b int);
  2865. CREATE TABLE t3 (b int NOT NULL);
  2866. INSERT INTO t1 VALUES (1), (2), (3), (4);
  2867. INSERT INTO t2 VALUES (1,10), (3,30);
  2868. SELECT * FROM t2 LEFT JOIN t3 ON t2.b=t3.b
  2869. WHERE t3.b IS NOT NULL OR t2.a > 10;
  2870. a b b
  2871. SELECT * FROM t1
  2872. WHERE t1.a NOT IN (SELECT a FROM t2 LEFT JOIN t3 ON t2.b=t3.b
  2873. WHERE t3.b IS NOT NULL OR t2.a > 10);
  2874. a
  2875. 1
  2876. 2
  2877. 3
  2878. 4
  2879. DROP TABLE t1,t2,t3;
  2880. CREATE TABLE t1 (f1 INT);
  2881. CREATE TABLE t2 (f2 INT);
  2882. INSERT INTO t1 VALUES (1);
  2883. SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2);
  2884. f1
  2885. 1
  2886. SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE 1=0);
  2887. f1
  2888. 1
  2889. INSERT INTO t2 VALUES (1);
  2890. INSERT INTO t2 VALUES (2);
  2891. SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE f2=0);
  2892. f1
  2893. 1
  2894. DROP TABLE t1, t2;
  2895. select 1 from dual where 1 < any (select 2);
  2896. 1
  2897. 1
  2898. select 1 from dual where 1 < all (select 2);
  2899. 1
  2900. 1
  2901. select 1 from dual where 2 > any (select 1);
  2902. 1
  2903. 1
  2904. select 1 from dual where 2 > all (select 1);
  2905. 1
  2906. 1
  2907. select 1 from dual where 1 < any (select 2 from dual);
  2908. 1
  2909. 1
  2910. select 1 from dual where 1 < all (select 2 from dual where 1!=1);
  2911. 1
  2912. 1
  2913. create table t1 (s1 char);
  2914. insert into t1 values (1),(2);
  2915. select * from t1 where (s1 < any (select s1 from t1));
  2916. s1
  2917. 1
  2918. select * from t1 where not (s1 < any (select s1 from t1));
  2919. s1
  2920. 2
  2921. select * from t1 where (s1 < ALL (select s1+1 from t1));
  2922. s1
  2923. 1
  2924. select * from t1 where not(s1 < ALL (select s1+1 from t1));
  2925. s1
  2926. 2
  2927. select * from t1 where (s1+1 = ANY (select s1 from t1));
  2928. s1
  2929. 1
  2930. select * from t1 where NOT(s1+1 = ANY (select s1 from t1));
  2931. s1
  2932. 2
  2933. select * from t1 where (s1 = ALL (select s1/s1 from t1));
  2934. s1
  2935. 1
  2936. select * from t1 where NOT(s1 = ALL (select s1/s1 from t1));
  2937. s1
  2938. 2
  2939. drop table t1;
  2940. create table t1 (
  2941. retailerID varchar(8) NOT NULL,
  2942. statusID int(10) unsigned NOT NULL,
  2943. changed datetime NOT NULL,
  2944. UNIQUE KEY retailerID (retailerID, statusID, changed)
  2945. );
  2946. INSERT INTO t1 VALUES("0026", "1", "2005-12-06 12:18:56");
  2947. INSERT INTO t1 VALUES("0026", "2", "2006-01-06 12:25:53");
  2948. INSERT INTO t1 VALUES("0037", "1", "2005-12-06 12:18:56");
  2949. INSERT INTO t1 VALUES("0037", "2", "2006-01-06 12:25:53");
  2950. INSERT INTO t1 VALUES("0048", "1", "2006-01-06 12:37:50");
  2951. INSERT INTO t1 VALUES("0059", "1", "2006-01-06 12:37:50");
  2952. select * from t1 r1
  2953. where (r1.retailerID,(r1.changed)) in
  2954. (SELECT r2.retailerId,(max(changed)) from t1 r2
  2955. group by r2.retailerId);
  2956. retailerID statusID changed
  2957. 0026 2 2006-01-06 12:25:53
  2958. 0037 2 2006-01-06 12:25:53
  2959. 0048 1 2006-01-06 12:37:50
  2960. 0059 1 2006-01-06 12:37:50
  2961. drop table t1;
  2962. create table t1(a int, primary key (a));
  2963. insert into t1 values (10);
  2964. create table t2 (a int primary key, b varchar(32), c int, unique key b(c, b));
  2965. insert into t2(a, c, b) values (1,10,'359'), (2,10,'35988'), (3,10,'35989');
  2966. explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
  2967. ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
  2968. ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
  2969. id select_type table type possible_keys key key_len ref rows Extra
  2970. 1 PRIMARY t1 const PRIMARY PRIMARY 4 const 1 Using index
  2971. 1 PRIMARY r eq_ref PRIMARY PRIMARY 4 const 1 Using where
  2972. 2 DEPENDENT SUBQUERY t2 range b b 40 NULL 1 Using where
  2973. SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
  2974. ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
  2975. ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
  2976. a a b
  2977. 10 3 35989
  2978. explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
  2979. ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
  2980. ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
  2981. id select_type table type possible_keys key key_len ref rows Extra
  2982. 1 PRIMARY t1 const PRIMARY PRIMARY 4 const 1 Using index
  2983. 1 PRIMARY r eq_ref PRIMARY PRIMARY 4 const 1 Using where
  2984. 2 DEPENDENT SUBQUERY t2 range b b 40 NULL 1 Using where
  2985. SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
  2986. ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
  2987. ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
  2988. a a b
  2989. 10 1 359
  2990. drop table t1,t2;
  2991. CREATE TABLE t1 (
  2992. field1 int NOT NULL,
  2993. field2 int NOT NULL,
  2994. field3 int NOT NULL,
  2995. PRIMARY KEY (field1,field2,field3)
  2996. );
  2997. CREATE TABLE t2 (
  2998. fieldA int NOT NULL,
  2999. fieldB int NOT NULL,
  3000. PRIMARY KEY (fieldA,fieldB)
  3001. );
  3002. INSERT INTO t1 VALUES
  3003. (1,1,1), (1,1,2), (1,2,1), (1,2,2), (1,2,3), (1,3,1);
  3004. INSERT INTO t2 VALUES (1,1), (1,2), (1,3);
  3005. SELECT field1, field2, COUNT(*)
  3006. FROM t1 GROUP BY field1, field2;
  3007. field1 field2 COUNT(*)
  3008. 1 1 2
  3009. 1 2 3
  3010. 1 3 1
  3011. SELECT field1, field2
  3012. FROM t1
  3013. GROUP BY field1, field2
  3014. HAVING COUNT(*) >= ALL (SELECT fieldB
  3015. FROM t2 WHERE fieldA = field1);
  3016. field1 field2
  3017. 1 2
  3018. SELECT field1, field2
  3019. FROM t1
  3020. GROUP BY field1, field2
  3021. HAVING COUNT(*) < ANY (SELECT fieldB
  3022. FROM t2 WHERE fieldA = field1);
  3023. field1 field2
  3024. 1 1
  3025. 1 3
  3026. DROP TABLE t1, t2;
  3027. set @save_optimizer_switch=@@optimizer_switch;
  3028. set @@optimizer_switch="partial_match_rowid_merge=off,partial_match_table_scan=off";
  3029. CREATE TABLE t1(a int, INDEX (a));
  3030. INSERT INTO t1 VALUES (1), (3), (5), (7);
  3031. INSERT INTO t1 VALUES (NULL);
  3032. CREATE TABLE t2(a int);
  3033. INSERT INTO t2 VALUES (1),(2),(3);
  3034. EXPLAIN SELECT a, a IN (SELECT a FROM t1) FROM t2;
  3035. id select_type table type possible_keys key key_len ref rows Extra
  3036. 1 PRIMARY t2 ALL NULL NULL NULL NULL 3
  3037. 2 DEPENDENT SUBQUERY t1 index_subquery a a 5 func 2 Using index; Full scan on NULL key
  3038. SELECT a, a IN (SELECT a FROM t1) FROM t2;
  3039. a a IN (SELECT a FROM t1)
  3040. 1 1
  3041. 2 NULL
  3042. 3 1
  3043. DROP TABLE t1,t2;
  3044. set @@optimizer_switch=@save_optimizer_switch;
  3045. CREATE TABLE t1 (a DATETIME);
  3046. INSERT INTO t1 VALUES ('1998-09-23'), ('2003-03-25');
  3047. CREATE TABLE t2 AS SELECT
  3048. (SELECT a FROM t1 WHERE a < '2000-01-01') AS sub_a
  3049. FROM t1 WHERE a > '2000-01-01';
  3050. SHOW CREATE TABLE t2;
  3051. Table Create Table
  3052. t2 CREATE TABLE `t2` (
  3053. `sub_a` datetime DEFAULT NULL
  3054. ) ENGINE=PBXT DEFAULT CHARSET=latin1
  3055. CREATE TABLE t3 AS (SELECT a FROM t1 WHERE a < '2000-01-01') UNION (SELECT a FROM t1 WHERE a > '2000-01-01');
  3056. SHOW CREATE TABLE t3;
  3057. Table Create Table
  3058. t3 CREATE TABLE `t3` (
  3059. `a` datetime DEFAULT NULL
  3060. ) ENGINE=PBXT DEFAULT CHARSET=latin1
  3061. DROP TABLE t1,t2,t3;
  3062. CREATE TABLE t1 (a int);
  3063. INSERT INTO t1 VALUES (1), (2);
  3064. SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) > 0;
  3065. a
  3066. SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL;
  3067. a
  3068. 1
  3069. 2
  3070. EXPLAIN SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL;
  3071. id select_type table type possible_keys key key_len ref rows Extra
  3072. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2
  3073. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  3074. DROP TABLE t1;
  3075. CREATE TABLE t1 (a int);
  3076. INSERT INTO t1 VALUES (2), (4), (1), (3);
  3077. CREATE TABLE t2 (b int, c int);
  3078. INSERT INTO t2 VALUES
  3079. (2,1), (1,3), (2,1), (4,4), (2,2), (1,4);
  3080. SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2 );
  3081. a
  3082. 2
  3083. 4
  3084. 1
  3085. 3
  3086. SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1);
  3087. ERROR 21000: Subquery returns more than 1 row
  3088. SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2), a;
  3089. a
  3090. 1
  3091. 2
  3092. 3
  3093. 4
  3094. SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1), a;
  3095. ERROR 21000: Subquery returns more than 1 row
  3096. SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 2);
  3097. b MAX(c)
  3098. 1 4
  3099. 2 2
  3100. 4 4
  3101. SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 1);
  3102. ERROR 21000: Subquery returns more than 1 row
  3103. SELECT a FROM t1 GROUP BY a
  3104. HAVING IFNULL((SELECT b FROM t2 WHERE b > 2),
  3105. (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
  3106. a
  3107. 1
  3108. 2
  3109. 3
  3110. 4
  3111. SELECT a FROM t1 GROUP BY a
  3112. HAVING IFNULL((SELECT b FROM t2 WHERE b > 1),
  3113. (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
  3114. ERROR 21000: Subquery returns more than 1 row
  3115. SELECT a FROM t1 GROUP BY a
  3116. HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
  3117. (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
  3118. a
  3119. 4
  3120. SELECT a FROM t1 GROUP BY a
  3121. HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
  3122. (SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)) > 3;
  3123. ERROR 21000: Subquery returns more than 1 row
  3124. SELECT a FROM t1
  3125. ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 2),
  3126. (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b));
  3127. a
  3128. 1
  3129. 2
  3130. 3
  3131. 4
  3132. SELECT a FROM t1
  3133. ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 1),
  3134. (SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b));
  3135. ERROR 21000: Subquery returns more than 1 row
  3136. SELECT a FROM t1
  3137. ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
  3138. (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b));
  3139. a
  3140. 1
  3141. 2
  3142. 3
  3143. 4
  3144. SELECT a FROM t1
  3145. ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
  3146. (SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b));
  3147. ERROR 21000: Subquery returns more than 1 row
  3148. DROP TABLE t1,t2;
  3149. create table t1 (df decimal(5,1));
  3150. insert into t1 values(1.1);
  3151. insert into t1 values(2.2);
  3152. select * from t1 where df <= all (select avg(df) from t1 group by df);
  3153. df
  3154. 1.1
  3155. select * from t1 where df >= all (select avg(df) from t1 group by df);
  3156. df
  3157. 2.2
  3158. drop table t1;
  3159. create table t1 (df decimal(5,1));
  3160. insert into t1 values(1.1);
  3161. select 1.1 * exists(select * from t1);
  3162. 1.1 * exists(select * from t1)
  3163. 1.1
  3164. drop table t1;
  3165. CREATE TABLE t1 (
  3166. grp int(11) default NULL,
  3167. a decimal(10,2) default NULL);
  3168. insert into t1 values (1, 1), (2, 2), (2, 3), (3, 4), (3, 5), (3, 6), (NULL, NULL);
  3169. select * from t1;
  3170. grp a
  3171. 1 1.00
  3172. 2 2.00
  3173. 2 3.00
  3174. 3 4.00
  3175. 3 5.00
  3176. 3 6.00
  3177. NULL NULL
  3178. select min(a) from t1 group by grp;
  3179. min(a)
  3180. NULL
  3181. 1.00
  3182. 2.00
  3183. 4.00
  3184. drop table t1;
  3185. CREATE table t1 ( c1 integer );
  3186. INSERT INTO t1 VALUES ( 1 );
  3187. INSERT INTO t1 VALUES ( 2 );
  3188. INSERT INTO t1 VALUES ( 3 );
  3189. CREATE TABLE t2 ( c2 integer );
  3190. INSERT INTO t2 VALUES ( 1 );
  3191. INSERT INTO t2 VALUES ( 4 );
  3192. INSERT INTO t2 VALUES ( 5 );
  3193. SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2 WHERE c2 IN (1);
  3194. c1 c2
  3195. 1 1
  3196. SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2
  3197. WHERE c2 IN ( SELECT c2 FROM t2 WHERE c2 IN ( 1 ) );
  3198. c1 c2
  3199. 1 1
  3200. DROP TABLE t1,t2;
  3201. CREATE TABLE t1 ( c1 integer );
  3202. INSERT INTO t1 VALUES ( 1 );
  3203. INSERT INTO t1 VALUES ( 2 );
  3204. INSERT INTO t1 VALUES ( 3 );
  3205. INSERT INTO t1 VALUES ( 6 );
  3206. CREATE TABLE t2 ( c2 integer );
  3207. INSERT INTO t2 VALUES ( 1 );
  3208. INSERT INTO t2 VALUES ( 4 );
  3209. INSERT INTO t2 VALUES ( 5 );
  3210. INSERT INTO t2 VALUES ( 6 );
  3211. CREATE TABLE t3 ( c3 integer );
  3212. INSERT INTO t3 VALUES ( 7 );
  3213. INSERT INTO t3 VALUES ( 8 );
  3214. SELECT c1,c2 FROM t1 LEFT JOIN t2 ON c1 = c2
  3215. WHERE EXISTS (SELECT c3 FROM t3 WHERE c2 IS NULL );
  3216. c1 c2
  3217. 2 NULL
  3218. 3 NULL
  3219. DROP TABLE t1,t2,t3;
  3220. CREATE TABLE `t1` (
  3221. `itemid` bigint(20) unsigned NOT NULL auto_increment,
  3222. `sessionid` bigint(20) unsigned default NULL,
  3223. `time` int(10) unsigned NOT NULL default '0',
  3224. `type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT
  3225. NULL default '',
  3226. `data` text collate latin1_general_ci NOT NULL,
  3227. PRIMARY KEY (`itemid`)
  3228. ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
  3229. INSERT INTO `t1` VALUES (1, 1, 1, 'D', '');
  3230. CREATE TABLE `t2` (
  3231. `sessionid` bigint(20) unsigned NOT NULL auto_increment,
  3232. `pid` int(10) unsigned NOT NULL default '0',
  3233. `date` int(10) unsigned NOT NULL default '0',
  3234. `ip` varchar(15) collate latin1_general_ci NOT NULL default '',
  3235. PRIMARY KEY (`sessionid`)
  3236. ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
  3237. INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
  3238. SELECT s.ip, count( e.itemid ) FROM `t1` e JOIN t2 s ON s.sessionid = e.sessionid WHERE e.sessionid = ( SELECT sessionid FROM t2 ORDER BY sessionid DESC LIMIT 1 ) GROUP BY s.ip HAVING count( e.itemid ) >0 LIMIT 0 , 30;
  3239. ip count( e.itemid )
  3240. 10.10.10.1 1
  3241. drop tables t1,t2;
  3242. CREATE TABLE t1 (EMPNUM CHAR(3));
  3243. CREATE TABLE t2 (EMPNUM CHAR(3) );
  3244. INSERT INTO t1 VALUES ('E1'),('E2');
  3245. INSERT INTO t2 VALUES ('E1');
  3246. DELETE FROM t1
  3247. WHERE t1.EMPNUM NOT IN
  3248. (SELECT t2.EMPNUM
  3249. FROM t2
  3250. WHERE t1.EMPNUM = t2.EMPNUM);
  3251. select * from t1;
  3252. EMPNUM
  3253. E1
  3254. DROP TABLE t1,t2;
  3255. CREATE TABLE t1(select_id BIGINT, values_id BIGINT);
  3256. INSERT INTO t1 VALUES (1, 1);
  3257. CREATE TABLE t2 (select_id BIGINT, values_id BIGINT,
  3258. PRIMARY KEY(select_id,values_id));
  3259. INSERT INTO t2 VALUES (0, 1), (0, 2), (0, 3), (1, 5);
  3260. SELECT values_id FROM t1
  3261. WHERE values_id IN (SELECT values_id FROM t2
  3262. WHERE select_id IN (1, 0));
  3263. values_id
  3264. 1
  3265. SELECT values_id FROM t1
  3266. WHERE values_id IN (SELECT values_id FROM t2
  3267. WHERE select_id BETWEEN 0 AND 1);
  3268. values_id
  3269. 1
  3270. SELECT values_id FROM t1
  3271. WHERE values_id IN (SELECT values_id FROM t2
  3272. WHERE select_id = 0 OR select_id = 1);
  3273. values_id
  3274. 1
  3275. DROP TABLE t1, t2;
  3276. create table t1 (fld enum('0','1'));
  3277. insert into t1 values ('1');
  3278. select * from (select max(fld) from t1) as foo;
  3279. max(fld)
  3280. 1
  3281. drop table t1;
  3282. CREATE TABLE t1 (a int, b int);
  3283. CREATE TABLE t2 (c int, d int);
  3284. CREATE TABLE t3 (e int);
  3285. INSERT INTO t1 VALUES
  3286. (1,10), (2,10), (1,20), (2,20), (3,20), (2,30), (4,40);
  3287. INSERT INTO t2 VALUES
  3288. (2,10), (2,20), (4,10), (5,10), (3,20), (2,40);
  3289. INSERT INTO t3 VALUES (10), (30), (10), (20) ;
  3290. SELECT a, MAX(b), MIN(b) FROM t1 GROUP BY a;
  3291. a MAX(b) MIN(b)
  3292. 1 20 10
  3293. 2 30 10
  3294. 3 20 20
  3295. 4 40 40
  3296. SELECT * FROM t2;
  3297. c d
  3298. 2 10
  3299. 2 20
  3300. 4 10
  3301. 5 10
  3302. 3 20
  3303. 2 40
  3304. SELECT * FROM t3;
  3305. e
  3306. 10
  3307. 30
  3308. 10
  3309. 20
  3310. SELECT a FROM t1 GROUP BY a
  3311. HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20);
  3312. a
  3313. 2
  3314. 4
  3315. SELECT a FROM t1 GROUP BY a
  3316. HAVING a IN (SELECT c FROM t2 WHERE MAX(b)<d);
  3317. a
  3318. 2
  3319. SELECT a FROM t1 GROUP BY a
  3320. HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>d);
  3321. a
  3322. 2
  3323. 4
  3324. SELECT a FROM t1 GROUP BY a
  3325. HAVING a IN (SELECT c FROM t2
  3326. WHERE d >= SOME(SELECT e FROM t3 WHERE MAX(b)=e));
  3327. a
  3328. 2
  3329. 3
  3330. SELECT a FROM t1 GROUP BY a
  3331. HAVING a IN (SELECT c FROM t2
  3332. WHERE EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
  3333. a
  3334. 2
  3335. 3
  3336. SELECT a FROM t1 GROUP BY a
  3337. HAVING a IN (SELECT c FROM t2
  3338. WHERE d > SOME(SELECT e FROM t3 WHERE MAX(b)=e));
  3339. a
  3340. 2
  3341. SELECT a FROM t1 GROUP BY a
  3342. HAVING a IN (SELECT c FROM t2
  3343. WHERE EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e < d));
  3344. a
  3345. 2
  3346. SELECT a FROM t1 GROUP BY a
  3347. HAVING a IN (SELECT c FROM t2
  3348. WHERE MIN(b) < d AND
  3349. EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
  3350. a
  3351. 2
  3352. SELECT a, SUM(a) FROM t1 GROUP BY a;
  3353. a SUM(a)
  3354. 1 2
  3355. 2 6
  3356. 3 3
  3357. 4 4
  3358. SELECT a FROM t1
  3359. WHERE EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c) GROUP BY a;
  3360. a
  3361. 3
  3362. 4
  3363. SELECT a FROM t1 GROUP BY a
  3364. HAVING EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c);
  3365. a
  3366. 1
  3367. 3
  3368. 4
  3369. SELECT a FROM t1
  3370. WHERE a < 3 AND
  3371. EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c) GROUP BY a;
  3372. a
  3373. 1
  3374. 2
  3375. SELECT a FROM t1
  3376. WHERE a < 3 AND
  3377. EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c);
  3378. a
  3379. 1
  3380. 2
  3381. 1
  3382. 2
  3383. 2
  3384. SELECT t1.a FROM t1 GROUP BY t1.a
  3385. HAVING t1.a < ALL(SELECT t2.c FROM t2 GROUP BY t2.c
  3386. HAVING EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
  3387. HAVING SUM(t1.a+t2.c) < t3.e/4));
  3388. a
  3389. 1
  3390. 2
  3391. SELECT t1.a FROM t1 GROUP BY t1.a
  3392. HAVING t1.a > ALL(SELECT t2.c FROM t2
  3393. WHERE EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
  3394. HAVING SUM(t1.a+t2.c) < t3.e/4));
  3395. a
  3396. 4
  3397. SELECT t1.a FROM t1 GROUP BY t1.a
  3398. HAVING t1.a > ALL(SELECT t2.c FROM t2
  3399. WHERE EXISTS(SELECT t3.e FROM t3
  3400. WHERE SUM(t1.a+t2.c) < t3.e/4));
  3401. ERROR HY000: Invalid use of group function
  3402. SELECT t1.a from t1 GROUP BY t1.a HAVING AVG(SUM(t1.b)) > 20;
  3403. ERROR HY000: Invalid use of group function
  3404. SELECT t1.a FROM t1 GROUP BY t1.a
  3405. HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
  3406. HAVING AVG(t2.c+SUM(t1.b)) > 20);
  3407. a
  3408. 2
  3409. 3
  3410. 4
  3411. SELECT t1.a FROM t1 GROUP BY t1.a
  3412. HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
  3413. HAVING AVG(SUM(t1.b)) > 20);
  3414. a
  3415. 2
  3416. 4
  3417. SELECT t1.a, SUM(b) AS sum FROM t1 GROUP BY t1.a
  3418. HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
  3419. HAVING t2.c+sum > 20);
  3420. a sum
  3421. 2 60
  3422. 3 20
  3423. 4 40
  3424. DROP TABLE t1,t2,t3;
  3425. CREATE TABLE t1 (a varchar(5), b varchar(10));
  3426. INSERT INTO t1 VALUES
  3427. ('AAA', 5), ('BBB', 4), ('BBB', 1), ('CCC', 2),
  3428. ('CCC', 7), ('AAA', 2), ('AAA', 4), ('BBB', 3), ('AAA', 8);
  3429. SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
  3430. a b
  3431. BBB 4
  3432. CCC 7
  3433. AAA 8
  3434. EXPLAIN
  3435. SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
  3436. id select_type table type possible_keys key key_len ref rows Extra
  3437. 1 PRIMARY t1 ALL NULL NULL NULL NULL 9 Using where
  3438. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 21 test.t1.a,test.t1.b 1
  3439. 2 MATERIALIZED t1 ALL NULL NULL NULL NULL 9 Using temporary
  3440. ALTER TABLE t1 ADD INDEX(a);
  3441. SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
  3442. a b
  3443. BBB 4
  3444. CCC 7
  3445. AAA 8
  3446. EXPLAIN
  3447. SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
  3448. id select_type table type possible_keys key key_len ref rows Extra
  3449. 1 PRIMARY t1 ALL a NULL NULL NULL 9 Using where
  3450. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 21 test.t1.a,test.t1.b 1
  3451. 2 MATERIALIZED t1 ALL NULL NULL NULL NULL 9 Using temporary
  3452. DROP TABLE t1;
  3453. create table t1( f1 int,f2 int);
  3454. insert into t1 values (1,1),(2,2);
  3455. select tt.t from (select 'crash1' as t, f2 from t1) as tt left join t1 on tt.t = 'crash2' and tt.f2 = t1.f2 where tt.t = 'crash1';
  3456. t
  3457. crash1
  3458. crash1
  3459. drop table t1;
  3460. create table t1 (c int, key(c));
  3461. insert into t1 values (1142477582), (1142455969);
  3462. create table t2 (a int, b int);
  3463. insert into t2 values (2, 1), (1, 0);
  3464. delete from t1 where c <= 1140006215 and (select b from t2 where a = 2) = 1;
  3465. drop table t1, t2;
  3466. CREATE TABLE t1 (a INT);
  3467. CREATE VIEW v1 AS SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1);
  3468. ERROR 42S22: Unknown column 'no_such_column' in 'where clause'
  3469. CREATE VIEW v2 AS SELECT * FROM t1 WHERE no_such_column = (SELECT 1);
  3470. ERROR 42S22: Unknown column 'no_such_column' in 'where clause'
  3471. SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1);
  3472. ERROR 42S22: Unknown column 'no_such_column' in 'IN/ALL/ANY subquery'
  3473. DROP TABLE t1;
  3474. create table t1 (i int, j bigint);
  3475. insert into t1 values (1, 2), (2, 2), (3, 2);
  3476. select * from (select min(i) from t1 where j=(select * from (select min(j) from t1) t2)) t3;
  3477. min(i)
  3478. 1
  3479. drop table t1;
  3480. CREATE TABLE t1 (i BIGINT UNSIGNED);
  3481. INSERT INTO t1 VALUES (10000000000000000000);
  3482. INSERT INTO t1 VALUES (1);
  3483. CREATE TABLE t2 (i BIGINT UNSIGNED);
  3484. INSERT INTO t2 VALUES (10000000000000000000);
  3485. INSERT INTO t2 VALUES (1);
  3486. /* simple test */
  3487. SELECT t1.i FROM t1 JOIN t2 ON t1.i = t2.i;
  3488. i
  3489. 10000000000000000000
  3490. 1
  3491. /* subquery test */
  3492. SELECT t1.i FROM t1 WHERE t1.i = (SELECT MAX(i) FROM t2);
  3493. i
  3494. 10000000000000000000
  3495. /* subquery test with cast*/
  3496. SELECT t1.i FROM t1 WHERE t1.i = CAST((SELECT MAX(i) FROM t2) AS UNSIGNED);
  3497. i
  3498. 10000000000000000000
  3499. DROP TABLE t1;
  3500. DROP TABLE t2;
  3501. CREATE TABLE t1 (
  3502. id bigint(20) unsigned NOT NULL auto_increment,
  3503. name varchar(255) NOT NULL,
  3504. PRIMARY KEY (id)
  3505. );
  3506. INSERT INTO t1 VALUES
  3507. (1, 'Balazs'), (2, 'Joe'), (3, 'Frank');
  3508. CREATE TABLE t2 (
  3509. id bigint(20) unsigned NOT NULL auto_increment,
  3510. mid bigint(20) unsigned NOT NULL,
  3511. date date NOT NULL,
  3512. PRIMARY KEY (id)
  3513. );
  3514. INSERT INTO t2 VALUES
  3515. (1, 1, '2006-03-30'), (2, 2, '2006-04-06'), (3, 3, '2006-04-13'),
  3516. (4, 2, '2006-04-20'), (5, 1, '2006-05-01');
  3517. SELECT *,
  3518. (SELECT date FROM t2 WHERE mid = t1.id
  3519. ORDER BY date DESC LIMIT 0, 1) AS date_last,
  3520. (SELECT date FROM t2 WHERE mid = t1.id
  3521. ORDER BY date DESC LIMIT 3, 1) AS date_next_to_last
  3522. FROM t1;
  3523. id name date_last date_next_to_last
  3524. 1 Balazs 2006-05-01 NULL
  3525. 2 Joe 2006-04-20 NULL
  3526. 3 Frank 2006-04-13 NULL
  3527. SELECT *,
  3528. (SELECT COUNT(*) FROM t2 WHERE mid = t1.id
  3529. ORDER BY date DESC LIMIT 1, 1) AS date_count
  3530. FROM t1;
  3531. id name date_count
  3532. 1 Balazs NULL
  3533. 2 Joe NULL
  3534. 3 Frank NULL
  3535. SELECT *,
  3536. (SELECT date FROM t2 WHERE mid = t1.id
  3537. ORDER BY date DESC LIMIT 0, 1) AS date_last,
  3538. (SELECT date FROM t2 WHERE mid = t1.id
  3539. ORDER BY date DESC LIMIT 1, 1) AS date_next_to_last
  3540. FROM t1;
  3541. id name date_last date_next_to_last
  3542. 1 Balazs 2006-05-01 2006-03-30
  3543. 2 Joe 2006-04-20 2006-04-06
  3544. 3 Frank 2006-04-13 NULL
  3545. DROP TABLE t1,t2;
  3546. CREATE TABLE t1 (
  3547. i1 int(11) NOT NULL default '0',
  3548. i2 int(11) NOT NULL default '0',
  3549. t datetime NOT NULL default '0000-00-00 00:00:00',
  3550. PRIMARY KEY (i1,i2,t)
  3551. );
  3552. INSERT INTO t1 VALUES
  3553. (24,1,'2005-03-03 16:31:31'),(24,1,'2005-05-27 12:40:07'),
  3554. (24,1,'2005-05-27 12:40:08'),(24,1,'2005-05-27 12:40:10'),
  3555. (24,1,'2005-05-27 12:40:25'),(24,1,'2005-05-27 12:40:30'),
  3556. (24,2,'2005-03-03 13:43:05'),(24,2,'2005-03-03 16:23:31'),
  3557. (24,2,'2005-03-03 16:31:30'),(24,2,'2005-05-27 12:37:02'),
  3558. (24,2,'2005-05-27 12:40:06');
  3559. CREATE TABLE t2 (
  3560. i1 int(11) NOT NULL default '0',
  3561. i2 int(11) NOT NULL default '0',
  3562. t datetime default NULL,
  3563. PRIMARY KEY (i1)
  3564. );
  3565. INSERT INTO t2 VALUES (24,1,'2006-06-20 12:29:40');
  3566. EXPLAIN
  3567. SELECT * FROM t1,t2
  3568. WHERE t1.t = (SELECT t1.t FROM t1
  3569. WHERE t1.t < t2.t AND t1.i2=1 AND t2.i1=t1.i1
  3570. ORDER BY t1.t DESC LIMIT 1);
  3571. id select_type table type possible_keys key key_len ref rows Extra
  3572. 1 PRIMARY t2 ALL NULL NULL NULL NULL 1
  3573. 1 PRIMARY t1 index NULL PRIMARY 16 NULL 11 Using where; Using index; Using join buffer (flat, BNL join)
  3574. 2 DEPENDENT SUBQUERY t1 ref PRIMARY PRIMARY 8 test.t2.i1,const 1 Using where; Using index; Using filesort
  3575. SELECT * FROM t1,t2
  3576. WHERE t1.t = (SELECT t1.t FROM t1
  3577. WHERE t1.t < t2.t AND t1.i2=1 AND t2.i1=t1.i1
  3578. ORDER BY t1.t DESC LIMIT 1);
  3579. i1 i2 t i1 i2 t
  3580. 24 1 2005-05-27 12:40:30 24 1 2006-06-20 12:29:40
  3581. DROP TABLE t1, t2;
  3582. CREATE TABLE t1 (i INT);
  3583. (SELECT i FROM t1) UNION (SELECT i FROM t1);
  3584. i
  3585. SELECT sql_no_cache * FROM t1 WHERE NOT EXISTS
  3586. (
  3587. (SELECT i FROM t1) UNION
  3588. (SELECT i FROM t1)
  3589. );
  3590. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNION
  3591. (SELECT i FROM t1)
  3592. )' at line 3
  3593. SELECT * FROM t1
  3594. WHERE NOT EXISTS (((SELECT i FROM t1) UNION (SELECT i FROM t1)));
  3595. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNION (SELECT i FROM t1)))' at line 2
  3596. explain select ((select t11.i from t1 t11) union (select t12.i from t1 t12))
  3597. from t1;
  3598. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'union (select t12.i from t1 t12))
  3599. from t1' at line 1
  3600. explain select * from t1 where not exists
  3601. ((select t11.i from t1 t11) union (select t12.i from t1 t12));
  3602. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'union (select t12.i from t1 t12))' at line 2
  3603. DROP TABLE t1;
  3604. CREATE TABLE t1 (a VARCHAR(250), b INT auto_increment, PRIMARY KEY (b));
  3605. insert into t1 (a) values (FLOOR(rand() * 100));
  3606. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3607. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3608. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3609. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3610. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3611. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3612. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3613. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3614. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3615. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3616. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3617. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3618. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3619. SELECT a,
  3620. (SELECT REPEAT(' ',250) FROM t1 i1
  3621. WHERE i1.b=t1.a ORDER BY RAND() LIMIT 1) AS a
  3622. FROM t1 ORDER BY a LIMIT 5;
  3623. a a
  3624. 0 NULL
  3625. 0 NULL
  3626. 0 NULL
  3627. 0 NULL
  3628. 0 NULL
  3629. DROP TABLE t1;
  3630. CREATE TABLE t1 (a INT, b INT);
  3631. CREATE TABLE t2 (a INT);
  3632. INSERT INTO t2 values (1);
  3633. INSERT INTO t1 VALUES (1,1),(1,2),(2,3),(3,4);
  3634. SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a;
  3635. (SELECT COUNT(DISTINCT t1.b) from t2)
  3636. 2
  3637. 1
  3638. 1
  3639. SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
  3640. FROM t1 GROUP BY t1.a;
  3641. (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
  3642. 2
  3643. 1
  3644. 1
  3645. SELECT COUNT(DISTINCT t1.b), (SELECT COUNT(DISTINCT t1.b)) FROM t1 GROUP BY t1.a;
  3646. COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b))
  3647. 2 2
  3648. 1 1
  3649. 1 1
  3650. SELECT COUNT(DISTINCT t1.b),
  3651. (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
  3652. FROM t1 GROUP BY t1.a;
  3653. COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
  3654. 2 2
  3655. 1 1
  3656. 1 1
  3657. SELECT (
  3658. SELECT (
  3659. SELECT COUNT(DISTINCT t1.b)
  3660. )
  3661. )
  3662. FROM t1 GROUP BY t1.a;
  3663. (
  3664. SELECT (
  3665. SELECT COUNT(DISTINCT t1.b)
  3666. )
  3667. )
  3668. 2
  3669. 1
  3670. 1
  3671. SELECT (
  3672. SELECT (
  3673. SELECT (
  3674. SELECT COUNT(DISTINCT t1.b)
  3675. )
  3676. )
  3677. FROM t1 GROUP BY t1.a LIMIT 1)
  3678. FROM t1 t2
  3679. GROUP BY t2.a;
  3680. (
  3681. SELECT (
  3682. SELECT (
  3683. SELECT COUNT(DISTINCT t1.b)
  3684. )
  3685. )
  3686. FROM t1 GROUP BY t1.a LIMIT 1)
  3687. 2
  3688. 2
  3689. 2
  3690. DROP TABLE t1,t2;
  3691. CREATE TABLE t1 (a int, b int auto_increment, PRIMARY KEY (b));
  3692. CREATE TABLE t2 (x int auto_increment, y int, z int,
  3693. PRIMARY KEY (x), FOREIGN KEY (y) REFERENCES t1 (b));
  3694. SET SESSION sort_buffer_size = 32 * 1024;
  3695. SELECT SQL_NO_CACHE COUNT(*)
  3696. FROM (SELECT a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c
  3697. FROM t1) t;
  3698. COUNT(*)
  3699. 3000
  3700. SET SESSION sort_buffer_size = 8 * 1024 * 1024;
  3701. SELECT SQL_NO_CACHE COUNT(*)
  3702. FROM (SELECT a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c
  3703. FROM t1) t;
  3704. COUNT(*)
  3705. 3000
  3706. DROP TABLE t2,t1;
  3707. CREATE TABLE t1 (id char(4) PRIMARY KEY, c int);
  3708. CREATE TABLE t2 (c int);
  3709. INSERT INTO t1 VALUES ('aa', 1);
  3710. INSERT INTO t2 VALUES (1);
  3711. SELECT * FROM t1
  3712. WHERE EXISTS (SELECT c FROM t2 WHERE c=1
  3713. UNION
  3714. SELECT c from t2 WHERE c=t1.c);
  3715. id c
  3716. aa 1
  3717. INSERT INTO t1 VALUES ('bb', 2), ('cc', 3), ('dd',1);
  3718. SELECT * FROM t1
  3719. WHERE EXISTS (SELECT c FROM t2 WHERE c=1
  3720. UNION
  3721. SELECT c from t2 WHERE c=t1.c);
  3722. id c
  3723. aa 1
  3724. bb 2
  3725. cc 3
  3726. dd 1
  3727. INSERT INTO t2 VALUES (2);
  3728. CREATE TABLE t3 (c int);
  3729. INSERT INTO t3 VALUES (1);
  3730. SELECT * FROM t1
  3731. WHERE EXISTS (SELECT t2.c FROM t2 JOIN t3 ON t2.c=t3.c WHERE t2.c=1
  3732. UNION
  3733. SELECT c from t2 WHERE c=t1.c);
  3734. id c
  3735. aa 1
  3736. bb 2
  3737. cc 3
  3738. dd 1
  3739. DROP TABLE t1,t2,t3;
  3740. CREATE TABLE t1(f1 int);
  3741. CREATE TABLE t2(f2 int, f21 int, f3 timestamp);
  3742. INSERT INTO t1 VALUES (1),(1),(2),(2);
  3743. INSERT INTO t2 VALUES (1,1,"2004-02-29 11:11:11"), (2,2,"2004-02-29 11:11:11");
  3744. SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1;
  3745. sq
  3746. 2
  3747. 4
  3748. SELECT (SELECT SUM(1) FROM t2 ttt GROUP BY t2.f3 LIMIT 1) AS tt FROM t2;
  3749. tt
  3750. 2
  3751. 2
  3752. PREPARE stmt1 FROM 'SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1';
  3753. EXECUTE stmt1;
  3754. sq
  3755. 2
  3756. 4
  3757. EXECUTE stmt1;
  3758. sq
  3759. 2
  3760. 4
  3761. DEALLOCATE PREPARE stmt1;
  3762. SELECT f2, AVG(f21),
  3763. (SELECT t.f3 FROM t2 AS t WHERE t2.f2=t.f2 AND t.f3=MAX(t2.f3)) AS test
  3764. FROM t2 GROUP BY f2;
  3765. f2 AVG(f21) test
  3766. 1 1.0000 2004-02-29 11:11:11
  3767. 2 2.0000 2004-02-29 11:11:11
  3768. DROP TABLE t1,t2;
  3769. CREATE TABLE t1 (a int, b INT, c CHAR(10) NOT NULL);
  3770. INSERT INTO t1 VALUES
  3771. (1,1,'a'), (1,2,'b'), (1,3,'c'), (1,4,'d'), (1,5,'e'),
  3772. (2,1,'f'), (2,2,'g'), (2,3,'h'), (3,4,'i'), (3,3,'j'),
  3773. (3,2,'k'), (3,1,'l'), (1,9,'m');
  3774. SELECT a, MAX(b),
  3775. (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b)) AS test
  3776. FROM t1 GROUP BY a;
  3777. a MAX(b) test
  3778. 1 9 m
  3779. 2 3 h
  3780. 3 4 i
  3781. DROP TABLE t1;
  3782. DROP TABLE IF EXISTS t1;
  3783. DROP TABLE IF EXISTS t2;
  3784. DROP TABLE IF EXISTS t1xt2;
  3785. CREATE TABLE t1 (
  3786. id_1 int(5) NOT NULL,
  3787. t varchar(4) DEFAULT NULL
  3788. );
  3789. CREATE TABLE t2 (
  3790. id_2 int(5) NOT NULL,
  3791. t varchar(4) DEFAULT NULL
  3792. );
  3793. CREATE TABLE t1xt2 (
  3794. id_1 int(5) NOT NULL,
  3795. id_2 int(5) NOT NULL
  3796. );
  3797. INSERT INTO t1 VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd');
  3798. INSERT INTO t2 VALUES (2, 'bb'), (3, 'cc'), (4, 'dd'), (12, 'aa');
  3799. INSERT INTO t1xt2 VALUES (2, 2), (3, 3), (4, 4);
  3800. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3801. (12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
  3802. id_1
  3803. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3804. (12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
  3805. id_1
  3806. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3807. (12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
  3808. id_1
  3809. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3810. (12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
  3811. id_1
  3812. 1
  3813. 2
  3814. 3
  3815. 4
  3816. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3817. (12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1)));
  3818. id_1
  3819. 1
  3820. 2
  3821. 3
  3822. 4
  3823. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3824. (12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1))));
  3825. id_1
  3826. 1
  3827. 2
  3828. 3
  3829. 4
  3830. insert INTO t1xt2 VALUES (1, 12);
  3831. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3832. (12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
  3833. id_1
  3834. 1
  3835. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3836. (12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
  3837. id_1
  3838. 1
  3839. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3840. (12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
  3841. id_1
  3842. 1
  3843. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3844. (12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
  3845. id_1
  3846. 2
  3847. 3
  3848. 4
  3849. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3850. (12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
  3851. id_1
  3852. 2
  3853. 3
  3854. 4
  3855. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3856. (12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
  3857. id_1
  3858. 2
  3859. 3
  3860. 4
  3861. insert INTO t1xt2 VALUES (2, 12);
  3862. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3863. (12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
  3864. id_1
  3865. 1
  3866. 2
  3867. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3868. (12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
  3869. id_1
  3870. 1
  3871. 2
  3872. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3873. (12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
  3874. id_1
  3875. 1
  3876. 2
  3877. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3878. (12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
  3879. id_1
  3880. 3
  3881. 4
  3882. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3883. (12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
  3884. id_1
  3885. 3
  3886. 4
  3887. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3888. (12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
  3889. id_1
  3890. 3
  3891. 4
  3892. DROP TABLE t1;
  3893. DROP TABLE t2;
  3894. DROP TABLE t1xt2;
  3895. CREATE TABLE t1 (a int);
  3896. INSERT INTO t1 VALUES (3), (1), (2);
  3897. SELECT 'this is ' 'a test.' AS col1, a AS col2 FROM t1;
  3898. col1 col2
  3899. this is a test. 3
  3900. this is a test. 1
  3901. this is a test. 2
  3902. SELECT * FROM (SELECT 'this is ' 'a test.' AS col1, a AS t2 FROM t1) t;
  3903. col1 t2
  3904. this is a test. 3
  3905. this is a test. 1
  3906. this is a test. 2
  3907. DROP table t1;
  3908. CREATE TABLE t1 (a int, b int);
  3909. CREATE TABLE t2 (m int, n int);
  3910. INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
  3911. INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
  3912. SELECT COUNT(*), a,
  3913. (SELECT m FROM t2 WHERE m = count(*) LIMIT 1)
  3914. FROM t1 GROUP BY a;
  3915. COUNT(*) a (SELECT m FROM t2 WHERE m = count(*) LIMIT 1)
  3916. 2 2 2
  3917. 3 3 3
  3918. 1 4 1
  3919. SELECT COUNT(*), a,
  3920. (SELECT MIN(m) FROM t2 WHERE m = count(*))
  3921. FROM t1 GROUP BY a;
  3922. COUNT(*) a (SELECT MIN(m) FROM t2 WHERE m = count(*))
  3923. 2 2 2
  3924. 3 3 3
  3925. 1 4 1
  3926. SELECT COUNT(*), a
  3927. FROM t1 GROUP BY a
  3928. HAVING (SELECT MIN(m) FROM t2 WHERE m = count(*)) > 1;
  3929. COUNT(*) a
  3930. 2 2
  3931. 3 3
  3932. DROP TABLE t1,t2;
  3933. CREATE TABLE t1 (a int, b int);
  3934. CREATE TABLE t2 (m int, n int);
  3935. INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
  3936. INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
  3937. SELECT COUNT(*) c, a,
  3938. (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a)
  3939. FROM t1 GROUP BY a;
  3940. c a (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a)
  3941. 2 2 2
  3942. 3 3 3
  3943. 1 4 1,1
  3944. SELECT COUNT(*) c, a,
  3945. (SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a)
  3946. FROM t1 GROUP BY a;
  3947. c a (SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a)
  3948. 2 2 3
  3949. 3 3 4
  3950. 1 4 2,2
  3951. DROP table t1,t2;
  3952. CREATE TABLE t1 (a int, b INT, d INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b));
  3953. INSERT INTO t1 VALUES (1,1,0,'a'), (1,2,0,'b'), (1,3,0,'c'), (1,4,0,'d'),
  3954. (1,5,0,'e'), (2,1,0,'f'), (2,2,0,'g'), (2,3,0,'h'), (3,4,0,'i'), (3,3,0,'j'),
  3955. (3,2,0,'k'), (3,1,0,'l'), (1,9,0,'m'), (1,0,10,'n'), (2,0,5,'o'), (3,0,7,'p');
  3956. SELECT a, MAX(b),
  3957. (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b + 0)) as test
  3958. FROM t1 GROUP BY a;
  3959. a MAX(b) test
  3960. 1 9 m
  3961. 2 3 h
  3962. 3 4 i
  3963. SELECT a x, MAX(b),
  3964. (SELECT t.c FROM t1 AS t WHERE x=t.a AND t.b=MAX(t1.b + 0)) as test
  3965. FROM t1 GROUP BY a;
  3966. x MAX(b) test
  3967. 1 9 m
  3968. 2 3 h
  3969. 3 4 i
  3970. SELECT a, AVG(b),
  3971. (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=AVG(t1.b)) AS test
  3972. FROM t1 WHERE t1.d=0 GROUP BY a;
  3973. a AVG(b) test
  3974. 1 4.0000 d
  3975. 2 2.0000 g
  3976. 3 2.5000 NULL
  3977. SELECT tt.a,
  3978. (SELECT (SELECT c FROM t1 as t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
  3979. LIMIT 1) FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test
  3980. FROM t1 as tt;
  3981. a test
  3982. 1 n
  3983. 1 n
  3984. 1 n
  3985. 1 n
  3986. 1 n
  3987. 1 n
  3988. 1 n
  3989. 2 o
  3990. 2 o
  3991. 2 o
  3992. 2 o
  3993. 3 p
  3994. 3 p
  3995. 3 p
  3996. 3 p
  3997. 3 p
  3998. SELECT tt.a,
  3999. (SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
  4000. LIMIT 1)
  4001. FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test
  4002. FROM t1 as tt GROUP BY tt.a;
  4003. a test
  4004. 1 n
  4005. 2 o
  4006. 3 p
  4007. SELECT tt.a, MAX(
  4008. (SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
  4009. LIMIT 1)
  4010. FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1)) as test
  4011. FROM t1 as tt GROUP BY tt.a;
  4012. a test
  4013. 1 n
  4014. 2 o
  4015. 3 p
  4016. DROP TABLE t1;
  4017. CREATE TABLE t1 (a int, b int);
  4018. INSERT INTO t1 VALUES (2,22),(1,11),(2,22);
  4019. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
  4020. a
  4021. 1
  4022. 2
  4023. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
  4024. a
  4025. SELECT a FROM t1 t0
  4026. WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
  4027. a
  4028. 1
  4029. 2
  4030. SET @@sql_mode='ansi';
  4031. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
  4032. ERROR HY000: Invalid use of group function
  4033. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
  4034. ERROR HY000: Invalid use of group function
  4035. SELECT a FROM t1 t0
  4036. WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
  4037. ERROR HY000: Invalid use of group function
  4038. SET @@sql_mode=default;
  4039. DROP TABLE t1;
  4040. CREATE TABLE t1 (a INT);
  4041. INSERT INTO t1 values (1),(1),(1),(1);
  4042. CREATE TABLE t2 (x INT);
  4043. INSERT INTO t1 values (1000),(1001),(1002);
  4044. SELECT SUM( (SELECT COUNT(a) FROM t2) ) FROM t1;
  4045. ERROR HY000: Invalid use of group function
  4046. SELECT SUM( (SELECT SUM(COUNT(a)) FROM t2) ) FROM t1;
  4047. ERROR HY000: Invalid use of group function
  4048. SELECT COUNT(1) FROM DUAL;
  4049. COUNT(1)
  4050. 1
  4051. SELECT SUM( (SELECT AVG( (SELECT t1.a FROM t2) ) FROM DUAL) ) FROM t1;
  4052. ERROR HY000: Invalid use of group function
  4053. SELECT
  4054. SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING t1.a < 12) ) FROM t2) )
  4055. FROM t1;
  4056. ERROR HY000: Invalid use of group function
  4057. SELECT t1.a as XXA,
  4058. SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING XXA < 12) ) FROM t2) )
  4059. FROM t1;
  4060. ERROR HY000: Invalid use of group function
  4061. DROP TABLE t1,t2;
  4062. CREATE TABLE t1 (a int, b int, KEY (a));
  4063. INSERT INTO t1 VALUES (1,1),(2,1);
  4064. EXPLAIN SELECT 1 FROM t1 WHERE a = (SELECT COUNT(*) FROM t1 GROUP BY b);
  4065. id select_type table type possible_keys key key_len ref rows Extra
  4066. 1 PRIMARY t1 ref a a 5 const 0 Using where; Using index
  4067. 2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using temporary; Using filesort
  4068. DROP TABLE t1;
  4069. CREATE TABLE t1 (id int NOT NULL, st CHAR(2), INDEX idx(id));
  4070. INSERT INTO t1 VALUES
  4071. (3,'FL'), (2,'GA'), (4,'FL'), (1,'GA'), (5,'NY'), (7,'FL'), (6,'NY');
  4072. CREATE TABLE t2 (id int NOT NULL, INDEX idx(id));
  4073. INSERT INTO t2 VALUES (7), (5), (1), (3);
  4074. SELECT id, st FROM t1
  4075. WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id);
  4076. id st
  4077. 3 FL
  4078. 1 GA
  4079. 7 FL
  4080. SELECT id, st FROM t1
  4081. WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id)
  4082. GROUP BY id;
  4083. id st
  4084. 1 GA
  4085. 3 FL
  4086. 7 FL
  4087. SELECT id, st FROM t1
  4088. WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id);
  4089. id st
  4090. 2 GA
  4091. 4 FL
  4092. SELECT id, st FROM t1
  4093. WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id)
  4094. GROUP BY id;
  4095. id st
  4096. 2 GA
  4097. 4 FL
  4098. DROP TABLE t1,t2;
  4099. CREATE TABLE t1 (a int);
  4100. INSERT INTO t1 VALUES (1), (2);
  4101. EXPLAIN EXTENDED
  4102. SELECT * FROM (SELECT count(*) FROM t1 GROUP BY a) as res;
  4103. id select_type table type possible_keys key key_len ref rows filtered Extra
  4104. 1 PRIMARY <derived2> ALL NULL NULL NULL NULL 2 100.00
  4105. 2 DERIVED t1 ALL NULL NULL NULL NULL 2 100.00 Using temporary; Using filesort
  4106. Warnings:
  4107. Note 1003 select `res`.`count(*)` AS `count(*)` from (select count(0) AS `count(*)` from `test`.`t1` group by `test`.`t1`.`a`) `res`
  4108. DROP TABLE t1;
  4109. CREATE TABLE t1 (
  4110. a varchar(255) default NULL,
  4111. b timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  4112. INDEX idx(a,b)
  4113. );
  4114. CREATE TABLE t2 (
  4115. a varchar(255) default NULL
  4116. );
  4117. INSERT INTO t1 VALUES ('abcdefghijk','2007-05-07 06:00:24');
  4118. INSERT INTO t1 SELECT * FROM t1;
  4119. INSERT INTO t1 SELECT * FROM t1;
  4120. INSERT INTO t1 SELECT * FROM t1;
  4121. INSERT INTO t1 SELECT * FROM t1;
  4122. INSERT INTO t1 SELECT * FROM t1;
  4123. INSERT INTO t1 SELECT * FROM t1;
  4124. INSERT INTO t1 SELECT * FROM t1;
  4125. INSERT INTO t1 SELECT * FROM t1;
  4126. INSERT INTO `t1` VALUES ('asdf','2007-02-08 01:11:26');
  4127. INSERT INTO `t2` VALUES ('abcdefghijk');
  4128. INSERT INTO `t2` VALUES ('asdf');
  4129. SET session sort_buffer_size=8192;
  4130. SELECT (SELECT 1 FROM t1 WHERE t1.a=t2.a ORDER BY t1.b LIMIT 1) AS d1 FROM t2;
  4131. d1
  4132. 1
  4133. 1
  4134. DROP TABLE t1,t2;
  4135. CREATE TABLE t1 (a INTEGER, b INTEGER);
  4136. CREATE TABLE t2 (x INTEGER);
  4137. INSERT INTO t1 VALUES (1,11), (2,22), (2,22);
  4138. INSERT INTO t2 VALUES (1), (2);
  4139. SELECT a, COUNT(b), (SELECT COUNT(b) FROM t2) FROM t1 GROUP BY a;
  4140. ERROR 21000: Subquery returns more than 1 row
  4141. SELECT a, COUNT(b), (SELECT COUNT(b)+0 FROM t2) FROM t1 GROUP BY a;
  4142. ERROR 21000: Subquery returns more than 1 row
  4143. SELECT (SELECT SUM(t1.a)/AVG(t2.x) FROM t2) FROM t1;
  4144. (SELECT SUM(t1.a)/AVG(t2.x) FROM t2)
  4145. 3.3333
  4146. DROP TABLE t1,t2;
  4147. CREATE TABLE t1 (a INT, b INT);
  4148. INSERT INTO t1 VALUES (1, 2), (1,3), (1,4), (2,1), (2,2);
  4149. SELECT a1.a, COUNT(*) FROM t1 a1 WHERE a1.a = 1
  4150. AND EXISTS( SELECT a2.a FROM t1 a2 WHERE a2.a = a1.a)
  4151. GROUP BY a1.a;
  4152. a COUNT(*)
  4153. 1 3
  4154. DROP TABLE t1;
  4155. CREATE TABLE t1 (a INT);
  4156. CREATE TABLE t2 (a INT);
  4157. INSERT INTO t1 VALUES (1),(2);
  4158. INSERT INTO t2 VALUES (1),(2);
  4159. SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=0) FROM t1;
  4160. (SELECT SUM(t1.a) FROM t2 WHERE a=0)
  4161. NULL
  4162. SELECT (SELECT SUM(t1.a) FROM t2 WHERE a!=0) FROM t1;
  4163. ERROR 21000: Subquery returns more than 1 row
  4164. SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=1) FROM t1;
  4165. (SELECT SUM(t1.a) FROM t2 WHERE a=1)
  4166. 3
  4167. DROP TABLE t1,t2;
  4168. CREATE TABLE t1 (a1 INT, a2 INT);
  4169. CREATE TABLE t2 (b1 INT, b2 INT);
  4170. INSERT INTO t1 VALUES (100, 200);
  4171. INSERT INTO t1 VALUES (101, 201);
  4172. INSERT INTO t2 VALUES (101, 201);
  4173. INSERT INTO t2 VALUES (103, 203);
  4174. SELECT ((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL FROM t1;
  4175. ((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL
  4176. 0
  4177. 0
  4178. DROP TABLE t1, t2;
  4179. CREATE TABLE t1 (s1 BINARY(5), s2 VARBINARY(5));
  4180. INSERT INTO t1 VALUES (0x41,0x41), (0x42,0x42), (0x43,0x43);
  4181. SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
  4182. s1 s2
  4183. SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
  4184. s1 s2
  4185. CREATE INDEX I1 ON t1 (s1);
  4186. CREATE INDEX I2 ON t1 (s2);
  4187. SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
  4188. s1 s2
  4189. SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
  4190. s1 s2
  4191. TRUNCATE t1;
  4192. INSERT INTO t1 VALUES (0x41,0x41);
  4193. SELECT * FROM t1 WHERE s1 = (SELECT s2 FROM t1);
  4194. s1 s2
  4195. DROP TABLE t1;
  4196. CREATE TABLE t1 (a1 VARBINARY(2) NOT NULL DEFAULT '0', PRIMARY KEY (a1));
  4197. CREATE TABLE t2 (a2 BINARY(2) default '0', INDEX (a2));
  4198. CREATE TABLE t3 (a3 BINARY(2) default '0');
  4199. INSERT INTO t1 VALUES (1),(2),(3),(4);
  4200. INSERT INTO t2 VALUES (1),(2),(3);
  4201. INSERT INTO t3 VALUES (1),(2),(3);
  4202. SELECT LEFT(t2.a2, 1) FROM t2,t3 WHERE t3.a3=t2.a2;
  4203. LEFT(t2.a2, 1)
  4204. 1
  4205. 2
  4206. 3
  4207. SELECT t1.a1, t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2) FROM t1;
  4208. a1 t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2)
  4209. 1 0
  4210. 2 0
  4211. 3 0
  4212. 4 0
  4213. DROP TABLE t1,t2,t3;
  4214. CREATE TABLE t1 (a1 BINARY(3) PRIMARY KEY, b1 VARBINARY(3));
  4215. CREATE TABLE t2 (a2 VARBINARY(3) PRIMARY KEY);
  4216. CREATE TABLE t3 (a3 VARBINARY(3) PRIMARY KEY);
  4217. INSERT INTO t1 VALUES (1,10), (2,20), (3,30), (4,40);
  4218. INSERT INTO t2 VALUES (2), (3), (4), (5);
  4219. INSERT INTO t3 VALUES (10), (20), (30);
  4220. SELECT LEFT(t1.a1,1) FROM t1,t3 WHERE t1.b1=t3.a3;
  4221. LEFT(t1.a1,1)
  4222. 1
  4223. 2
  4224. 3
  4225. SELECT a2 FROM t2 WHERE t2.a2 IN (SELECT t1.a1 FROM t1,t3 WHERE t1.b1=t3.a3);
  4226. a2
  4227. DROP TABLE t1, t2, t3;
  4228. SET @save_optimizer_switch=@@optimizer_switch;
  4229. SET optimizer_switch='semijoin_with_cache=off';
  4230. SET optimizer_switch='materialization=off';
  4231. CREATE TABLE t1 (a CHAR(1), b VARCHAR(10));
  4232. INSERT INTO t1 VALUES ('a', 'aa');
  4233. INSERT INTO t1 VALUES ('a', 'aaa');
  4234. SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
  4235. a b
  4236. CREATE INDEX I1 ON t1 (a);
  4237. CREATE INDEX I2 ON t1 (b);
  4238. EXPLAIN SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
  4239. id select_type table type possible_keys key key_len ref rows Extra
  4240. 1 PRIMARY t1 ALL I2 NULL NULL NULL 2 Using where
  4241. 1 PRIMARY t1 ref I1 I1 2 test.t1.b 1 Using where; Using index; FirstMatch(t1)
  4242. SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
  4243. a b
  4244. CREATE TABLE t2 (a VARCHAR(1), b VARCHAR(10));
  4245. INSERT INTO t2 SELECT * FROM t1;
  4246. CREATE INDEX I1 ON t2 (a);
  4247. CREATE INDEX I2 ON t2 (b);
  4248. EXPLAIN SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
  4249. id select_type table type possible_keys key key_len ref rows Extra
  4250. 1 PRIMARY t2 ALL I2 NULL NULL NULL 2 Using where
  4251. 1 PRIMARY t2 ref I1 I1 4 test.t2.b 1 Using where; Using index; FirstMatch(t2)
  4252. SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
  4253. a b
  4254. EXPLAIN
  4255. SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
  4256. id select_type table type possible_keys key key_len ref rows Extra
  4257. 1 PRIMARY t1 ALL I2 NULL NULL NULL 2 Using where
  4258. 1 PRIMARY t1 ref I1 I1 2 test.t1.b 1 Using where; Using index; FirstMatch(t1)
  4259. SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
  4260. a b
  4261. DROP TABLE t1,t2;
  4262. SET optimizer_switch= @save_optimizer_switch;
  4263. CREATE TABLE t1(a INT, b INT);
  4264. INSERT INTO t1 VALUES (1,1), (1,2), (2,3), (2,4);
  4265. EXPLAIN
  4266. SELECT a AS out_a, MIN(b) FROM t1
  4267. WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
  4268. GROUP BY a;
  4269. ERROR 42S22: Unknown column 'out_a' in 'where clause'
  4270. SELECT a AS out_a, MIN(b) FROM t1
  4271. WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
  4272. GROUP BY a;
  4273. ERROR 42S22: Unknown column 'out_a' in 'where clause'
  4274. EXPLAIN
  4275. SELECT a AS out_a, MIN(b) FROM t1 t1_outer
  4276. WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
  4277. GROUP BY a;
  4278. id select_type table type possible_keys key key_len ref rows Extra
  4279. 1 PRIMARY t1_outer ALL NULL NULL NULL NULL 4 Using where; Using temporary; Using filesort
  4280. 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 4 Using where
  4281. SELECT a AS out_a, MIN(b) FROM t1 t1_outer
  4282. WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
  4283. GROUP BY a;
  4284. out_a MIN(b)
  4285. 1 2
  4286. 2 4
  4287. DROP TABLE t1;
  4288. CREATE TABLE t1 (a INT);
  4289. CREATE TABLE t2 (a INT);
  4290. INSERT INTO t1 VALUES (1),(2);
  4291. INSERT INTO t2 VALUES (1),(2);
  4292. SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
  4293. 2
  4294. 2
  4295. 2
  4296. EXPLAIN EXTENDED
  4297. SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
  4298. id select_type table type possible_keys key key_len ref rows filtered Extra
  4299. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 Using where
  4300. 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
  4301. Warnings:
  4302. Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
  4303. Note 1003 select 2 AS `2` from `test`.`t1` where <expr_cache><`test`.`t1`.`a`>(exists(select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)))
  4304. EXPLAIN EXTENDED
  4305. SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION
  4306. (SELECT 1 FROM t2 WHERE t1.a = t2.a));
  4307. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNION
  4308. (SELECT 1 FROM t2 WHERE t1.a = t2.a))' at line 2
  4309. DROP TABLE t1,t2;
  4310. create table t1(f11 int, f12 int);
  4311. create table t2(f21 int unsigned not null, f22 int, f23 varchar(10));
  4312. insert into t1 values(1,1),(2,2), (3, 3);
  4313. set session sort_buffer_size= 33*1024;
  4314. select count(*) from t1 where f12 =
  4315. (select f22 from t2 where f22 = f12 order by f21 desc, f22, f23 limit 1);
  4316. count(*)
  4317. 3
  4318. drop table t1,t2;
  4319. End of 5.0 tests.
  4320. CREATE TABLE t1 (a int, b int);
  4321. INSERT INTO t1 VALUES (2,22),(1,11),(2,22);
  4322. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
  4323. a
  4324. 1
  4325. 2
  4326. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
  4327. a
  4328. SELECT a FROM t1 t0
  4329. WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
  4330. a
  4331. 1
  4332. 2
  4333. SET @@sql_mode='ansi';
  4334. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
  4335. ERROR HY000: Invalid use of group function
  4336. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
  4337. ERROR HY000: Invalid use of group function
  4338. SELECT a FROM t1 t0
  4339. WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
  4340. ERROR HY000: Invalid use of group function
  4341. SET @@sql_mode=default;
  4342. DROP TABLE t1;
  4343. CREATE TABLE t1 (s1 char(1));
  4344. INSERT INTO t1 VALUES ('a');
  4345. SELECT * FROM t1 WHERE _utf8'a' = ANY (SELECT s1 FROM t1);
  4346. s1
  4347. a
  4348. DROP TABLE t1;