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.

6018 lines
201 KiB

15 years ago
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
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
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
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
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
15 years ago
15 years ago
Fix MySQL BUG#12329653 In MariaDB, when running in ONLY_FULL_GROUP_BY mode, the server produced in incorrect error message that there is an aggregate function without GROUP BY, for artificially created MIN/MAX functions during subquery MIN/MAX optimization. The fix introduces a way to distinguish between artifially created MIN/MAX functions as a result of a rewrite, and normal ones present in the query. The test for ONLY_FULL_GROUP_BY violation now tests in addition if a MIN/MAX function was part of a MIN/MAX subquery rewrite. In order to be able to distinguish these MIN/MAX functions, the patch introduces an additional flag in Item_in_subselect::in_strategy - SUBS_STRATEGY_CHOSEN. This flag is set when the optimizer makes its final choice of a subuqery strategy. In order to make the choice consistent, access to Item_in_subselect::in_strategy is provided via new class methods. ****** Fix MySQL BUG#12329653 In MariaDB, when running in ONLY_FULL_GROUP_BY mode, the server produced in incorrect error message that there is an aggregate function without GROUP BY, for artificially created MIN/MAX functions during subquery MIN/MAX optimization. The fix introduces a way to distinguish between artifially created MIN/MAX functions as a result of a rewrite, and normal ones present in the query. The test for ONLY_FULL_GROUP_BY violation now tests in addition if a MIN/MAX function was part of a MIN/MAX subquery rewrite. In order to be able to distinguish these MIN/MAX functions, the patch introduces an additional flag in Item_in_subselect::in_strategy - SUBS_STRATEGY_CHOSEN. This flag is set when the optimizer makes its final choice of a subuqery strategy. In order to make the choice consistent, access to Item_in_subselect::in_strategy is provided via new class methods.
14 years ago
Fix bug lp:833777 Analysis: The optimizer distinguishes two kinds of 'constant' conditions: expensive ones, and non-expensive ones. The non-expensive conditions are evaluated inside make_join_select(), and if false, already the optimizer detects empty query results. In order to avoid arbitrarily expensive optimization, the evaluation of expensive constant conditions is delayed until execution. These conditions are attached to JOIN::exec_const_cond and evaluated in the beginning of JOIN::exec. The relevant execution logic is: JOIN::exec() { if (! join->exec_const_cond->val_int()) { produce an empty result; stop execution } continue execution execute the original WHERE clause (that contains exec_const_cond) ... } As a result, when an expensive constant condition is TRUE, it is evaluated twice - once through JOIN::exec_const_cond, and once through JOIN::cond. When the expensive constant condition is a subquery, predicate, the subquery is evaluated twice. If we have many levels of subqueries, this logic results in a chain of recursive subquery executions that walk a perfect binary tree. The result is that for subquries with depth N, JOIN::exec is executed O(2^N) times. Solution: Notice that the second execution of the constant conditions happens inside do_select(), in the branch: if (join->table_count == join->const_tables) { ... } In this case exec_const_cond is equivalent to the whole WHERE clause, therefore the WHERE clause has already been checked in the beginnig of JOIN::exec, and has been found to be true. The bug is addressed by not evaluating the WHERE clause if there was exec_const_conds, and it was TRUE.
14 years ago
Fix bug lp:833777 Analysis: The optimizer distinguishes two kinds of 'constant' conditions: expensive ones, and non-expensive ones. The non-expensive conditions are evaluated inside make_join_select(), and if false, already the optimizer detects empty query results. In order to avoid arbitrarily expensive optimization, the evaluation of expensive constant conditions is delayed until execution. These conditions are attached to JOIN::exec_const_cond and evaluated in the beginning of JOIN::exec. The relevant execution logic is: JOIN::exec() { if (! join->exec_const_cond->val_int()) { produce an empty result; stop execution } continue execution execute the original WHERE clause (that contains exec_const_cond) ... } As a result, when an expensive constant condition is TRUE, it is evaluated twice - once through JOIN::exec_const_cond, and once through JOIN::cond. When the expensive constant condition is a subquery, predicate, the subquery is evaluated twice. If we have many levels of subqueries, this logic results in a chain of recursive subquery executions that walk a perfect binary tree. The result is that for subquries with depth N, JOIN::exec is executed O(2^N) times. Solution: Notice that the second execution of the constant conditions happens inside do_select(), in the branch: if (join->table_count == join->const_tables) { ... } In this case exec_const_cond is equivalent to the whole WHERE clause, therefore the WHERE clause has already been checked in the beginnig of JOIN::exec, and has been found to be true. The bug is addressed by not evaluating the WHERE clause if there was exec_const_conds, and it was TRUE.
14 years ago
Fix bug lp:833777 Analysis: The optimizer distinguishes two kinds of 'constant' conditions: expensive ones, and non-expensive ones. The non-expensive conditions are evaluated inside make_join_select(), and if false, already the optimizer detects empty query results. In order to avoid arbitrarily expensive optimization, the evaluation of expensive constant conditions is delayed until execution. These conditions are attached to JOIN::exec_const_cond and evaluated in the beginning of JOIN::exec. The relevant execution logic is: JOIN::exec() { if (! join->exec_const_cond->val_int()) { produce an empty result; stop execution } continue execution execute the original WHERE clause (that contains exec_const_cond) ... } As a result, when an expensive constant condition is TRUE, it is evaluated twice - once through JOIN::exec_const_cond, and once through JOIN::cond. When the expensive constant condition is a subquery, predicate, the subquery is evaluated twice. If we have many levels of subqueries, this logic results in a chain of recursive subquery executions that walk a perfect binary tree. The result is that for subquries with depth N, JOIN::exec is executed O(2^N) times. Solution: Notice that the second execution of the constant conditions happens inside do_select(), in the branch: if (join->table_count == join->const_tables) { ... } In this case exec_const_cond is equivalent to the whole WHERE clause, therefore the WHERE clause has already been checked in the beginnig of JOIN::exec, and has been found to be true. The bug is addressed by not evaluating the WHERE clause if there was exec_const_conds, and it was TRUE.
14 years ago
  1. select @@optimizer_switch like '%subquery_cache=on%';
  2. @@optimizer_switch like '%subquery_cache=on%'
  3. 1
  4. set optimizer_switch='subquery_cache=off';
  5. set join_cache_level=1;
  6. set @join_cache_level_for_subselect_test=@@join_cache_level;
  7. drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t11,t12;
  8. drop view if exists v2;
  9. set @subselect_tmp=@@optimizer_switch;
  10. set @@optimizer_switch=ifnull(@optimizer_switch_for_subselect_test,
  11. "semijoin=on,firstmatch=on,loosescan=on,semijoin_with_cache=on,partial_match_rowid_merge=off,partial_match_table_scan=off");
  12. set join_cache_level=@join_cache_level_for_subselect_test;
  13. set optimizer_switch='mrr=on,mrr_sort_keys=on,index_condition_pushdown=on';
  14. select (select 2);
  15. (select 2)
  16. 2
  17. explain extended select (select 2);
  18. id select_type table type possible_keys key key_len ref rows filtered Extra
  19. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  20. Warnings:
  21. Note 1249 Select 2 was reduced during optimization
  22. Note 1003 select 2 AS `(select 2)`
  23. SELECT (SELECT 1) UNION SELECT (SELECT 2);
  24. (SELECT 1)
  25. 1
  26. 2
  27. explain extended SELECT (SELECT 1) UNION SELECT (SELECT 2);
  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 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  31. NULL UNION RESULT <union1,3> ALL NULL NULL NULL NULL NULL NULL
  32. Warnings:
  33. Note 1249 Select 2 was reduced during optimization
  34. Note 1249 Select 4 was reduced during optimization
  35. Note 1003 select 1 AS `(SELECT 1)` union select 2 AS `(SELECT 2)`
  36. SELECT (SELECT (SELECT 0 UNION SELECT 0));
  37. (SELECT (SELECT 0 UNION SELECT 0))
  38. 0
  39. explain extended SELECT (SELECT (SELECT 0 UNION SELECT 0));
  40. id select_type table type possible_keys key key_len ref rows filtered Extra
  41. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  42. 3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  43. 4 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  44. NULL UNION RESULT <union3,4> ALL NULL NULL NULL NULL NULL NULL
  45. Warnings:
  46. Note 1249 Select 2 was reduced during optimization
  47. Note 1003 select (select 0 union select 0) AS `(SELECT (SELECT 0 UNION SELECT 0))`
  48. SELECT (SELECT 1 FROM (SELECT 1) as b HAVING a=1) as a;
  49. ERROR 42S22: Reference 'a' not supported (forward reference in item list)
  50. 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;
  51. ERROR 42S22: Reference 'b' not supported (forward reference in item list)
  52. SELECT (SELECT 1),MAX(1) FROM (SELECT 1) as a;
  53. (SELECT 1) MAX(1)
  54. 1 1
  55. SELECT (SELECT a) as a;
  56. ERROR 42S22: Reference 'a' not supported (forward reference in item list)
  57. EXPLAIN EXTENDED SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1;
  58. id select_type table type possible_keys key key_len ref rows filtered Extra
  59. 1 PRIMARY <derived2> system NULL NULL NULL NULL 1 100.00
  60. 3 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  61. 2 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  62. Warnings:
  63. Note 1276 Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1
  64. Note 1276 Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1
  65. Note 1003 select 1 AS `1` from (select 1 AS `a`) `b` having ((select 1) = 1)
  66. SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1;
  67. 1
  68. 1
  69. SELECT (SELECT 1), a;
  70. ERROR 42S22: Unknown column 'a' in 'field list'
  71. SELECT 1 as a FROM (SELECT 1) as b HAVING (SELECT a)=1;
  72. a
  73. 1
  74. SELECT 1 FROM (SELECT (SELECT a) b) c;
  75. ERROR 42S22: Unknown column 'a' in 'field list'
  76. SELECT * FROM (SELECT 1 as id) b WHERE id IN (SELECT * FROM (SELECT 1 as id) c ORDER BY id);
  77. id
  78. 1
  79. SELECT * FROM (SELECT 1) a WHERE 1 IN (SELECT 1,1);
  80. ERROR 21000: Operand should contain 1 column(s)
  81. SELECT 1 IN (SELECT 1);
  82. 1 IN (SELECT 1)
  83. 1
  84. SELECT 1 FROM (SELECT 1 as a) b WHERE 1 IN (SELECT (SELECT a));
  85. 1
  86. 1
  87. select (SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE(1));
  88. ERROR HY000: Incorrect usage of PROCEDURE and subquery
  89. SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE((SELECT 1));
  90. ERROR HY000: Incorrect parameters to procedure 'ANALYSE'
  91. SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NULL;
  92. ERROR 42S22: Unknown column 'a' in 'field list'
  93. SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NOT NULL;
  94. ERROR 42S22: Unknown column 'a' in 'field list'
  95. SELECT (SELECT 1,2,3) = ROW(1,2,3);
  96. (SELECT 1,2,3) = ROW(1,2,3)
  97. 1
  98. SELECT (SELECT 1,2,3) = ROW(1,2,1);
  99. (SELECT 1,2,3) = ROW(1,2,1)
  100. 0
  101. SELECT (SELECT 1,2,3) < ROW(1,2,1);
  102. (SELECT 1,2,3) < ROW(1,2,1)
  103. 0
  104. SELECT (SELECT 1,2,3) > ROW(1,2,1);
  105. (SELECT 1,2,3) > ROW(1,2,1)
  106. 1
  107. SELECT (SELECT 1,2,3) = ROW(1,2,NULL);
  108. (SELECT 1,2,3) = ROW(1,2,NULL)
  109. NULL
  110. SELECT ROW(1,2,3) = (SELECT 1,2,3);
  111. ROW(1,2,3) = (SELECT 1,2,3)
  112. 1
  113. SELECT ROW(1,2,3) = (SELECT 1,2,1);
  114. ROW(1,2,3) = (SELECT 1,2,1)
  115. 0
  116. SELECT ROW(1,2,3) < (SELECT 1,2,1);
  117. ROW(1,2,3) < (SELECT 1,2,1)
  118. 0
  119. SELECT ROW(1,2,3) > (SELECT 1,2,1);
  120. ROW(1,2,3) > (SELECT 1,2,1)
  121. 1
  122. SELECT ROW(1,2,3) = (SELECT 1,2,NULL);
  123. ROW(1,2,3) = (SELECT 1,2,NULL)
  124. NULL
  125. SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a');
  126. (SELECT 1.5,2,'a') = ROW(1.5,2,'a')
  127. 1
  128. SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b');
  129. (SELECT 1.5,2,'a') = ROW(1.5,2,'b')
  130. 0
  131. SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b');
  132. (SELECT 1.5,2,'a') = ROW('1.5b',2,'b')
  133. 0
  134. Warnings:
  135. Warning 1292 Truncated incorrect DOUBLE value: '1.5b'
  136. SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a');
  137. (SELECT 'b',2,'a') = ROW(1.5,2,'a')
  138. 0
  139. SELECT (SELECT 1.5,2,'a') = ROW(1.5,'2','a');
  140. (SELECT 1.5,2,'a') = ROW(1.5,'2','a')
  141. 1
  142. SELECT (SELECT 1.5,'c','a') = ROW(1.5,2,'a');
  143. (SELECT 1.5,'c','a') = ROW(1.5,2,'a')
  144. 0
  145. SELECT (SELECT * FROM (SELECT 'test' a,'test' b) a);
  146. ERROR 21000: Operand should contain 1 column(s)
  147. SELECT 1 as a,(SELECT a+a) b,(SELECT b);
  148. a b (SELECT b)
  149. 1 2 2
  150. create table t1 (a int);
  151. create table t2 (a int, b int);
  152. create table t3 (a int);
  153. create table t4 (a int not null, b int not null);
  154. insert into t1 values (2);
  155. insert into t2 values (1,7),(2,7);
  156. insert into t4 values (4,8),(3,8),(5,9);
  157. select (select a from t1 where t1.a = a1) as a2, (select b from t2 where t2.b=a2) as a1;
  158. ERROR 42S22: Reference 'a1' not supported (forward reference in item list)
  159. select (select a from t1 where t1.a=t2.a), a from t2;
  160. (select a from t1 where t1.a=t2.a) a
  161. NULL 1
  162. 2 2
  163. select (select a from t1 where t1.a=t2.b), a from t2;
  164. (select a from t1 where t1.a=t2.b) a
  165. NULL 1
  166. NULL 2
  167. select (select a from t1), a, (select 1 union select 2 limit 1) from t2;
  168. (select a from t1) a (select 1 union select 2 limit 1)
  169. 2 1 1
  170. 2 2 1
  171. select (select a from t3), a from t2;
  172. (select a from t3) a
  173. NULL 1
  174. NULL 2
  175. select * from t2 where t2.a=(select a from t1);
  176. a b
  177. 2 7
  178. insert into t3 values (6),(7),(3);
  179. select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1);
  180. a b
  181. 1 7
  182. 2 7
  183. (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;
  184. a b
  185. 1 7
  186. 2 7
  187. 3 8
  188. (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);
  189. a b
  190. 1 7
  191. 2 7
  192. 4 8
  193. 3 8
  194. 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);
  195. id select_type table type possible_keys key key_len ref rows filtered Extra
  196. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
  197. 2 SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using filesort
  198. 3 UNION t4 ALL NULL NULL NULL NULL 3 100.00 Using where
  199. 4 SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00
  200. NULL UNION RESULT <union1,3> ALL NULL NULL NULL NULL NULL NULL
  201. Warnings:
  202. 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`)
  203. select (select a from t3 where a<t2.a*4 order by 1 desc limit 1), a from t2;
  204. (select a from t3 where a<t2.a*4 order by 1 desc limit 1) a
  205. 3 1
  206. 7 2
  207. select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from
  208. (select * from t2 where a>1) as tt;
  209. (select t3.a from t3 where a<8 order by 1 desc limit 1) a
  210. 7 2
  211. set @tmp_optimizer_switch=@@optimizer_switch;
  212. set optimizer_switch='derived_merge=off,derived_with_keys=off';
  213. explain extended select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from
  214. (select * from t2 where a>1) as tt;
  215. id select_type table type possible_keys key key_len ref rows filtered Extra
  216. 1 PRIMARY <derived3> ALL NULL NULL NULL NULL 2 100.00
  217. 3 DERIVED t2 ALL NULL NULL NULL NULL 2 100.00 Using where
  218. 2 SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using filesort
  219. Warnings:
  220. 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`
  221. set optimizer_switch=@tmp_optimizer_switch;
  222. 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);
  223. a
  224. 2
  225. 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);
  226. a
  227. 2
  228. 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);
  229. a
  230. select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
  231. b (select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)
  232. 8 7.5000
  233. 8 4.5000
  234. 9 7.5000
  235. explain extended select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
  236. id select_type table type possible_keys key key_len ref rows filtered Extra
  237. 1 PRIMARY t4 ALL NULL NULL NULL NULL 3 100.00
  238. 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00
  239. 3 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
  240. Warnings:
  241. Note 1276 Field or reference 'test.t4.a' of SELECT #3 was resolved in SELECT #1
  242. Note 1003 select `test`.`t4`.`b` AS `b`,(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`
  243. select * from t3 where exists (select * from t2 where t2.b=t3.a);
  244. a
  245. 7
  246. select * from t3 where not exists (select * from t2 where t2.b=t3.a);
  247. a
  248. 6
  249. 3
  250. select * from t3 where a in (select b from t2);
  251. a
  252. 7
  253. select * from t3 where a not in (select b from t2);
  254. a
  255. 6
  256. 3
  257. select * from t3 where a = some (select b from t2);
  258. a
  259. 7
  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. 7
  267. select * from t3 where a <> all (select b from t2);
  268. a
  269. 6
  270. 3
  271. insert into t2 values (100, 5);
  272. select * from t3 where a < any (select b from t2);
  273. a
  274. 6
  275. 3
  276. select * from t3 where a < all (select b from t2);
  277. a
  278. 3
  279. select * from t3 where a >= any (select b from t2);
  280. a
  281. 6
  282. 7
  283. explain extended select * from t3 where a >= any (select b from t2);
  284. id select_type table type possible_keys key key_len ref rows filtered Extra
  285. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
  286. 2 SUBQUERY t2 ALL NULL NULL NULL NULL 3 100.00
  287. Warnings:
  288. 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`))))
  289. select * from t3 where a >= all (select b from t2);
  290. a
  291. 7
  292. delete from t2 where a=100;
  293. select * from t3 where a in (select a,b from t2);
  294. ERROR 21000: Operand should contain 1 column(s)
  295. select * from t3 where a in (select * from t2);
  296. ERROR 21000: Operand should contain 1 column(s)
  297. insert into t4 values (12,7),(1,7),(10,9),(9,6),(7,6),(3,9),(1,10);
  298. select b,max(a) as ma from t4 group by b having b < (select max(t2.a) from t2 where t2.b=t4.b);
  299. b ma
  300. insert into t2 values (2,10);
  301. select b,max(a) as ma from t4 group by b having ma < (select max(t2.a) from t2 where t2.b=t4.b);
  302. b ma
  303. 10 1
  304. delete from t2 where a=2 and b=10;
  305. select b,max(a) as ma from t4 group by b having b >= (select max(t2.a) from t2 where t2.b=t4.b);
  306. b ma
  307. 7 12
  308. create table t5 (a int);
  309. select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
  310. (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
  311. NULL 1
  312. 2 2
  313. insert into t5 values (5);
  314. select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
  315. (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
  316. NULL 1
  317. 2 2
  318. insert into t5 values (2);
  319. select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
  320. (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
  321. NULL 1
  322. 2 2
  323. 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;
  324. id select_type table type possible_keys key key_len ref rows filtered Extra
  325. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00
  326. 2 DEPENDENT SUBQUERY t1 system NULL NULL NULL NULL 1 100.00
  327. 3 DEPENDENT UNION t5 ALL NULL NULL NULL NULL 2 100.00 Using where
  328. NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
  329. Warnings:
  330. Note 1276 Field or reference 'test.t2.a' of SELECT #2 was resolved in SELECT #1
  331. Note 1276 Field or reference 'test.t2.a' of SELECT #3 was resolved in SELECT #1
  332. Note 1003 select (select 2 from `test`.`t1` where (2 = `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`
  333. select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2;
  334. ERROR 21000: Subquery returns more than 1 row
  335. create table t6 (patient_uq int, clinic_uq int, index i1 (clinic_uq));
  336. create table t7( uq int primary key, name char(25));
  337. insert into t7 values(1,"Oblastnaia bolnitsa"),(2,"Bolnitsa Krasnogo Kresta");
  338. insert into t6 values (1,1),(1,2),(2,2),(1,3);
  339. select * from t6 where exists (select * from t7 where uq = clinic_uq);
  340. patient_uq clinic_uq
  341. 1 1
  342. 1 2
  343. 2 2
  344. explain extended select * from t6 where exists (select * from t7 where uq = clinic_uq);
  345. id select_type table type possible_keys key key_len ref rows filtered Extra
  346. 1 PRIMARY t6 ALL NULL NULL NULL NULL 4 100.00 Using where
  347. 2 DEPENDENT SUBQUERY t7 eq_ref PRIMARY PRIMARY 4 test.t6.clinic_uq 1 100.00 Using index
  348. Warnings:
  349. Note 1276 Field or reference 'test.t6.clinic_uq' of SELECT #2 was resolved in SELECT #1
  350. Note 1003 select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t6` where exists(select 1 from `test`.`t7` where (`test`.`t7`.`uq` = `test`.`t6`.`clinic_uq`))
  351. select * from t1 where a= (select a from t2,t4 where t2.b=t4.b);
  352. ERROR 23000: Column 'a' in field list is ambiguous
  353. drop table t1,t2,t3;
  354. CREATE TABLE t3 (a varchar(20),b char(1) NOT NULL default '0');
  355. INSERT INTO t3 VALUES ('W','a'),('A','c'),('J','b');
  356. CREATE TABLE t2 (a varchar(20),b int NOT NULL default '0');
  357. INSERT INTO t2 VALUES ('W','1'),('A','3'),('J','2');
  358. CREATE TABLE t1 (a varchar(20),b date NOT NULL default '0000-00-00');
  359. INSERT INTO t1 VALUES ('W','1732-02-22'),('A','1735-10-30'),('J','1743-04-13');
  360. SELECT * FROM t1 WHERE b = (SELECT MIN(b) FROM t1);
  361. a b
  362. W 1732-02-22
  363. SELECT * FROM t2 WHERE b = (SELECT MIN(b) FROM t2);
  364. a b
  365. W 1
  366. SELECT * FROM t3 WHERE b = (SELECT MIN(b) FROM t3);
  367. a b
  368. W a
  369. CREATE TABLE `t8` (
  370. `pseudo` varchar(35) character set latin1 NOT NULL default '',
  371. `email` varchar(60) character set latin1 NOT NULL default '',
  372. PRIMARY KEY (`pseudo`),
  373. UNIQUE KEY `email` (`email`)
  374. ) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
  375. INSERT INTO t8 (pseudo,email) VALUES ('joce','test');
  376. INSERT INTO t8 (pseudo,email) VALUES ('joce1','test1');
  377. INSERT INTO t8 (pseudo,email) VALUES ('2joce1','2test1');
  378. 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');
  379. id select_type table type possible_keys key key_len ref rows filtered Extra
  380. 1 PRIMARY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using where; Using index
  381. 4 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using index
  382. 2 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using where
  383. 3 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using index
  384. Warnings:
  385. 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))
  386. SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo,email FROM
  387. t8 WHERE pseudo='joce');
  388. ERROR 21000: Operand should contain 1 column(s)
  389. SELECT pseudo FROM t8 WHERE pseudo=(SELECT * FROM t8 WHERE
  390. pseudo='joce');
  391. ERROR 21000: Operand should contain 1 column(s)
  392. SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce');
  393. pseudo
  394. joce
  395. SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo LIKE '%joce%');
  396. ERROR 21000: Subquery returns more than 1 row
  397. drop table if exists t1,t2,t3,t4,t5,t6,t7,t8;
  398. CREATE TABLE `t1` (
  399. `topic` mediumint(8) unsigned NOT NULL default '0',
  400. `date` date NOT NULL default '0000-00-00',
  401. `pseudo` varchar(35) character set latin1 NOT NULL default '',
  402. PRIMARY KEY (`pseudo`,`date`,`topic`),
  403. KEY `topic` (`topic`)
  404. ) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
  405. INSERT INTO t1 (topic,date,pseudo) VALUES
  406. ('43506','2002-10-02','joce'),('40143','2002-08-03','joce');
  407. EXPLAIN EXTENDED SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
  408. id select_type table type possible_keys key key_len ref rows filtered Extra
  409. 1 SIMPLE t1 index NULL PRIMARY 43 NULL 2 100.00 Using where; Using index
  410. Warnings:
  411. Note 1003 select distinct `test`.`t1`.`date` AS `date` from `test`.`t1` where (`test`.`t1`.`date` = '2002-08-03')
  412. EXPLAIN EXTENDED SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
  413. id select_type table type possible_keys key key_len ref rows filtered Extra
  414. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  415. 2 SUBQUERY t1 index NULL PRIMARY 43 NULL 2 100.00 Using where; Using index
  416. Warnings:
  417. 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')`
  418. SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
  419. date
  420. 2002-08-03
  421. SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
  422. (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03')
  423. 2002-08-03
  424. SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1) UNION ALL SELECT 1;
  425. 1
  426. 1
  427. 1
  428. 1
  429. SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1) UNION SELECT 1;
  430. ERROR 21000: Subquery returns more than 1 row
  431. EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1);
  432. id select_type table type possible_keys key key_len ref rows filtered Extra
  433. 1 PRIMARY t1 index NULL topic 3 NULL 2 100.00 Using index
  434. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  435. 3 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  436. NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
  437. Warnings:
  438. Note 1003 select 1 AS `1` from `test`.`t1` where (1 = (select 1 union select 1))
  439. drop table t1;
  440. CREATE TABLE `t1` (
  441. `numeropost` mediumint(8) unsigned NOT NULL auto_increment,
  442. `maxnumrep` int(10) unsigned NOT NULL default '0',
  443. PRIMARY KEY (`numeropost`),
  444. UNIQUE KEY `maxnumrep` (`maxnumrep`)
  445. ) ENGINE=MyISAM ROW_FORMAT=FIXED;
  446. INSERT INTO t1 (numeropost,maxnumrep) VALUES (40143,1),(43506,2);
  447. CREATE TABLE `t2` (
  448. `mot` varchar(30) NOT NULL default '',
  449. `topic` mediumint(8) unsigned NOT NULL default '0',
  450. `date` date NOT NULL default '0000-00-00',
  451. `pseudo` varchar(35) NOT NULL default '',
  452. PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`)
  453. ) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
  454. INSERT INTO t2 (mot,topic,date,pseudo) VALUES ('joce','40143','2002-10-22','joce'), ('joce','43506','2002-10-22','joce');
  455. select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
  456. a
  457. 40143
  458. 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;
  459. numeropost maxnumrep
  460. 43506 2
  461. 40143 1
  462. SELECT (SELECT 1) as a FROM (SELECT 1 FROM t1 HAVING a=1) b;
  463. ERROR 42S22: Unknown column 'a' in 'having clause'
  464. SELECT 1 IN (SELECT 1 FROM t2 HAVING a);
  465. ERROR 42S22: Unknown column 'a' in 'having clause'
  466. SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic);
  467. mot topic date pseudo
  468. joce 40143 2002-10-22 joce
  469. joce 43506 2002-10-22 joce
  470. SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
  471. mot topic date pseudo
  472. SELECT * from t2 where topic IN (SELECT SUM(topic) FROM t1);
  473. mot topic date pseudo
  474. SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic);
  475. mot topic date pseudo
  476. joce 40143 2002-10-22 joce
  477. joce 43506 2002-10-22 joce
  478. SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
  479. mot topic date pseudo
  480. SELECT * from t2 where topic = any (SELECT SUM(topic) FROM t1);
  481. mot topic date pseudo
  482. SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic);
  483. mot topic date pseudo
  484. SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
  485. mot topic date pseudo
  486. joce 40143 2002-10-22 joce
  487. joce 43506 2002-10-22 joce
  488. SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) from t2;
  489. mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100)
  490. joce 40143 2002-10-22 joce 1
  491. joce 43506 2002-10-22 joce 1
  492. SELECT * from t2 where topic = all (SELECT SUM(topic) FROM t2);
  493. mot topic date pseudo
  494. SELECT * from t2 where topic <> any (SELECT SUM(topic) FROM t2);
  495. mot topic date pseudo
  496. joce 40143 2002-10-22 joce
  497. joce 43506 2002-10-22 joce
  498. SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
  499. mot topic date pseudo
  500. joce 40143 2002-10-22 joce
  501. SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
  502. mot topic date pseudo
  503. joce 40143 2002-10-22 joce
  504. SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
  505. mot topic date pseudo
  506. joce 40143 2002-10-22 joce
  507. SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) from t2;
  508. mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000)
  509. joce 40143 2002-10-22 joce 1
  510. joce 43506 2002-10-22 joce 0
  511. drop table t1,t2;
  512. CREATE TABLE `t1` (
  513. `numeropost` mediumint(8) unsigned NOT NULL auto_increment,
  514. `maxnumrep` int(10) unsigned NOT NULL default '0',
  515. PRIMARY KEY (`numeropost`),
  516. UNIQUE KEY `maxnumrep` (`maxnumrep`)
  517. ) ENGINE=MyISAM ROW_FORMAT=FIXED;
  518. INSERT INTO t1 (numeropost,maxnumrep) VALUES (1,0),(2,1);
  519. select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
  520. ERROR 21000: Subquery returns more than 1 row
  521. select numeropost as a FROM t1 ORDER BY (SELECT 1 FROM t1 HAVING a=1);
  522. ERROR 21000: Subquery returns more than 1 row
  523. show warnings;
  524. Level Code Message
  525. Error 1242 Subquery returns more than 1 row
  526. drop table t1;
  527. create table t1 (a int);
  528. insert into t1 values (1),(2),(3);
  529. (select * from t1) union (select * from t1) order by (select a from t1 limit 1);
  530. a
  531. 1
  532. 2
  533. 3
  534. drop table t1;
  535. CREATE TABLE t1 (field char(1) NOT NULL DEFAULT 'b');
  536. INSERT INTO t1 VALUES ();
  537. SELECT field FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1 FROM (SELECT 1) a HAVING field='b');
  538. ERROR 21000: Subquery returns more than 1 row
  539. drop table t1;
  540. CREATE TABLE `t1` (
  541. `numeropost` mediumint(8) unsigned NOT NULL default '0',
  542. `numreponse` int(10) unsigned NOT NULL auto_increment,
  543. `pseudo` varchar(35) NOT NULL default '',
  544. PRIMARY KEY (`numeropost`,`numreponse`),
  545. UNIQUE KEY `numreponse` (`numreponse`),
  546. KEY `pseudo` (`pseudo`,`numeropost`)
  547. ) ENGINE=MyISAM;
  548. SELECT (SELECT numeropost FROM t1 HAVING numreponse=a),numreponse FROM (SELECT * FROM t1) as a;
  549. ERROR 42S22: Reference 'numreponse' not supported (forward reference in item list)
  550. SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=a) FROM (SELECT * FROM t1) as a;
  551. ERROR 42S22: Unknown column 'a' in 'having clause'
  552. SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=1) FROM (SELECT * FROM t1) as a;
  553. numreponse (SELECT numeropost FROM t1 HAVING numreponse=1)
  554. INSERT INTO t1 (numeropost,numreponse,pseudo) VALUES (1,1,'joce'),(1,2,'joce'),(1,3,'test');
  555. EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1');
  556. id select_type table type possible_keys key key_len ref rows filtered Extra
  557. 1 PRIMARY t1 const PRIMARY,numreponse PRIMARY 7 const,const 1 100.00 Using where; Using index
  558. 2 SUBQUERY t1 ref PRIMARY PRIMARY 3 const 2 100.00 Using index
  559. Warnings:
  560. 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'))))
  561. SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1');
  562. ERROR 21000: Subquery returns more than 1 row
  563. EXPLAIN EXTENDED SELECT MAX(numreponse) FROM t1 WHERE numeropost='1';
  564. id select_type table type possible_keys key key_len ref rows filtered Extra
  565. 1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
  566. Warnings:
  567. Note 1003 select max(`test`.`t1`.`numreponse`) AS `MAX(numreponse)` from `test`.`t1` where (`test`.`t1`.`numeropost` = '1')
  568. EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT MAX(numreponse) FROM t1 WHERE numeropost='1');
  569. id select_type table type possible_keys key key_len ref rows filtered Extra
  570. 1 PRIMARY t1 const PRIMARY,numreponse PRIMARY 7 const,const 1 100.00 Using where; Using index
  571. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
  572. Warnings:
  573. 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'))))
  574. drop table t1;
  575. CREATE TABLE t1 (a int(1));
  576. INSERT INTO t1 VALUES (1);
  577. SELECT 1 FROM (SELECT a FROM t1) b HAVING (SELECT b.a)=1;
  578. 1
  579. 1
  580. drop table t1;
  581. create table t1 (a int NOT NULL, b int, primary key (a));
  582. create table t2 (a int NOT NULL, b int, primary key (a));
  583. insert into t1 values (0, 10),(1, 11),(2, 12);
  584. insert into t2 values (1, 21),(2, 22),(3, 23);
  585. select * from t1;
  586. a b
  587. 0 10
  588. 1 11
  589. 2 12
  590. update t1 set b= (select b from t1);
  591. ERROR HY000: You can't specify target table 't1' for update in FROM clause
  592. update t1 set b= (select b from t2);
  593. ERROR 21000: Subquery returns more than 1 row
  594. update t1 set b= (select b from t2 where t1.a = t2.a);
  595. select * from t1;
  596. a b
  597. 0 NULL
  598. 1 21
  599. 2 22
  600. drop table t1, t2;
  601. create table t1 (a int NOT NULL, b int, primary key (a));
  602. create table t2 (a int NOT NULL, b int, primary key (a));
  603. insert into t1 values (0, 10),(1, 11),(2, 12);
  604. insert into t2 values (1, 21),(2, 12),(3, 23);
  605. select * from t1;
  606. a b
  607. 0 10
  608. 1 11
  609. 2 12
  610. select * from t1 where b = (select b from t2 where t1.a = t2.a);
  611. a b
  612. 2 12
  613. delete from t1 where b in (select b from t1);
  614. ERROR HY000: You can't specify target table 't1' for update in FROM clause
  615. delete from t1 where b = (select b from t2);
  616. ERROR 21000: Subquery returns more than 1 row
  617. delete from t1 where b = (select b from t2 where t1.a = t2.a);
  618. select * from t1;
  619. a b
  620. 0 10
  621. 1 11
  622. drop table t1, t2;
  623. create table t11 (a int NOT NULL, b int, primary key (a));
  624. create table t12 (a int NOT NULL, b int, primary key (a));
  625. create table t2 (a int NOT NULL, b int, primary key (a));
  626. insert into t11 values (0, 10),(1, 11),(2, 12);
  627. insert into t12 values (33, 10),(22, 11),(2, 12);
  628. insert into t2 values (1, 21),(2, 12),(3, 23);
  629. select * from t11;
  630. a b
  631. 0 10
  632. 1 11
  633. 2 12
  634. select * from t12;
  635. a b
  636. 33 10
  637. 22 11
  638. 2 12
  639. delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
  640. ERROR HY000: You can't specify target table 't12' for update in FROM clause
  641. delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
  642. ERROR 21000: Subquery returns more than 1 row
  643. delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
  644. select * from t11;
  645. a b
  646. 0 10
  647. 1 11
  648. select * from t12;
  649. a b
  650. 33 10
  651. 22 11
  652. drop table t11, t12, t2;
  653. CREATE TABLE t1 (x int) ENGINE=MyISAM;
  654. create table t2 (a int) ENGINE=MyISAM;
  655. create table t3 (b int);
  656. insert into t2 values (1);
  657. insert into t3 values (1),(2);
  658. INSERT INTO t1 (x) VALUES ((SELECT x FROM t1));
  659. ERROR HY000: You can't specify target table 't1' for update in FROM clause
  660. INSERT INTO t1 (x) VALUES ((SELECT b FROM t3));
  661. ERROR 21000: Subquery returns more than 1 row
  662. INSERT INTO t1 (x) VALUES ((SELECT a FROM t2));
  663. select * from t1;
  664. x
  665. 1
  666. insert into t2 values (1);
  667. INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
  668. select * from t1;
  669. x
  670. 1
  671. 2
  672. INSERT INTO t1 (x) select (SELECT SUM(a)+1 FROM t2) FROM t2;
  673. select * from t1;
  674. x
  675. 1
  676. 2
  677. 3
  678. 3
  679. INSERT INTO t1 (x) select (SELECT SUM(x)+2 FROM t1) FROM t2;
  680. select * from t1;
  681. x
  682. 1
  683. 2
  684. 3
  685. 3
  686. 11
  687. 11
  688. INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(x) FROM t2));
  689. ERROR 42S22: Unknown column 'x' in 'field list'
  690. INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
  691. select * from t1;
  692. x
  693. 1
  694. 2
  695. 3
  696. 3
  697. 11
  698. 11
  699. 2
  700. drop table t1, t2, t3;
  701. CREATE TABLE t1 (x int not null, y int, primary key (x)) ENGINE=MyISAM;
  702. create table t2 (a int);
  703. create table t3 (a int);
  704. insert into t2 values (1);
  705. insert into t3 values (1),(2);
  706. select * from t1;
  707. x y
  708. replace into t1 (x, y) VALUES ((SELECT x FROM t1), (SELECT a+1 FROM t2));
  709. ERROR HY000: You can't specify target table 't1' for update in FROM clause
  710. replace into t1 (x, y) VALUES ((SELECT a FROM t3), (SELECT a+1 FROM t2));
  711. ERROR 21000: Subquery returns more than 1 row
  712. replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+1 FROM t2));
  713. select * from t1;
  714. x y
  715. 1 2
  716. replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+2 FROM t2));
  717. select * from t1;
  718. x y
  719. 1 3
  720. replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a FROM t2));
  721. select * from t1;
  722. x y
  723. 1 3
  724. 4 1
  725. replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a+1 FROM t2));
  726. select * from t1;
  727. x y
  728. 1 3
  729. 4 2
  730. replace LOW_PRIORITY into t1 (x, y) VALUES ((SELECT a+1 FROM t2), (SELECT a FROM t2));
  731. select * from t1;
  732. x y
  733. 1 3
  734. 4 2
  735. 2 1
  736. drop table t1, t2, t3;
  737. SELECT * FROM (SELECT 1) b WHERE 1 IN (SELECT *);
  738. ERROR HY000: No tables used
  739. CREATE TABLE t2 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;
  740. INSERT INTO t2 VALUES (1),(2);
  741. SELECT * FROM t2 WHERE id IN (SELECT 1);
  742. id
  743. 1
  744. EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1);
  745. id select_type table type possible_keys key key_len ref rows filtered Extra
  746. 1 PRIMARY t2 ref id id 5 const 1 100.00 Using index
  747. Warnings:
  748. Note 1249 Select 2 was reduced during optimization
  749. Note 1003 select `test`.`t2`.`id` AS `id` from `test`.`t2` where (`test`.`t2`.`id` = 1)
  750. SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);
  751. id
  752. 1
  753. SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));
  754. id
  755. 2
  756. EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));
  757. id select_type table type possible_keys key key_len ref rows filtered Extra
  758. 1 PRIMARY t2 ref id id 5 const 1 100.00 Using index
  759. Warnings:
  760. Note 1249 Select 3 was reduced during optimization
  761. Note 1249 Select 2 was reduced during optimization
  762. Note 1003 select `test`.`t2`.`id` AS `id` from `test`.`t2` where (`test`.`t2`.`id` = (1 + 1))
  763. EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);
  764. id select_type table type possible_keys key key_len ref rows filtered Extra
  765. 1 PRIMARY t2 index NULL id 5 NULL 2 100.00 Using where; Using index
  766. 2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  767. 3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  768. NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
  769. Warnings:
  770. Note 1003 select `test`.`t2`.`id` AS `id` from `test`.`t2` where <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))))
  771. SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 3);
  772. id
  773. SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 2);
  774. id
  775. 2
  776. INSERT INTO t2 VALUES ((SELECT * FROM t2));
  777. ERROR HY000: You can't specify target table 't2' for update in FROM clause
  778. INSERT INTO t2 VALUES ((SELECT id FROM t2));
  779. ERROR HY000: You can't specify target table 't2' for update in FROM clause
  780. SELECT * FROM t2;
  781. id
  782. 1
  783. 2
  784. CREATE TABLE t1 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;
  785. INSERT INTO t1 values (1),(1);
  786. UPDATE t2 SET id=(SELECT * FROM t1);
  787. ERROR 21000: Subquery returns more than 1 row
  788. drop table t2, t1;
  789. create table t1 (a int);
  790. insert into t1 values (1),(2),(3);
  791. select 1 IN (SELECT * from t1);
  792. 1 IN (SELECT * from t1)
  793. 1
  794. select 10 IN (SELECT * from t1);
  795. 10 IN (SELECT * from t1)
  796. 0
  797. select NULL IN (SELECT * from t1);
  798. NULL IN (SELECT * from t1)
  799. NULL
  800. update t1 set a=NULL where a=2;
  801. select 1 IN (SELECT * from t1);
  802. 1 IN (SELECT * from t1)
  803. 1
  804. select 3 IN (SELECT * from t1);
  805. 3 IN (SELECT * from t1)
  806. 1
  807. select 10 IN (SELECT * from t1);
  808. 10 IN (SELECT * from t1)
  809. NULL
  810. select 1 > ALL (SELECT * from t1);
  811. 1 > ALL (SELECT * from t1)
  812. 0
  813. select 10 > ALL (SELECT * from t1);
  814. 10 > ALL (SELECT * from t1)
  815. NULL
  816. select 1 > ANY (SELECT * from t1);
  817. 1 > ANY (SELECT * from t1)
  818. NULL
  819. select 10 > ANY (SELECT * from t1);
  820. 10 > ANY (SELECT * from t1)
  821. 1
  822. drop table t1;
  823. create table t1 (a varchar(20));
  824. insert into t1 values ('A'),('BC'),('DEF');
  825. select 'A' IN (SELECT * from t1);
  826. 'A' IN (SELECT * from t1)
  827. 1
  828. select 'XYZS' IN (SELECT * from t1);
  829. 'XYZS' IN (SELECT * from t1)
  830. 0
  831. select NULL IN (SELECT * from t1);
  832. NULL IN (SELECT * from t1)
  833. NULL
  834. update t1 set a=NULL where a='BC';
  835. select 'A' IN (SELECT * from t1);
  836. 'A' IN (SELECT * from t1)
  837. 1
  838. select 'DEF' IN (SELECT * from t1);
  839. 'DEF' IN (SELECT * from t1)
  840. 1
  841. select 'XYZS' IN (SELECT * from t1);
  842. 'XYZS' IN (SELECT * from t1)
  843. NULL
  844. select 'A' > ALL (SELECT * from t1);
  845. 'A' > ALL (SELECT * from t1)
  846. 0
  847. select 'XYZS' > ALL (SELECT * from t1);
  848. 'XYZS' > ALL (SELECT * from t1)
  849. NULL
  850. select 'A' > ANY (SELECT * from t1);
  851. 'A' > ANY (SELECT * from t1)
  852. NULL
  853. select 'XYZS' > ANY (SELECT * from t1);
  854. 'XYZS' > ANY (SELECT * from t1)
  855. 1
  856. drop table t1;
  857. create table t1 (a float);
  858. insert into t1 values (1.5),(2.5),(3.5);
  859. select 1.5 IN (SELECT * from t1);
  860. 1.5 IN (SELECT * from t1)
  861. 1
  862. select 10.5 IN (SELECT * from t1);
  863. 10.5 IN (SELECT * from t1)
  864. 0
  865. select NULL IN (SELECT * from t1);
  866. NULL IN (SELECT * from t1)
  867. NULL
  868. update t1 set a=NULL where a=2.5;
  869. select 1.5 IN (SELECT * from t1);
  870. 1.5 IN (SELECT * from t1)
  871. 1
  872. select 3.5 IN (SELECT * from t1);
  873. 3.5 IN (SELECT * from t1)
  874. 1
  875. select 10.5 IN (SELECT * from t1);
  876. 10.5 IN (SELECT * from t1)
  877. NULL
  878. select 1.5 > ALL (SELECT * from t1);
  879. 1.5 > ALL (SELECT * from t1)
  880. 0
  881. select 10.5 > ALL (SELECT * from t1);
  882. 10.5 > ALL (SELECT * from t1)
  883. NULL
  884. select 1.5 > ANY (SELECT * from t1);
  885. 1.5 > ANY (SELECT * from t1)
  886. NULL
  887. select 10.5 > ANY (SELECT * from t1);
  888. 10.5 > ANY (SELECT * from t1)
  889. 1
  890. explain extended select (select a+1) from t1;
  891. id select_type table type possible_keys key key_len ref rows filtered Extra
  892. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
  893. Warnings:
  894. Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
  895. Note 1249 Select 2 was reduced during optimization
  896. Note 1003 select (`test`.`t1`.`a` + 1) AS `(select a+1)` from `test`.`t1`
  897. select (select a+1) from t1;
  898. (select a+1)
  899. 2.5
  900. NULL
  901. 4.5
  902. drop table t1;
  903. CREATE TABLE t1 (a int(11) NOT NULL default '0', PRIMARY KEY (a));
  904. CREATE TABLE t2 (a int(11) default '0', INDEX (a));
  905. INSERT INTO t1 VALUES (1),(2),(3),(4);
  906. INSERT INTO t2 VALUES (1),(2),(3);
  907. SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
  908. a t1.a in (select t2.a from t2)
  909. 1 1
  910. 2 1
  911. 3 1
  912. 4 0
  913. explain extended SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
  914. id select_type table type possible_keys key key_len ref rows filtered Extra
  915. 1 PRIMARY t1 index NULL PRIMARY 4 NULL 4 100.00 Using index
  916. 2 DEPENDENT SUBQUERY t2 index_subquery a a 5 func 2 100.00 Using index
  917. Warnings:
  918. Note 1003 select `test`.`t1`.`a` AS `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`
  919. CREATE TABLE t3 (a int(11) default '0');
  920. INSERT INTO t3 VALUES (1),(2),(3);
  921. SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
  922. a t1.a in (select t2.a from t2,t3 where t3.a=t2.a)
  923. 1 1
  924. 2 1
  925. 3 1
  926. 4 0
  927. explain extended SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
  928. id select_type table type possible_keys key key_len ref rows filtered Extra
  929. 1 PRIMARY t1 index NULL PRIMARY 4 NULL 4 100.00 Using index
  930. 2 DEPENDENT SUBQUERY t2 ref_or_null a a 5 func 2 100.00 Using where; Using index
  931. 2 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join)
  932. Warnings:
  933. Note 1003 select `test`.`t1`.`a` AS `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`
  934. drop table t1,t2,t3;
  935. # check correct NULL Processing for normal IN/ALL/ANY
  936. # and 2 ways of max/min optimization
  937. create table t1 (a int);
  938. insert into t1 values (1), (100), (NULL), (1000);
  939. create table t2 (a int not null);
  940. # subselect returns empty set (for NULL and non-NULL left part)
  941. select a, a in (select * from t2) from t1;
  942. a a in (select * from t2)
  943. 1 0
  944. 100 0
  945. NULL 0
  946. 1000 0
  947. select a, a > any (select * from t2) from t1;
  948. a a > any (select * from t2)
  949. 1 0
  950. 100 0
  951. NULL 0
  952. 1000 0
  953. select a, a > all (select * from t2) from t1;
  954. a a > all (select * from t2)
  955. 1 1
  956. 100 1
  957. NULL 1
  958. 1000 1
  959. select a from t1 where a in (select * from t2);
  960. a
  961. select a from t1 where a > any (select * from t2);
  962. a
  963. select a from t1 where a > all (select * from t2);
  964. a
  965. 1
  966. 100
  967. NULL
  968. 1000
  969. select a from t1 where a in (select * from t2 group by a);
  970. a
  971. select a from t1 where a > any (select * from t2 group by a);
  972. a
  973. select a from t1 where a > all (select * from t2 group by a);
  974. a
  975. 1
  976. 100
  977. NULL
  978. 1000
  979. insert into t2 values (1),(200);
  980. # sebselect returns non-empty set without NULLs
  981. select a, a in (select * from t2) from t1;
  982. a a in (select * from t2)
  983. 1 1
  984. 100 0
  985. NULL NULL
  986. 1000 0
  987. select a, a > any (select * from t2) from t1;
  988. a a > any (select * from t2)
  989. 1 0
  990. 100 1
  991. NULL NULL
  992. 1000 1
  993. select a, a > all (select * from t2) from t1;
  994. a a > all (select * from t2)
  995. 1 0
  996. 100 0
  997. NULL NULL
  998. 1000 1
  999. select a from t1 where a in (select * from t2);
  1000. a
  1001. 1
  1002. select a from t1 where a > any (select * from t2);
  1003. a
  1004. 100
  1005. 1000
  1006. select a from t1 where a > all (select * from t2);
  1007. a
  1008. 1000
  1009. select a from t1 where a in (select * from t2 group by a);
  1010. a
  1011. 1
  1012. select a from t1 where a > any (select * from t2 group by a);
  1013. a
  1014. 100
  1015. 1000
  1016. select a from t1 where a > all (select * from t2 group by a);
  1017. a
  1018. 1000
  1019. drop table t2;
  1020. create table t2 (a int);
  1021. insert into t2 values (1),(NULL),(200);
  1022. # sebselect returns non-empty set with NULLs
  1023. select a, a in (select * from t2) from t1;
  1024. a a in (select * from t2)
  1025. 1 1
  1026. 100 NULL
  1027. NULL NULL
  1028. 1000 NULL
  1029. select a, a > any (select * from t2) from t1;
  1030. a a > any (select * from t2)
  1031. 1 NULL
  1032. 100 1
  1033. NULL NULL
  1034. 1000 1
  1035. select a, a > all (select * from t2) from t1;
  1036. a a > all (select * from t2)
  1037. 1 0
  1038. 100 0
  1039. NULL NULL
  1040. 1000 NULL
  1041. select a from t1 where a in (select * from t2);
  1042. a
  1043. 1
  1044. select a from t1 where a > any (select * from t2);
  1045. a
  1046. 100
  1047. 1000
  1048. select a from t1 where a > all (select * from t2);
  1049. a
  1050. select a from t1 where a in (select * from t2 group by a);
  1051. a
  1052. 1
  1053. select a from t1 where a > any (select * from t2 group by a);
  1054. a
  1055. 100
  1056. 1000
  1057. select a from t1 where a > all (select * from t2 group by a);
  1058. a
  1059. drop table t1, t2;
  1060. create table t1 (a float);
  1061. select 10.5 IN (SELECT * from t1 LIMIT 1);
  1062. ERROR 42000: This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
  1063. select 10.5 IN (SELECT * from t1 LIMIT 1 UNION SELECT 1.5);
  1064. ERROR 42000: This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
  1065. drop table t1;
  1066. create table t1 (a int, b int, c varchar(10));
  1067. create table t2 (a int);
  1068. insert into t1 values (1,2,'a'),(2,3,'b'),(3,4,'c');
  1069. insert into t2 values (1),(2),(NULL);
  1070. 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;
  1071. a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a') (select c from t1 where a=t2.a)
  1072. 1 1 a
  1073. 2 0 b
  1074. NULL NULL NULL
  1075. 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;
  1076. a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,3,'b') (select c from t1 where a=t2.a)
  1077. 1 0 a
  1078. 2 1 b
  1079. NULL NULL NULL
  1080. 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;
  1081. a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,4,'c') (select c from t1 where a=t2.a)
  1082. 1 0 a
  1083. 2 0 b
  1084. NULL NULL NULL
  1085. drop table t1,t2;
  1086. create table t1 (a int, b real, c varchar(10));
  1087. insert into t1 values (1, 1, 'a'), (2,2,'b'), (NULL, 2, 'b');
  1088. select ROW(1, 1, 'a') IN (select a,b,c from t1);
  1089. ROW(1, 1, 'a') IN (select a,b,c from t1)
  1090. 1
  1091. select ROW(1, 2, 'a') IN (select a,b,c from t1);
  1092. ROW(1, 2, 'a') IN (select a,b,c from t1)
  1093. 0
  1094. select ROW(1, 1, 'a') IN (select b,a,c from t1);
  1095. ROW(1, 1, 'a') IN (select b,a,c from t1)
  1096. 1
  1097. select ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null);
  1098. ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null)
  1099. 1
  1100. select ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null);
  1101. ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null)
  1102. 0
  1103. select ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null);
  1104. ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null)
  1105. 1
  1106. select ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a');
  1107. ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a')
  1108. 1
  1109. select ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a');
  1110. ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a')
  1111. 0
  1112. select ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a');
  1113. ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a')
  1114. 1
  1115. select ROW(1, 1, 'a') IN (select b,a,c from t1 limit 2);
  1116. ERROR 42000: This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
  1117. drop table t1;
  1118. create table t1 (a int);
  1119. insert into t1 values (1);
  1120. do @a:=(SELECT a from t1);
  1121. select @a;
  1122. @a
  1123. 1
  1124. set @a:=2;
  1125. set @a:=(SELECT a from t1);
  1126. select @a;
  1127. @a
  1128. 1
  1129. drop table t1;
  1130. do (SELECT a from t1);
  1131. ERROR 42S02: Table 'test.t1' doesn't exist
  1132. set @a:=(SELECT a from t1);
  1133. ERROR 42S02: Table 'test.t1' doesn't exist
  1134. CREATE TABLE t1 (a int, KEY(a));
  1135. HANDLER t1 OPEN;
  1136. HANDLER t1 READ a=((SELECT 1));
  1137. 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
  1138. HANDLER t1 CLOSE;
  1139. drop table t1;
  1140. create table t1 (a int);
  1141. create table t2 (b int);
  1142. insert into t1 values (1),(2);
  1143. insert into t2 values (1);
  1144. select a from t1 where a in (select a from t1 where a in (select b from t2));
  1145. a
  1146. 1
  1147. drop table t1, t2;
  1148. create table t1 (a int, b int);
  1149. create table t2 like t1;
  1150. insert into t1 values (1,2),(1,3),(1,4),(1,5);
  1151. insert into t2 values (1,2),(1,3);
  1152. select * from t1 where row(a,b) in (select a,b from t2);
  1153. a b
  1154. 1 2
  1155. 1 3
  1156. drop table t1, t2;
  1157. CREATE TABLE `t1` (`i` int(11) NOT NULL default '0',PRIMARY KEY (`i`)) ENGINE=MyISAM CHARSET=latin1;
  1158. INSERT INTO t1 VALUES (1);
  1159. UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));
  1160. select * from t1;
  1161. i
  1162. 2
  1163. drop table t1;
  1164. CREATE TABLE t1 (a int(1));
  1165. EXPLAIN EXTENDED SELECT (SELECT RAND() FROM t1) FROM t1;
  1166. id select_type table type possible_keys key key_len ref rows filtered Extra
  1167. 1 PRIMARY t1 system NULL NULL NULL NULL 0 0.00 const row not found
  1168. 2 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 0 0.00 const row not found
  1169. Warnings:
  1170. Note 1003 select (select rand() from `test`.`t1`) AS `(SELECT RAND() FROM t1)` from `test`.`t1`
  1171. EXPLAIN EXTENDED SELECT (SELECT ENCRYPT('test') FROM t1) FROM t1;
  1172. id select_type table type possible_keys key key_len ref rows filtered Extra
  1173. 1 PRIMARY t1 system NULL NULL NULL NULL 0 0.00 const row not found
  1174. 2 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 0 0.00 const row not found
  1175. Warnings:
  1176. Note 1003 select (select encrypt('test') from `test`.`t1`) AS `(SELECT ENCRYPT('test') FROM t1)` from `test`.`t1`
  1177. EXPLAIN EXTENDED SELECT (SELECT BENCHMARK(1,1) FROM t1) FROM t1;
  1178. id select_type table type possible_keys key key_len ref rows filtered Extra
  1179. 1 PRIMARY t1 system NULL NULL NULL NULL 0 0.00 const row not found
  1180. 2 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 0 0.00 const row not found
  1181. Warnings:
  1182. Note 1003 select (select benchmark(1,1) from `test`.`t1`) AS `(SELECT BENCHMARK(1,1) FROM t1)` from `test`.`t1`
  1183. drop table t1;
  1184. CREATE TABLE `t1` (
  1185. `mot` varchar(30) character set latin1 NOT NULL default '',
  1186. `topic` mediumint(8) unsigned NOT NULL default '0',
  1187. `date` date NOT NULL default '0000-00-00',
  1188. `pseudo` varchar(35) character set latin1 NOT NULL default '',
  1189. PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`),
  1190. KEY `pseudo` (`pseudo`,`date`,`topic`),
  1191. KEY `topic` (`topic`)
  1192. ) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
  1193. CREATE TABLE `t2` (
  1194. `mot` varchar(30) character set latin1 NOT NULL default '',
  1195. `topic` mediumint(8) unsigned NOT NULL default '0',
  1196. `date` date NOT NULL default '0000-00-00',
  1197. `pseudo` varchar(35) character set latin1 NOT NULL default '',
  1198. PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`),
  1199. KEY `pseudo` (`pseudo`,`date`,`topic`),
  1200. KEY `topic` (`topic`)
  1201. ) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
  1202. CREATE TABLE `t3` (
  1203. `numeropost` mediumint(8) unsigned NOT NULL auto_increment,
  1204. `maxnumrep` int(10) unsigned NOT NULL default '0',
  1205. PRIMARY KEY (`numeropost`),
  1206. UNIQUE KEY `maxnumrep` (`maxnumrep`)
  1207. ) ENGINE=MyISAM CHARSET=latin1;
  1208. INSERT INTO t1 VALUES ('joce','1','','joce'),('test','2','','test');
  1209. Warnings:
  1210. Warning 1265 Data truncated for column 'date' at row 1
  1211. Warning 1265 Data truncated for column 'date' at row 2
  1212. INSERT INTO t2 VALUES ('joce','1','','joce'),('test','2','','test');
  1213. Warnings:
  1214. Warning 1265 Data truncated for column 'date' at row 1
  1215. Warning 1265 Data truncated for column 'date' at row 2
  1216. INSERT INTO t3 VALUES (1,1);
  1217. SELECT DISTINCT topic FROM t2 WHERE NOT EXISTS(SELECT * FROM t3 WHERE
  1218. numeropost=topic);
  1219. topic
  1220. 2
  1221. select * from t1;
  1222. mot topic date pseudo
  1223. joce 1 0000-00-00 joce
  1224. test 2 0000-00-00 test
  1225. DELETE FROM t1 WHERE topic IN (SELECT DISTINCT topic FROM t2 WHERE NOT
  1226. EXISTS(SELECT * FROM t3 WHERE numeropost=topic));
  1227. select * from t1;
  1228. mot topic date pseudo
  1229. joce 1 0000-00-00 joce
  1230. drop table t1, t2, t3;
  1231. SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
  1232. a (SELECT a)
  1233. 1 1
  1234. CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT 1)) a;
  1235. SHOW CREATE TABLE t1;
  1236. Table Create Table
  1237. t1 CREATE TABLE `t1` (
  1238. `a` int(1) NOT NULL DEFAULT '0',
  1239. `(SELECT 1)` int(1) NOT NULL DEFAULT '0'
  1240. ) ENGINE=MyISAM DEFAULT CHARSET=latin1
  1241. drop table t1;
  1242. CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
  1243. SHOW CREATE TABLE t1;
  1244. Table Create Table
  1245. t1 CREATE TABLE `t1` (
  1246. `a` int(1) NOT NULL DEFAULT '0',
  1247. `(SELECT a)` int(1) NOT NULL DEFAULT '0'
  1248. ) ENGINE=MyISAM DEFAULT CHARSET=latin1
  1249. drop table t1;
  1250. CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a;
  1251. SHOW CREATE TABLE t1;
  1252. Table Create Table
  1253. t1 CREATE TABLE `t1` (
  1254. `a` int(1) NOT NULL DEFAULT '0',
  1255. `(SELECT a+0)` int(3) NOT NULL DEFAULT '0'
  1256. ) ENGINE=MyISAM DEFAULT CHARSET=latin1
  1257. drop table t1;
  1258. CREATE TABLE t1 SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a;
  1259. select * from t1;
  1260. a
  1261. 2
  1262. SHOW CREATE TABLE t1;
  1263. Table Create Table
  1264. t1 CREATE TABLE `t1` (
  1265. `a` bigint(20) NOT NULL DEFAULT '0'
  1266. ) ENGINE=MyISAM DEFAULT CHARSET=latin1
  1267. drop table t1;
  1268. create table t1 (a int);
  1269. insert into t1 values (1), (2), (3);
  1270. explain extended select a,(select (select rand() from t1 limit 1) from t1 limit 1)
  1271. from t1;
  1272. id select_type table type possible_keys key key_len ref rows filtered Extra
  1273. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
  1274. 2 UNCACHEABLE SUBQUERY t1 ALL NULL NULL NULL NULL 3 100.00
  1275. 3 UNCACHEABLE SUBQUERY t1 ALL NULL NULL NULL NULL 3 100.00
  1276. Warnings:
  1277. 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`
  1278. drop table t1;
  1279. 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);
  1280. ERROR 42S02: Table 'test.t1' doesn't exist
  1281. CREATE TABLE t1 (
  1282. ID int(11) NOT NULL auto_increment,
  1283. name char(35) NOT NULL default '',
  1284. t2 char(3) NOT NULL default '',
  1285. District char(20) NOT NULL default '',
  1286. Population int(11) NOT NULL default '0',
  1287. PRIMARY KEY (ID)
  1288. ) ENGINE=MyISAM;
  1289. INSERT INTO t1 VALUES (130,'Sydney','AUS','New South Wales',3276207);
  1290. INSERT INTO t1 VALUES (131,'Melbourne','AUS','Victoria',2865329);
  1291. INSERT INTO t1 VALUES (132,'Brisbane','AUS','Queensland',1291117);
  1292. CREATE TABLE t2 (
  1293. Code char(3) NOT NULL default '',
  1294. Name char(52) NOT NULL default '',
  1295. Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
  1296. Region char(26) NOT NULL default '',
  1297. SurfaceArea float(10,2) NOT NULL default '0.00',
  1298. IndepYear smallint(6) default NULL,
  1299. Population int(11) NOT NULL default '0',
  1300. LifeExpectancy float(3,1) default NULL,
  1301. GNP float(10,2) default NULL,
  1302. GNPOld float(10,2) default NULL,
  1303. LocalName char(45) NOT NULL default '',
  1304. GovernmentForm char(45) NOT NULL default '',
  1305. HeadOfState char(60) default NULL,
  1306. Capital int(11) default NULL,
  1307. Code2 char(2) NOT NULL default '',
  1308. PRIMARY KEY (Code)
  1309. ) ENGINE=MyISAM;
  1310. 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');
  1311. 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');
  1312. 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);
  1313. Continent Name Population
  1314. Oceania Sydney 3276207
  1315. drop table t1, t2;
  1316. CREATE TABLE `t1` (
  1317. `id` mediumint(8) unsigned NOT NULL auto_increment,
  1318. `pseudo` varchar(35) character set latin1 NOT NULL default '',
  1319. PRIMARY KEY (`id`),
  1320. UNIQUE KEY `pseudo` (`pseudo`)
  1321. ) ENGINE=MyISAM PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
  1322. INSERT INTO t1 (pseudo) VALUES ('test');
  1323. SELECT 0 IN (SELECT 1 FROM t1 a);
  1324. 0 IN (SELECT 1 FROM t1 a)
  1325. 0
  1326. EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
  1327. id select_type table type possible_keys key key_len ref rows filtered Extra
  1328. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  1329. 2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  1330. Warnings:
  1331. Note 1003 select <in_optimizer>(0,<exists>(select 1 from `test`.`t1` `a` where (0 = 1))) AS `0 IN (SELECT 1 FROM t1 a)`
  1332. INSERT INTO t1 (pseudo) VALUES ('test1');
  1333. SELECT 0 IN (SELECT 1 FROM t1 a);
  1334. 0 IN (SELECT 1 FROM t1 a)
  1335. 0
  1336. EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
  1337. id select_type table type possible_keys key key_len ref rows filtered Extra
  1338. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  1339. 2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  1340. Warnings:
  1341. Note 1003 select <in_optimizer>(0,<exists>(select 1 from `test`.`t1` `a` where (0 = 1))) AS `0 IN (SELECT 1 FROM t1 a)`
  1342. drop table t1;
  1343. CREATE TABLE `t1` (
  1344. `i` int(11) NOT NULL default '0',
  1345. PRIMARY KEY (`i`)
  1346. ) ENGINE=MyISAM CHARSET=latin1;
  1347. INSERT INTO t1 VALUES (1);
  1348. UPDATE t1 SET i=i+(SELECT MAX(i) FROM (SELECT 1) t) WHERE i=(SELECT MAX(i));
  1349. UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));
  1350. UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t);
  1351. ERROR 42S22: Unknown column 't.i' in 'field list'
  1352. select * from t1;
  1353. i
  1354. 3
  1355. drop table t1;
  1356. CREATE TABLE t1 (
  1357. id int(11) default NULL
  1358. ) ENGINE=MyISAM CHARSET=latin1;
  1359. INSERT INTO t1 VALUES (1),(1),(2),(2),(1),(3);
  1360. CREATE TABLE t2 (
  1361. id int(11) default NULL,
  1362. name varchar(15) default NULL
  1363. ) ENGINE=MyISAM CHARSET=latin1;
  1364. INSERT INTO t2 VALUES (4,'vita'), (1,'vita'), (2,'vita'), (1,'vita');
  1365. update t1, t2 set t2.name='lenka' where t2.id in (select id from t1);
  1366. select * from t2;
  1367. id name
  1368. 4 vita
  1369. 1 lenka
  1370. 2 lenka
  1371. 1 lenka
  1372. drop table t1,t2;
  1373. create table t1 (a int, unique index indexa (a));
  1374. insert into t1 values (-1), (-4), (-2), (NULL);
  1375. select -10 IN (select a from t1 FORCE INDEX (indexa));
  1376. -10 IN (select a from t1 FORCE INDEX (indexa))
  1377. NULL
  1378. drop table t1;
  1379. create table t1 (id int not null auto_increment primary key, salary int, key(salary));
  1380. insert into t1 (salary) values (100),(1000),(10000),(10),(500),(5000),(50000);
  1381. explain extended SELECT id FROM t1 where salary = (SELECT MAX(salary) FROM t1);
  1382. id select_type table type possible_keys key key_len ref rows filtered Extra
  1383. 1 PRIMARY t1 ref salary salary 5 const 0 0.00 Using where
  1384. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
  1385. Warnings:
  1386. Note 1003 select `test`.`t1`.`id` AS `id` from `test`.`t1` where (`test`.`t1`.`salary` = (select max(`test`.`t1`.`salary`) from `test`.`t1`))
  1387. drop table t1;
  1388. CREATE TABLE t1 (
  1389. ID int(10) unsigned NOT NULL auto_increment,
  1390. SUB_ID int(3) unsigned NOT NULL default '0',
  1391. REF_ID int(10) unsigned default NULL,
  1392. REF_SUB int(3) unsigned default '0',
  1393. PRIMARY KEY (ID,SUB_ID),
  1394. UNIQUE KEY t1_PK (ID,SUB_ID),
  1395. KEY t1_FK (REF_ID,REF_SUB),
  1396. KEY t1_REFID (REF_ID)
  1397. ) ENGINE=MyISAM CHARSET=cp1251;
  1398. INSERT INTO t1 VALUES (1,0,NULL,NULL),(2,0,NULL,NULL);
  1399. SELECT DISTINCT REF_ID FROM t1 WHERE ID= (SELECT DISTINCT REF_ID FROM t1 WHERE ID=2);
  1400. REF_ID
  1401. DROP TABLE t1;
  1402. create table t1 (a int, b int);
  1403. create table t2 (a int, b int);
  1404. insert into t1 values (1,0), (2,0), (3,0);
  1405. insert into t2 values (1,1), (2,1), (3,1), (2,2);
  1406. update ignore t1 set b=(select b from t2 where t1.a=t2.a);
  1407. Warnings:
  1408. Warning 1242 Subquery returns more than 1 row
  1409. select * from t1;
  1410. a b
  1411. 1 1
  1412. 2 NULL
  1413. 3 1
  1414. drop table t1, t2;
  1415. CREATE TABLE `t1` (
  1416. `id` mediumint(8) unsigned NOT NULL auto_increment,
  1417. `pseudo` varchar(35) NOT NULL default '',
  1418. `email` varchar(60) NOT NULL default '',
  1419. PRIMARY KEY (`id`),
  1420. UNIQUE KEY `email` (`email`),
  1421. UNIQUE KEY `pseudo` (`pseudo`)
  1422. ) ENGINE=MyISAM CHARSET=latin1 PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
  1423. INSERT INTO t1 (id,pseudo,email) VALUES (1,'test','test'),(2,'test1','test1');
  1424. SELECT pseudo as a, pseudo as b FROM t1 GROUP BY (SELECT a) ORDER BY (SELECT id*1);
  1425. a b
  1426. test test
  1427. test1 test1
  1428. drop table if exists t1;
  1429. (SELECT 1 as a) UNION (SELECT 1) ORDER BY (SELECT a+0);
  1430. a
  1431. 1
  1432. create table t1 (a int not null, b int, primary key (a));
  1433. create table t2 (a int not null, primary key (a));
  1434. create table t3 (a int not null, b int, primary key (a));
  1435. insert into t1 values (1,10), (2,20), (3,30), (4,40);
  1436. insert into t2 values (2), (3), (4), (5);
  1437. insert into t3 values (10,3), (20,4), (30,5);
  1438. select * from t2 where t2.a in (select a from t1);
  1439. a
  1440. 2
  1441. 3
  1442. 4
  1443. explain extended select * from t2 where t2.a in (select a from t1);
  1444. id select_type table type possible_keys key key_len ref rows filtered Extra
  1445. 1 PRIMARY t2 index PRIMARY PRIMARY 4 NULL 4 100.00 Using index
  1446. 1 PRIMARY t1 index PRIMARY PRIMARY 4 NULL 4 75.00 Using where; Using index; Using join buffer (flat, BNL join)
  1447. Warnings:
  1448. Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t1` join `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)
  1449. select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
  1450. a
  1451. 2
  1452. 4
  1453. explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
  1454. id select_type table type possible_keys key key_len ref rows filtered Extra
  1455. 1 PRIMARY t2 index PRIMARY PRIMARY 4 NULL 4 100.00 Using index
  1456. 1 PRIMARY t1 ALL PRIMARY NULL NULL NULL 4 75.00 Using where; Using join buffer (flat, BNL join)
  1457. Warnings:
  1458. 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))
  1459. select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
  1460. a
  1461. 2
  1462. 3
  1463. explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
  1464. id select_type table type possible_keys key key_len ref rows filtered Extra
  1465. 1 PRIMARY t2 index PRIMARY PRIMARY 4 NULL 4 100.00 Using index
  1466. 1 PRIMARY t1 ALL PRIMARY NULL NULL NULL 4 75.00 Using where; Using join buffer (flat, BNL join)
  1467. 1 PRIMARY t3 eq_ref PRIMARY PRIMARY 4 test.t1.b 1 100.00 Using index
  1468. Warnings:
  1469. 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`))
  1470. drop table t1, t2, t3;
  1471. create table t1 (a int, b int, index a (a,b));
  1472. create table t2 (a int, index a (a));
  1473. create table t3 (a int, b int, index a (a));
  1474. insert into t1 values (1,10), (2,20), (3,30), (4,40);
  1475. create table t0(a int);
  1476. insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
  1477. insert into t1
  1478. select rand()*100000+200,rand()*100000 from t0 A, t0 B, t0 C, t0 D;
  1479. insert into t2 values (2), (3), (4), (5);
  1480. insert into t3 values (10,3), (20,4), (30,5);
  1481. select * from t2 where t2.a in (select a from t1);
  1482. a
  1483. 2
  1484. 3
  1485. 4
  1486. explain extended select * from t2 where t2.a in (select a from t1);
  1487. id select_type table type possible_keys key key_len ref rows filtered Extra
  1488. 1 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index
  1489. 1 PRIMARY t1 ref a a 5 test.t2.a 101 100.00 Using index; FirstMatch(t2)
  1490. Warnings:
  1491. Note 1003 select `test`.`t2`.`a` AS `a` from `test`.`t2` semi join (`test`.`t1`) where (`test`.`t1`.`a` = `test`.`t2`.`a`)
  1492. select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
  1493. a
  1494. 2
  1495. 4
  1496. explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
  1497. id select_type table type possible_keys key key_len ref rows filtered Extra
  1498. 1 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index
  1499. 1 PRIMARY t1 ref a a 5 test.t2.a 101 100.00 Using where; Using index; FirstMatch(t2)
  1500. Warnings:
  1501. 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))
  1502. select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
  1503. a
  1504. 2
  1505. 3
  1506. explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
  1507. id select_type table type possible_keys key key_len ref rows filtered Extra
  1508. 1 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index
  1509. 1 PRIMARY t3 index a a 5 NULL 3 100.00 Using where; Using index
  1510. 1 PRIMARY t1 ref a a 10 test.t2.a,test.t3.a 116 100.00 Using index; FirstMatch(t2)
  1511. Warnings:
  1512. 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`.`t1`.`b` = `test`.`t3`.`a`))
  1513. insert into t1 values (3,31);
  1514. select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
  1515. a
  1516. 2
  1517. 3
  1518. 4
  1519. select * from t2 where t2.a in (select a from t1 where t1.b <> 30 and t1.b <> 31);
  1520. a
  1521. 2
  1522. 4
  1523. explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
  1524. id select_type table type possible_keys key key_len ref rows filtered Extra
  1525. 1 PRIMARY t2 index a a 5 NULL 4 100.00 Using where; Using index
  1526. 1 PRIMARY t1 ref a a 5 test.t2.a 101 100.00 Using where; Using index; FirstMatch(t2)
  1527. Warnings:
  1528. 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))
  1529. drop table t0, t1, t2, t3;
  1530. create table t1 (a int, b int);
  1531. create table t2 (a int, b int);
  1532. create table t3 (a int, b int);
  1533. insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10);
  1534. insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1);
  1535. insert into t3 values (3,3), (2,2), (1,1);
  1536. 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;
  1537. 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)
  1538. 3 1
  1539. 2 2
  1540. 1 2
  1541. drop table t1,t2,t3;
  1542. create table t1 (s1 int);
  1543. create table t2 (s1 int);
  1544. insert into t1 values (1);
  1545. insert into t2 values (1);
  1546. select * from t1 where exists (select s1 from t2 having max(t2.s1)=t1.s1);
  1547. s1
  1548. 1
  1549. drop table t1,t2;
  1550. create table t1 (s1 int);
  1551. create table t2 (s1 int);
  1552. insert into t1 values (1);
  1553. insert into t2 values (1);
  1554. update t1 set s1 = s1 + 1 where 1 = (select x.s1 as A from t2 WHERE t2.s1 > t1.s1 order by A);
  1555. ERROR 42S22: Unknown column 'x.s1' in 'field list'
  1556. DROP TABLE t1, t2;
  1557. CREATE TABLE t1 (s1 CHAR(5) COLLATE latin1_german1_ci,
  1558. s2 CHAR(5) COLLATE latin1_swedish_ci);
  1559. INSERT INTO t1 VALUES ('z','?');
  1560. select * from t1 where s1 > (select max(s2) from t1);
  1561. ERROR HY000: Illegal mix of collations (latin1_german1_ci,IMPLICIT) and (latin1_swedish_ci,IMPLICIT) for operation '>'
  1562. select * from t1 where s1 > any (select max(s2) from t1);
  1563. ERROR HY000: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (latin1_german1_ci,IMPLICIT) for operation '<'
  1564. drop table t1;
  1565. create table t1(toid int,rd int);
  1566. create table t2(userid int,pmnew int,pmtotal int);
  1567. insert into t2 values(1,0,0),(2,0,0);
  1568. 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);
  1569. 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);
  1570. userid pmtotal pmnew calc_total calc_new
  1571. 1 0 0 9 3
  1572. 2 0 0 4 2
  1573. drop table t1, t2;
  1574. create table t1 (s1 char(5));
  1575. select (select 'a','b' from t1 union select 'a','b' from t1) from t1;
  1576. ERROR 21000: Operand should contain 1 column(s)
  1577. insert into t1 values ('tttt');
  1578. select * from t1 where ('a','b')=(select 'a','b' from t1 union select 'a','b' from t1);
  1579. s1
  1580. tttt
  1581. explain extended (select * from t1);
  1582. id select_type table type possible_keys key key_len ref rows filtered Extra
  1583. 1 SIMPLE t1 system NULL NULL NULL NULL 1 100.00
  1584. Warnings:
  1585. Note 1003 (select 'tttt' AS `s1` from `test`.`t1`)
  1586. (select * from t1);
  1587. s1
  1588. tttt
  1589. drop table t1;
  1590. create table t1 (s1 char(5), index s1(s1));
  1591. create table t2 (s1 char(5), index s1(s1));
  1592. insert into t1 values ('a1'),('a2'),('a3');
  1593. insert into t2 values ('a1'),('a2');
  1594. select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
  1595. s1 s1 NOT IN (SELECT s1 FROM t2)
  1596. a1 0
  1597. a2 0
  1598. a3 1
  1599. select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
  1600. s1 s1 = ANY (SELECT s1 FROM t2)
  1601. a1 1
  1602. a2 1
  1603. a3 0
  1604. select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
  1605. s1 s1 <> ALL (SELECT s1 FROM t2)
  1606. a1 0
  1607. a2 0
  1608. a3 1
  1609. select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
  1610. s1 s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2')
  1611. a1 0
  1612. a2 1
  1613. a3 1
  1614. explain extended select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
  1615. id select_type table type possible_keys key key_len ref rows filtered Extra
  1616. 1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index
  1617. 2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Full scan on NULL key
  1618. Warnings:
  1619. Note 1003 select `test`.`t1`.`s1` AS `s1`,(not(<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`
  1620. explain extended select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
  1621. id select_type table type possible_keys key key_len ref rows filtered Extra
  1622. 1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index
  1623. 2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Full scan on NULL key
  1624. Warnings:
  1625. Note 1003 select `test`.`t1`.`s1` AS `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`
  1626. explain extended select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
  1627. id select_type table type possible_keys key key_len ref rows filtered Extra
  1628. 1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index
  1629. 2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Full scan on NULL key
  1630. Warnings:
  1631. Note 1003 select `test`.`t1`.`s1` AS `s1`,(not(<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`
  1632. explain extended select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
  1633. id select_type table type possible_keys key key_len ref rows filtered Extra
  1634. 1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index
  1635. 2 DEPENDENT SUBQUERY t2 index_subquery s1 s1 6 func 2 100.00 Using index; Using where; Full scan on NULL key
  1636. Warnings:
  1637. Note 1003 select `test`.`t1`.`s1` AS `s1`,(not(<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`
  1638. drop table t1,t2;
  1639. create table t2 (a int, b int not null);
  1640. create table t3 (a int);
  1641. insert into t3 values (6),(7),(3);
  1642. select * from t3 where a >= all (select b from t2);
  1643. a
  1644. 6
  1645. 7
  1646. 3
  1647. explain extended select * from t3 where a >= all (select b from t2);
  1648. id select_type table type possible_keys key key_len ref rows filtered Extra
  1649. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
  1650. 2 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 const row not found
  1651. Warnings:
  1652. Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <not>(<in_optimizer>(`test`.`t3`.`a`,((select max(NULL) from `test`.`t2`) > <cache>(`test`.`t3`.`a`))))
  1653. select * from t3 where a >= some (select b from t2);
  1654. a
  1655. explain extended select * from t3 where a >= some (select b from t2);
  1656. id select_type table type possible_keys key key_len ref rows filtered Extra
  1657. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
  1658. 2 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 const row not found
  1659. Warnings:
  1660. Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(`test`.`t3`.`a`,((select min(NULL) from `test`.`t2`) <= <cache>(`test`.`t3`.`a`))))
  1661. select * from t3 where a >= all (select b from t2 group by 1);
  1662. a
  1663. 6
  1664. 7
  1665. 3
  1666. explain extended select * from t3 where a >= all (select b from t2 group by 1);
  1667. id select_type table type possible_keys key key_len ref rows filtered Extra
  1668. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
  1669. 2 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 const row not found
  1670. Warnings:
  1671. Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <not>(<in_optimizer>(`test`.`t3`.`a`,((select max(NULL) from `test`.`t2`) > <cache>(`test`.`t3`.`a`))))
  1672. select * from t3 where a >= some (select b from t2 group by 1);
  1673. a
  1674. explain extended select * from t3 where a >= some (select b from t2 group by 1);
  1675. id select_type table type possible_keys key key_len ref rows filtered Extra
  1676. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
  1677. 2 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 const row not found
  1678. Warnings:
  1679. Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(`test`.`t3`.`a`,((select min(NULL) from `test`.`t2`) <= <cache>(`test`.`t3`.`a`))))
  1680. select * from t3 where NULL >= any (select b from t2);
  1681. a
  1682. explain extended select * from t3 where NULL >= any (select b from t2);
  1683. id select_type table type possible_keys key key_len ref rows filtered Extra
  1684. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00
  1685. 2 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 const row not found
  1686. Warnings:
  1687. Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(NULL,((select min(NULL) from `test`.`t2`) <= NULL)))
  1688. select * from t3 where NULL >= any (select b from t2 group by 1);
  1689. a
  1690. explain extended select * from t3 where NULL >= any (select b from t2 group by 1);
  1691. id select_type table type possible_keys key key_len ref rows filtered Extra
  1692. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00
  1693. 2 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 const row not found
  1694. Warnings:
  1695. Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(NULL,((select min(NULL) from `test`.`t2`) <= NULL)))
  1696. select * from t3 where NULL >= some (select b from t2);
  1697. a
  1698. explain extended select * from t3 where NULL >= some (select b from t2);
  1699. id select_type table type possible_keys key key_len ref rows filtered Extra
  1700. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00
  1701. 2 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 const row not found
  1702. Warnings:
  1703. Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(NULL,((select min(NULL) from `test`.`t2`) <= NULL)))
  1704. select * from t3 where NULL >= some (select b from t2 group by 1);
  1705. a
  1706. explain extended select * from t3 where NULL >= some (select b from t2 group by 1);
  1707. id select_type table type possible_keys key key_len ref rows filtered Extra
  1708. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00
  1709. 2 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 const row not found
  1710. Warnings:
  1711. Note 1003 select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(NULL,((select min(NULL) from `test`.`t2`) <= NULL)))
  1712. insert into t2 values (2,2), (2,1), (3,3), (3,1);
  1713. select * from t3 where a > all (select max(b) from t2 group by a);
  1714. a
  1715. 6
  1716. 7
  1717. explain extended select * from t3 where a > all (select max(b) from t2 group by a);
  1718. id select_type table type possible_keys key key_len ref rows filtered Extra
  1719. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
  1720. 2 SUBQUERY t2 ALL NULL NULL NULL NULL 4 100.00 Using temporary
  1721. Warnings:
  1722. 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`))))
  1723. drop table t2, t3;
  1724. 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 ;
  1725. 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());
  1726. 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;
  1727. 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);
  1728. 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 ;
  1729. 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);
  1730. CREATE TABLE `t4` (`task_id` smallint(6) NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
  1731. INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status');
  1732. 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;
  1733. 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')
  1734. -1 Valid 1
  1735. -1 Valid 2 1
  1736. -1 Should Not Return 0
  1737. 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;
  1738. dbid name
  1739. -1 Valid
  1740. -1 Valid 2
  1741. drop table t1,t2,t3,t4;
  1742. CREATE TABLE t1 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1;
  1743. INSERT INTO t1 VALUES (1),(5);
  1744. CREATE TABLE t2 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1;
  1745. INSERT INTO t2 VALUES (2),(6);
  1746. select * from t1 where (1,2,6) in (select * from t2);
  1747. ERROR 21000: Operand should contain 3 column(s)
  1748. DROP TABLE t1,t2;
  1749. create table t1 (s1 int);
  1750. insert into t1 values (1);
  1751. insert into t1 values (2);
  1752. set sort_buffer_size = (select s1 from t1);
  1753. ERROR 21000: Subquery returns more than 1 row
  1754. do (select * from t1);
  1755. Warnings:
  1756. Error 1242 Subquery returns more than 1 row
  1757. drop table t1;
  1758. create table t1 (s1 char);
  1759. insert into t1 values ('e');
  1760. select * from t1 where 'f' > any (select s1 from t1);
  1761. s1
  1762. e
  1763. select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
  1764. s1
  1765. e
  1766. explain extended select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
  1767. id select_type table type possible_keys key key_len ref rows filtered Extra
  1768. 1 PRIMARY t1 system NULL NULL NULL NULL 1 100.00
  1769. 2 SUBQUERY t1 system NULL NULL NULL NULL 1 100.00
  1770. 3 UNION t1 system NULL NULL NULL NULL 1 100.00
  1771. NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
  1772. Warnings:
  1773. Note 1003 select 'e' AS `s1` from `test`.`t1` where <nop>(<in_optimizer>('f',(<min>(select 'e' from `test`.`t1` union select 'e' from `test`.`t1`) < 'f')))
  1774. drop table t1;
  1775. CREATE TABLE t1 (number char(11) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
  1776. INSERT INTO t1 VALUES ('69294728265'),('18621828126'),('89356874041'),('95895001874');
  1777. CREATE TABLE t2 (code char(5) NOT NULL default '',UNIQUE KEY code (code)) ENGINE=MyISAM CHARSET=latin1;
  1778. INSERT INTO t2 VALUES ('1'),('1226'),('1245'),('1862'),('18623'),('1874'),('1967'),('6');
  1779. 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;
  1780. phone code
  1781. 69294728265 6
  1782. 18621828126 1862
  1783. 89356874041 NULL
  1784. 95895001874 NULL
  1785. drop table t1, t2;
  1786. create table t1 (s1 int);
  1787. create table t2 (s1 int);
  1788. select * from t1 where (select count(*) from t2 where t1.s2) = 1;
  1789. ERROR 42S22: Unknown column 't1.s2' in 'where clause'
  1790. select * from t1 where (select count(*) from t2 group by t1.s2) = 1;
  1791. ERROR 42S22: Unknown column 't1.s2' in 'group statement'
  1792. select count(*) from t2 group by t1.s2;
  1793. ERROR 42S22: Unknown column 't1.s2' in 'group statement'
  1794. drop table t1, t2;
  1795. CREATE TABLE t1(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC VARCHAR(20) DEFAULT NULL,PRIMARY KEY (COLA, COLB));
  1796. CREATE TABLE t2(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC CHAR(1) NOT NULL,PRIMARY KEY (COLA));
  1797. INSERT INTO t1 VALUES (1,1,'1A3240'), (1,2,'4W2365');
  1798. INSERT INTO t2 VALUES (100, 200, 'C');
  1799. SELECT DISTINCT COLC FROM t1 WHERE COLA = (SELECT COLA FROM t2 WHERE COLB = 200 AND COLC ='C' LIMIT 1);
  1800. COLC
  1801. DROP TABLE t1, t2;
  1802. CREATE TABLE t1 (a int(1));
  1803. INSERT INTO t1 VALUES (1),(1),(1),(1),(1),(2),(3),(4),(5);
  1804. SELECT DISTINCT (SELECT a) FROM t1 LIMIT 100;
  1805. (SELECT a)
  1806. 1
  1807. 2
  1808. 3
  1809. 4
  1810. 5
  1811. DROP TABLE t1;
  1812. create table t1 (a int, b decimal(13, 3));
  1813. insert into t1 values (1, 0.123);
  1814. select a, (select max(b) from t1) into outfile "../../tmp/subselect.out.file.1" from t1;
  1815. delete from t1;
  1816. load data infile "../../tmp/subselect.out.file.1" into table t1;
  1817. select * from t1;
  1818. a b
  1819. 1 0.123
  1820. drop table t1;
  1821. CREATE TABLE `t1` (
  1822. `id` int(11) NOT NULL auto_increment,
  1823. `id_cns` tinyint(3) unsigned NOT NULL default '0',
  1824. `tipo` enum('','UNO','DUE') NOT NULL default '',
  1825. `anno_dep` smallint(4) unsigned zerofill NOT NULL default '0000',
  1826. `particolare` mediumint(8) unsigned NOT NULL default '0',
  1827. `generale` mediumint(8) unsigned NOT NULL default '0',
  1828. `bis` tinyint(3) unsigned NOT NULL default '0',
  1829. PRIMARY KEY (`id`),
  1830. UNIQUE KEY `idx_cns_gen_anno` (`anno_dep`,`id_cns`,`generale`,`particolare`),
  1831. UNIQUE KEY `idx_cns_par_anno` (`id_cns`,`anno_dep`,`tipo`,`particolare`,`bis`)
  1832. );
  1833. 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);
  1834. CREATE TABLE `t2` (
  1835. `id` tinyint(3) unsigned NOT NULL auto_increment,
  1836. `max_anno_dep` smallint(6) unsigned NOT NULL default '0',
  1837. PRIMARY KEY (`id`)
  1838. );
  1839. INSERT INTO `t2` VALUES (16,1987),(50,1990),(51,1990);
  1840. 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;
  1841. id max_anno_dep PIPPO
  1842. 16 1987 1
  1843. 50 1990 0
  1844. 51 1990 NULL
  1845. DROP TABLE t1, t2;
  1846. create table t1 (a int);
  1847. insert into t1 values (1), (2), (3);
  1848. SET SQL_SELECT_LIMIT=1;
  1849. select sum(a) from (select * from t1) as a;
  1850. sum(a)
  1851. 6
  1852. select 2 in (select * from t1);
  1853. 2 in (select * from t1)
  1854. 1
  1855. SET SQL_SELECT_LIMIT=default;
  1856. drop table t1;
  1857. CREATE TABLE t1 (a int, b int, INDEX (a));
  1858. INSERT INTO t1 VALUES (1, 1), (1, 2), (1, 3);
  1859. SELECT * FROM t1 WHERE a = (SELECT MAX(a) FROM t1 WHERE a = 1) ORDER BY b;
  1860. a b
  1861. 1 1
  1862. 1 2
  1863. 1 3
  1864. DROP TABLE t1;
  1865. create table t1(val varchar(10));
  1866. insert into t1 values ('aaa'), ('bbb'),('eee'),('mmm'),('ppp');
  1867. 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%');
  1868. count(*)
  1869. 0
  1870. drop table t1;
  1871. create table t1 (id int not null, text varchar(20) not null default '', primary key (id));
  1872. 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');
  1873. select * from t1 where id not in (select id from t1 where id < 8);
  1874. id text
  1875. 8 text8
  1876. 9 text9
  1877. 10 text10
  1878. 11 text11
  1879. 12 text12
  1880. 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);
  1881. id text
  1882. 8 text8
  1883. 9 text9
  1884. 10 text10
  1885. 11 text11
  1886. 12 text12
  1887. explain extended select * from t1 where id not in (select id from t1 where id < 8);
  1888. id select_type table type possible_keys key key_len ref rows filtered Extra
  1889. 1 PRIMARY t1 ALL NULL NULL NULL NULL 12 100.00 Using where
  1890. 2 DEPENDENT SUBQUERY t1 unique_subquery PRIMARY PRIMARY 4 func 1 100.00 Using index; Using where
  1891. Warnings:
  1892. Note 1003 select `test`.`t1`.`id` AS `id`,`test`.`t1`.`text` AS `text` from `test`.`t1` where (not(<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`)))))))
  1893. 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);
  1894. id select_type table type possible_keys key key_len ref rows filtered Extra
  1895. 1 PRIMARY tt ALL NULL NULL NULL NULL 12 100.00 Using where
  1896. 2 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.tt.id 1 100.00 Using where; Using index
  1897. Warnings:
  1898. Note 1276 Field or reference 'test.tt.id' of SELECT #2 was resolved in SELECT #1
  1899. Note 1003 select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where (not(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))))
  1900. insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001');
  1901. create table t2 (id int not null, text varchar(20) not null default '', primary key (id));
  1902. 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');
  1903. 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);
  1904. id text id text id text
  1905. 1 text1 1 text1 1 text1
  1906. 2 text2 2 text2 2 text2
  1907. 3 text3 3 text3 3 text3
  1908. 4 text4 4 text4 4 text4
  1909. 5 text5 5 text5 5 text5
  1910. 6 text6 6 text6 6 text6
  1911. 7 text7 7 text7 7 text7
  1912. 8 text8 8 text8 8 text8
  1913. 9 text9 9 text9 9 text9
  1914. 10 text10 10 text10 10 text10
  1915. 11 text11 11 text1 11 text11
  1916. 12 text12 12 text2 12 text12
  1917. 1000 text1000 NULL NULL 1000 text1000
  1918. 1001 text1001 NULL NULL 1000 text1000
  1919. 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);
  1920. id select_type table type possible_keys key key_len ref rows filtered Extra
  1921. 1 SIMPLE a ALL NULL NULL NULL NULL 14 100.00
  1922. 1 SIMPLE b eq_ref PRIMARY PRIMARY 4 test.a.id 2 100.00
  1923. 1 SIMPLE c eq_ref PRIMARY PRIMARY 4 func 1 100.00 Using index condition
  1924. Warnings:
  1925. 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`)
  1926. drop table t1,t2;
  1927. create table t1 (a int);
  1928. insert into t1 values (1);
  1929. explain select benchmark(1000, (select a from t1 where a=sha(rand())));
  1930. id select_type table type possible_keys key key_len ref rows Extra
  1931. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  1932. 2 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 1
  1933. drop table t1;
  1934. create table t1(id int);
  1935. create table t2(id int);
  1936. create table t3(flag int);
  1937. select (select * from t3 where id not null) from t1, t2;
  1938. 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
  1939. drop table t1,t2,t3;
  1940. CREATE TABLE t1 (id INT);
  1941. CREATE TABLE t2 (id INT);
  1942. INSERT INTO t1 VALUES (1), (2);
  1943. INSERT INTO t2 VALUES (1);
  1944. 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);
  1945. id c
  1946. 1 1
  1947. 2 0
  1948. SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);
  1949. id c
  1950. 1 1
  1951. 2 0
  1952. 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;
  1953. id c
  1954. 1 1
  1955. 2 0
  1956. 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;
  1957. id c
  1958. 1 1
  1959. 2 0
  1960. DROP TABLE t1,t2;
  1961. CREATE TABLE t1 ( a int, b int );
  1962. INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
  1963. SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
  1964. a
  1965. 3
  1966. SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
  1967. a
  1968. 1
  1969. SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
  1970. a
  1971. 2
  1972. SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
  1973. a
  1974. 2
  1975. 3
  1976. SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
  1977. a
  1978. 1
  1979. 2
  1980. SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
  1981. a
  1982. 1
  1983. 3
  1984. SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
  1985. a
  1986. 3
  1987. SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
  1988. a
  1989. 1
  1990. SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
  1991. a
  1992. 2
  1993. SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
  1994. a
  1995. 2
  1996. 3
  1997. SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
  1998. a
  1999. 1
  2000. 2
  2001. SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
  2002. a
  2003. 1
  2004. 3
  2005. ALTER TABLE t1 ADD INDEX (a);
  2006. SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
  2007. a
  2008. 3
  2009. SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
  2010. a
  2011. 1
  2012. SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
  2013. a
  2014. 2
  2015. SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
  2016. a
  2017. 2
  2018. 3
  2019. SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
  2020. a
  2021. 1
  2022. 2
  2023. SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
  2024. a
  2025. 1
  2026. 3
  2027. SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
  2028. a
  2029. 3
  2030. SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
  2031. a
  2032. 1
  2033. SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
  2034. a
  2035. 2
  2036. SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
  2037. a
  2038. 2
  2039. 3
  2040. SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
  2041. a
  2042. 1
  2043. 2
  2044. SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
  2045. a
  2046. 1
  2047. 3
  2048. SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2);
  2049. a
  2050. 3
  2051. SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2);
  2052. a
  2053. 1
  2054. SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2);
  2055. a
  2056. 2
  2057. SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2);
  2058. a
  2059. 2
  2060. 3
  2061. SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2);
  2062. a
  2063. 1
  2064. 2
  2065. SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2);
  2066. a
  2067. 1
  2068. 3
  2069. SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2);
  2070. a
  2071. 3
  2072. SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2);
  2073. a
  2074. 1
  2075. SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2);
  2076. a
  2077. 2
  2078. SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2);
  2079. a
  2080. 2
  2081. 3
  2082. SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2);
  2083. a
  2084. 1
  2085. 2
  2086. SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2);
  2087. a
  2088. 1
  2089. 3
  2090. SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2091. a
  2092. 3
  2093. SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2094. a
  2095. 1
  2096. SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2097. a
  2098. 2
  2099. SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2100. a
  2101. 2
  2102. 3
  2103. SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2104. a
  2105. 1
  2106. 2
  2107. SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2108. a
  2109. 1
  2110. 3
  2111. SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2112. a
  2113. 3
  2114. SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2115. a
  2116. 1
  2117. SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2118. a
  2119. 2
  2120. SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2121. a
  2122. 2
  2123. 3
  2124. SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2125. a
  2126. 1
  2127. 2
  2128. SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2129. a
  2130. 1
  2131. 3
  2132. SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2133. a
  2134. 3
  2135. SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2136. a
  2137. 1
  2138. SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2139. a
  2140. 2
  2141. SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2142. a
  2143. 2
  2144. 3
  2145. SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2146. a
  2147. 1
  2148. 2
  2149. SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2150. a
  2151. 1
  2152. 3
  2153. SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2154. a
  2155. 3
  2156. SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2157. a
  2158. 1
  2159. SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2160. a
  2161. 2
  2162. SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2163. a
  2164. 2
  2165. 3
  2166. SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2167. a
  2168. 1
  2169. 2
  2170. SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2171. a
  2172. 1
  2173. 3
  2174. SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a FROM t1 WHERE b = 2);
  2175. ERROR 21000: Operand should contain 1 column(s)
  2176. SELECT a FROM t1 WHERE a > ANY (SELECT a,2 FROM t1 WHERE b = 2);
  2177. ERROR 21000: Operand should contain 1 column(s)
  2178. SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a,2 FROM t1 WHERE b = 2);
  2179. ERROR 21000: Operand should contain 1 column(s)
  2180. SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a FROM t1 WHERE b = 2);
  2181. ERROR 21000: Operand should contain 1 column(s)
  2182. SELECT a FROM t1 WHERE a > ALL (SELECT a,2 FROM t1 WHERE b = 2);
  2183. ERROR 21000: Operand should contain 1 column(s)
  2184. SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a,2 FROM t1 WHERE b = 2);
  2185. ERROR 21000: Operand should contain 1 column(s)
  2186. SELECT a FROM t1 WHERE (1,2) = ALL (SELECT a,2 FROM t1 WHERE b = 2);
  2187. ERROR 21000: Operand should contain 1 column(s)
  2188. SELECT a FROM t1 WHERE (1,2) <> ANY (SELECT a,2 FROM t1 WHERE b = 2);
  2189. ERROR 21000: Operand should contain 1 column(s)
  2190. SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a FROM t1 WHERE b = 2);
  2191. ERROR 21000: Operand should contain 2 column(s)
  2192. SELECT a FROM t1 WHERE a = ANY (SELECT a,2 FROM t1 WHERE b = 2);
  2193. ERROR 21000: Operand should contain 1 column(s)
  2194. SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a,2 FROM t1 WHERE b = 2);
  2195. a
  2196. SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a FROM t1 WHERE b = 2);
  2197. ERROR 21000: Operand should contain 2 column(s)
  2198. SELECT a FROM t1 WHERE a <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
  2199. ERROR 21000: Operand should contain 1 column(s)
  2200. SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
  2201. a
  2202. 1
  2203. 2
  2204. 3
  2205. SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2);
  2206. a
  2207. 2
  2208. SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2);
  2209. a
  2210. 1
  2211. 3
  2212. SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2);
  2213. a
  2214. 2
  2215. SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2);
  2216. a
  2217. 1
  2218. 3
  2219. 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);
  2220. a
  2221. 2
  2222. 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);
  2223. a
  2224. 1
  2225. 3
  2226. 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);
  2227. a
  2228. 2
  2229. 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);
  2230. a
  2231. 1
  2232. 3
  2233. SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 group by a);
  2234. a
  2235. 3
  2236. SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 group by a);
  2237. a
  2238. 1
  2239. SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 group by a);
  2240. a
  2241. 2
  2242. SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 group by a);
  2243. a
  2244. 2
  2245. 3
  2246. SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 group by a);
  2247. a
  2248. 1
  2249. 2
  2250. SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 group by a);
  2251. a
  2252. 1
  2253. 3
  2254. SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 group by a);
  2255. a
  2256. 3
  2257. SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 group by a);
  2258. a
  2259. 1
  2260. SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 group by a);
  2261. a
  2262. 2
  2263. SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 group by a);
  2264. a
  2265. 2
  2266. 3
  2267. SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 group by a);
  2268. a
  2269. 1
  2270. 2
  2271. SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 group by a);
  2272. a
  2273. 1
  2274. 3
  2275. SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 group by a HAVING a = 2);
  2276. a
  2277. 3
  2278. SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 group by a HAVING a = 2);
  2279. a
  2280. 1
  2281. SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 group by a HAVING a = 2);
  2282. a
  2283. 2
  2284. SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 group by a HAVING a = 2);
  2285. a
  2286. 2
  2287. 3
  2288. SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 group by a HAVING a = 2);
  2289. a
  2290. 1
  2291. 2
  2292. SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 group by a HAVING a = 2);
  2293. a
  2294. 1
  2295. 3
  2296. SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 group by a HAVING a = 2);
  2297. a
  2298. 3
  2299. SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 group by a HAVING a = 2);
  2300. a
  2301. 1
  2302. SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 group by a HAVING a = 2);
  2303. a
  2304. 2
  2305. SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 group by a HAVING a = 2);
  2306. a
  2307. 2
  2308. 3
  2309. SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 group by a HAVING a = 2);
  2310. a
  2311. 1
  2312. 2
  2313. SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 group by a HAVING a = 2);
  2314. a
  2315. 1
  2316. 3
  2317. SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-') from t1 a;
  2318. concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-')
  2319. 0-
  2320. 0-
  2321. 1-
  2322. SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-') from t1 a;
  2323. concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-')
  2324. 1-
  2325. 0-
  2326. 0-
  2327. SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-') from t1 a;
  2328. concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-')
  2329. 0-
  2330. 1-
  2331. 0-
  2332. DROP TABLE t1;
  2333. CREATE TABLE t1 ( a double, b double );
  2334. INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
  2335. SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2e0);
  2336. a
  2337. 3
  2338. SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2e0);
  2339. a
  2340. 1
  2341. SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2e0);
  2342. a
  2343. 2
  2344. SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2e0);
  2345. a
  2346. 2
  2347. 3
  2348. SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2e0);
  2349. a
  2350. 1
  2351. 2
  2352. SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2e0);
  2353. a
  2354. 1
  2355. 3
  2356. SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2e0);
  2357. a
  2358. 3
  2359. SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2e0);
  2360. a
  2361. 1
  2362. SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2e0);
  2363. a
  2364. 2
  2365. SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2e0);
  2366. a
  2367. 2
  2368. 3
  2369. SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2e0);
  2370. a
  2371. 1
  2372. 2
  2373. SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2e0);
  2374. a
  2375. 1
  2376. 3
  2377. DROP TABLE t1;
  2378. CREATE TABLE t1 ( a char(1), b char(1));
  2379. INSERT INTO t1 VALUES ('1','1'),('2','2'),('3','3');
  2380. SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = '2');
  2381. a
  2382. 3
  2383. SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = '2');
  2384. a
  2385. 1
  2386. SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = '2');
  2387. a
  2388. 2
  2389. SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = '2');
  2390. a
  2391. 2
  2392. 3
  2393. SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = '2');
  2394. a
  2395. 1
  2396. 2
  2397. SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = '2');
  2398. a
  2399. 1
  2400. 3
  2401. SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = '2');
  2402. a
  2403. 3
  2404. SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = '2');
  2405. a
  2406. 1
  2407. SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = '2');
  2408. a
  2409. 2
  2410. SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = '2');
  2411. a
  2412. 2
  2413. 3
  2414. SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = '2');
  2415. a
  2416. 1
  2417. 2
  2418. SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = '2');
  2419. a
  2420. 1
  2421. 3
  2422. DROP TABLE t1;
  2423. create table t1 (a int, b int);
  2424. insert into t1 values (1,2),(3,4);
  2425. select * from t1 up where exists (select * from t1 where t1.a=up.a);
  2426. a b
  2427. 1 2
  2428. 3 4
  2429. explain extended select * from t1 up where exists (select * from t1 where t1.a=up.a);
  2430. id select_type table type possible_keys key key_len ref rows filtered Extra
  2431. 1 PRIMARY up ALL NULL NULL NULL NULL 2 100.00 Using where
  2432. 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using where
  2433. Warnings:
  2434. Note 1276 Field or reference 'test.up.a' of SELECT #2 was resolved in SELECT #1
  2435. Note 1003 select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` where exists(select 1 from `test`.`t1` where (`test`.`t1`.`a` = `test`.`up`.`a`))
  2436. drop table t1;
  2437. CREATE TABLE t1 (t1_a int);
  2438. INSERT INTO t1 VALUES (1);
  2439. CREATE TABLE t2 (t2_a int, t2_b int, PRIMARY KEY (t2_a, t2_b));
  2440. INSERT INTO t2 VALUES (1, 1), (1, 2);
  2441. SELECT * FROM t1, t2 table2 WHERE t1_a = 1 AND table2.t2_a = 1
  2442. HAVING table2.t2_b = (SELECT MAX(t2_b) FROM t2 WHERE t2_a = table2.t2_a);
  2443. t1_a t2_a t2_b
  2444. 1 1 2
  2445. DROP TABLE t1, t2;
  2446. CREATE TABLE t1 (id int(11) default NULL,name varchar(10) default NULL);
  2447. INSERT INTO t1 VALUES (1,'Tim'),(2,'Rebecca'),(3,NULL);
  2448. CREATE TABLE t2 (id int(11) default NULL, pet varchar(10) default NULL);
  2449. INSERT INTO t2 VALUES (1,'Fido'),(2,'Spot'),(3,'Felix');
  2450. SELECT a.*, b.* FROM (SELECT * FROM t1) AS a JOIN t2 as b on a.id=b.id;
  2451. id name id pet
  2452. 1 Tim 1 Fido
  2453. 2 Rebecca 2 Spot
  2454. 3 NULL 3 Felix
  2455. drop table t1,t2;
  2456. CREATE TABLE t1 ( a int, b int );
  2457. CREATE TABLE t2 ( c int, d int );
  2458. INSERT INTO t1 VALUES (1,2), (2,3), (3,4);
  2459. SELECT a AS abc, b FROM t1 outr WHERE b =
  2460. (SELECT MIN(b) FROM t1 WHERE a=outr.a);
  2461. abc b
  2462. 1 2
  2463. 2 3
  2464. 3 4
  2465. INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b =
  2466. (SELECT MIN(b) FROM t1 WHERE a=outr.a);
  2467. select * from t2;
  2468. c d
  2469. 1 2
  2470. 2 3
  2471. 3 4
  2472. CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b =
  2473. (SELECT MIN(b) FROM t1 WHERE a=outr.a);
  2474. select * from t3;
  2475. abc b
  2476. 1 2
  2477. 2 3
  2478. 3 4
  2479. 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);";
  2480. execute stmt1;
  2481. deallocate prepare stmt1;
  2482. select * from t2;
  2483. c d
  2484. 1 2
  2485. 2 3
  2486. 3 4
  2487. 1 2
  2488. 2 3
  2489. 3 4
  2490. drop table t3;
  2491. 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);";
  2492. execute stmt1;
  2493. select * from t3;
  2494. abc b
  2495. 1 2
  2496. 2 3
  2497. 3 4
  2498. deallocate prepare stmt1;
  2499. DROP TABLE t1, t2, t3;
  2500. CREATE TABLE `t1` ( `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  2501. insert into t1 values (1);
  2502. CREATE TABLE `t2` ( `b` int(11) default NULL, `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  2503. insert into t2 values (1,2);
  2504. select t000.a, count(*) `C` FROM t1 t000 GROUP BY t000.a HAVING count(*) > ALL (SELECT count(*) FROM t2 t001 WHERE t001.a=1);
  2505. a C
  2506. 1 1
  2507. drop table t1,t2;
  2508. create table t1 (a int not null auto_increment primary key, b varchar(40), fulltext(b));
  2509. insert into t1 (b) values ('ball'),('ball games'), ('games'), ('foo'), ('foobar'), ('Serg'), ('Sergei'),('Georg'), ('Patrik'),('Hakan');
  2510. create table t2 (a int);
  2511. insert into t2 values (1),(3),(2),(7);
  2512. select a,b from t1 where match(b) against ('Ball') > 0;
  2513. a b
  2514. 1 ball
  2515. 2 ball games
  2516. select a from t2 where a in (select a from t1 where match(b) against ('Ball') > 0);
  2517. a
  2518. 1
  2519. 2
  2520. drop table t1,t2;
  2521. 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);
  2522. CREATE INDEX AK01IZAVORGANG ON t1(izaAnalyseart_id,Kuerzel);
  2523. INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000001','601','D0000000001','I0000000001');
  2524. INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000002','602','D0000000001','I0000000001');
  2525. INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000003','603','D0000000001','I0000000001');
  2526. INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000004','101','D0000000001','I0000000001');
  2527. SELECT `IZAVORGANG_ID` FROM t1 WHERE `KUERZEL` IN(SELECT MIN(`KUERZEL`)`Feld1` FROM t1 WHERE `KUERZEL` LIKE'601%'And`IZAANALYSEART_ID`='D0000000001');
  2528. IZAVORGANG_ID
  2529. D0000000001
  2530. drop table t1;
  2531. CREATE TABLE `t1` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY (`aid`,`bid`));
  2532. CREATE TABLE `t2` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY (`aid`,`bid`));
  2533. insert into t1 values (1,1),(1,2),(2,1),(2,2);
  2534. insert into t2 values (1,2),(2,2);
  2535. select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
  2536. aid bid
  2537. 1 1
  2538. 2 1
  2539. alter table t2 drop primary key;
  2540. alter table t2 add key KEY1 (aid, bid);
  2541. select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
  2542. aid bid
  2543. 1 1
  2544. 2 1
  2545. alter table t2 drop key KEY1;
  2546. alter table t2 add primary key (bid, aid);
  2547. select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
  2548. aid bid
  2549. 1 1
  2550. 2 1
  2551. drop table t1,t2;
  2552. CREATE TABLE t1 (howmanyvalues bigint, avalue int);
  2553. INSERT INTO t1 VALUES (1, 1),(2, 1),(2, 2),(3, 1),(3, 2),(3, 3),(4, 1),(4, 2),(4, 3),(4, 4);
  2554. SELECT howmanyvalues, count(*) from t1 group by howmanyvalues;
  2555. howmanyvalues count(*)
  2556. 1 1
  2557. 2 2
  2558. 3 3
  2559. 4 4
  2560. SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues;
  2561. howmanyvalues mycount
  2562. 1 1
  2563. 2 2
  2564. 3 3
  2565. 4 4
  2566. CREATE INDEX t1_howmanyvalues_idx ON t1 (howmanyvalues);
  2567. SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues+1 = a.howmanyvalues+1) as mycount from t1 a group by a.howmanyvalues;
  2568. howmanyvalues mycount
  2569. 1 1
  2570. 2 2
  2571. 3 3
  2572. 4 4
  2573. SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues;
  2574. howmanyvalues mycount
  2575. 1 1
  2576. 2 2
  2577. 3 3
  2578. 4 4
  2579. SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.avalue) as mycount from t1 a group by a.howmanyvalues;
  2580. howmanyvalues mycount
  2581. 1 1
  2582. 2 1
  2583. 3 1
  2584. 4 1
  2585. drop table t1;
  2586. create table t1 (x int);
  2587. 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;
  2588. (select b.x from t1 as b where b.x=a.x)
  2589. drop table t1;
  2590. 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`));
  2591. 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);
  2592. 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`)) ;
  2593. 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');
  2594. 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;
  2595. ERROR 42S22: Unknown column 'b.sc' in 'field list'
  2596. 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;
  2597. ac
  2598. 700
  2599. NULL
  2600. drop tables t1,t2;
  2601. create table t1 (a int not null, b int not null, c int, primary key (a,b));
  2602. insert into t1 values (1,1,1), (2,2,2), (3,3,3);
  2603. set @b:= 0;
  2604. explain select sum(a) from t1 where b > @b;
  2605. id select_type table type possible_keys key key_len ref rows Extra
  2606. 1 SIMPLE t1 index NULL PRIMARY 8 NULL 3 Using where; Using index
  2607. set @a:= (select sum(a) from t1 where b > @b);
  2608. explain select a from t1 where c=2;
  2609. id select_type table type possible_keys key key_len ref rows Extra
  2610. 1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using where
  2611. do @a:= (select sum(a) from t1 where b > @b);
  2612. explain select a from t1 where c=2;
  2613. id select_type table type possible_keys key key_len ref rows Extra
  2614. 1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using where
  2615. drop table t1;
  2616. set @got_val= (SELECT 1 FROM (SELECT 'A' as my_col) as T1 ) ;
  2617. create table t1 (a int, b int);
  2618. create table t2 (a int, b int);
  2619. insert into t1 values (1,1),(1,2),(1,3),(2,4),(2,5);
  2620. insert into t2 values (1,3),(2,1);
  2621. select distinct a,b, (select max(b) from t2 where t1.b=t2.a) from t1 order by t1.b;
  2622. a b (select max(b) from t2 where t1.b=t2.a)
  2623. 1 1 3
  2624. 1 2 1
  2625. 1 3 NULL
  2626. 2 4 NULL
  2627. 2 5 NULL
  2628. drop table t1, t2;
  2629. create table t1 (id int);
  2630. create table t2 (id int, body text, fulltext (body));
  2631. insert into t1 values(1),(2),(3);
  2632. insert into t2 values (1,'test'), (2,'mysql'), (3,'test'), (4,'test');
  2633. select count(distinct id) from t1 where id in (select id from t2 where match(body) against ('mysql' in boolean mode));
  2634. count(distinct id)
  2635. 1
  2636. drop table t2,t1;
  2637. create table t1 (s1 int,s2 int);
  2638. insert into t1 values (20,15);
  2639. select * from t1 where (('a',null) <=> (select 'a',s2 from t1 where s1 = 0));
  2640. s1 s2
  2641. drop table t1;
  2642. create table t1 (s1 int);
  2643. insert into t1 values (1),(null);
  2644. select * from t1 where s1 < all (select s1 from t1);
  2645. s1
  2646. select s1, s1 < all (select s1 from t1) from t1;
  2647. s1 s1 < all (select s1 from t1)
  2648. 1 0
  2649. NULL NULL
  2650. drop table t1;
  2651. CREATE TABLE t1 (
  2652. Code char(3) NOT NULL default '',
  2653. Name char(52) NOT NULL default '',
  2654. Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
  2655. Region char(26) NOT NULL default '',
  2656. SurfaceArea float(10,2) NOT NULL default '0.00',
  2657. IndepYear smallint(6) default NULL,
  2658. Population int(11) NOT NULL default '0',
  2659. LifeExpectancy float(3,1) default NULL,
  2660. GNP float(10,2) default NULL,
  2661. GNPOld float(10,2) default NULL,
  2662. LocalName char(45) NOT NULL default '',
  2663. GovernmentForm char(45) NOT NULL default '',
  2664. HeadOfState char(60) default NULL,
  2665. Capital int(11) default NULL,
  2666. Code2 char(2) NOT NULL default ''
  2667. ) ENGINE=MyISAM;
  2668. INSERT INTO t1 VALUES ('XXX','Xxxxx','Oceania','Xxxxxx',26.00,0,0,0,0,0,'Xxxxx','Xxxxx','Xxxxx',NULL,'XX');
  2669. 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');
  2670. 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');
  2671. 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');
  2672. /*!40000 ALTER TABLE t1 ENABLE KEYS */;
  2673. SELECT DISTINCT Continent AS c FROM t1 outr WHERE
  2674. Code <> SOME ( SELECT Code FROM t1 WHERE Continent = outr.Continent AND
  2675. Population < 200);
  2676. c
  2677. Oceania
  2678. drop table t1;
  2679. create table t1 (a1 int);
  2680. create table t2 (b1 int);
  2681. select * from t1 where a2 > any(select b1 from t2);
  2682. ERROR 42S22: Unknown column 'a2' in 'IN/ALL/ANY subquery'
  2683. select * from t1 where a1 > any(select b1 from t2);
  2684. a1
  2685. drop table t1,t2;
  2686. create table t1 (a integer, b integer);
  2687. select (select * from t1) = (select 1,2);
  2688. (select * from t1) = (select 1,2)
  2689. NULL
  2690. select (select 1,2) = (select * from t1);
  2691. (select 1,2) = (select * from t1)
  2692. NULL
  2693. select row(1,2) = ANY (select * from t1);
  2694. row(1,2) = ANY (select * from t1)
  2695. 0
  2696. select row(1,2) != ALL (select * from t1);
  2697. row(1,2) != ALL (select * from t1)
  2698. 1
  2699. drop table t1;
  2700. create table t1 (a integer, b integer);
  2701. select row(1,(2,2)) in (select * from t1 );
  2702. ERROR 21000: Operand should contain 2 column(s)
  2703. select row(1,(2,2)) = (select * from t1 );
  2704. ERROR 21000: Operand should contain 2 column(s)
  2705. select (select * from t1) = row(1,(2,2));
  2706. ERROR 21000: Operand should contain 1 column(s)
  2707. drop table t1;
  2708. create table t1 (a integer);
  2709. insert into t1 values (1);
  2710. select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx ;
  2711. ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
  2712. select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
  2713. ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
  2714. select 1 as xx, 1 = ALL ( select 1 from t1 where 1 = xx );
  2715. xx 1 = ALL ( select 1 from t1 where 1 = xx )
  2716. 1 1
  2717. select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
  2718. ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
  2719. select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx from DUAL;
  2720. ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
  2721. drop table t1;
  2722. CREATE TABLE t1 (
  2723. categoryId int(11) NOT NULL,
  2724. courseId int(11) NOT NULL,
  2725. startDate datetime NOT NULL,
  2726. endDate datetime NOT NULL,
  2727. createDate datetime NOT NULL,
  2728. modifyDate timestamp NOT NULL,
  2729. attributes text NOT NULL
  2730. );
  2731. INSERT INTO t1 VALUES (1,41,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
  2732. (1,86,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
  2733. (1,87,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
  2734. (2,52,'2004-03-15','2004-10-01','2004-03-15','2004-09-17',''),
  2735. (2,53,'2004-03-16','2004-10-01','2004-03-16','2004-09-17',''),
  2736. (2,88,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
  2737. (2,89,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
  2738. (3,51,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
  2739. (5,12,'2004-02-18','2010-01-01','2004-02-18','2004-02-18','');
  2740. CREATE TABLE t2 (
  2741. userId int(11) NOT NULL,
  2742. courseId int(11) NOT NULL,
  2743. date datetime NOT NULL
  2744. );
  2745. INSERT INTO t2 VALUES (5141,71,'2003-11-18'),
  2746. (5141,72,'2003-11-25'),(5141,41,'2004-08-06'),
  2747. (5141,52,'2004-08-06'),(5141,53,'2004-08-06'),
  2748. (5141,12,'2004-08-06'),(5141,86,'2004-10-21'),
  2749. (5141,87,'2004-10-21'),(5141,88,'2004-10-21'),
  2750. (5141,89,'2004-10-22'),(5141,51,'2004-10-26');
  2751. CREATE TABLE t3 (
  2752. groupId int(11) NOT NULL,
  2753. parentId int(11) NOT NULL,
  2754. startDate datetime NOT NULL,
  2755. endDate datetime NOT NULL,
  2756. createDate datetime NOT NULL,
  2757. modifyDate timestamp NOT NULL,
  2758. ordering int(11)
  2759. );
  2760. INSERT INTO t3 VALUES (12,9,'1000-01-01','3999-12-31','2004-01-29','2004-01-29',NULL);
  2761. CREATE TABLE t4 (
  2762. id int(11) NOT NULL,
  2763. groupTypeId int(11) NOT NULL,
  2764. groupKey varchar(50) NOT NULL,
  2765. name text,
  2766. ordering int(11),
  2767. description text,
  2768. createDate datetime NOT NULL,
  2769. modifyDate timestamp NOT NULL
  2770. );
  2771. INSERT INTO t4 VALUES (9,5,'stationer','stationer',0,'Stationer','2004-01-29','2004-01-29'),
  2772. (12,5,'group2','group2',0,'group2','2004-01-29','2004-01-29');
  2773. CREATE TABLE t5 (
  2774. userId int(11) NOT NULL,
  2775. groupId int(11) NOT NULL,
  2776. createDate datetime NOT NULL,
  2777. modifyDate timestamp NOT NULL
  2778. );
  2779. INSERT INTO t5 VALUES (5141,12,'2004-08-06','2004-08-06');
  2780. select
  2781. count(distinct t2.userid) pass,
  2782. groupstuff.*,
  2783. count(t2.courseid) crse,
  2784. t1.categoryid,
  2785. t2.courseid,
  2786. date_format(date, '%b%y') as colhead
  2787. from t2
  2788. join t1 on t2.courseid=t1.courseid
  2789. join
  2790. (
  2791. select
  2792. t5.userid,
  2793. parentid,
  2794. parentgroup,
  2795. childid,
  2796. groupname,
  2797. grouptypeid
  2798. from t5
  2799. join
  2800. (
  2801. select t4.id as parentid,
  2802. t4.name as parentgroup,
  2803. t4.id as childid,
  2804. t4.name as groupname,
  2805. t4.grouptypeid
  2806. from t4
  2807. ) as gin on t5.groupid=gin.childid
  2808. ) as groupstuff on t2.userid = groupstuff.userid
  2809. group by
  2810. groupstuff.groupname, colhead , t2.courseid;
  2811. pass userid parentid parentgroup childid groupname grouptypeid crse categoryid courseid colhead
  2812. 1 5141 12 group2 12 group2 5 1 5 12 Aug04
  2813. 1 5141 12 group2 12 group2 5 1 1 41 Aug04
  2814. 1 5141 12 group2 12 group2 5 1 2 52 Aug04
  2815. 1 5141 12 group2 12 group2 5 1 2 53 Aug04
  2816. 1 5141 12 group2 12 group2 5 1 3 51 Oct04
  2817. 1 5141 12 group2 12 group2 5 1 1 86 Oct04
  2818. 1 5141 12 group2 12 group2 5 1 1 87 Oct04
  2819. 1 5141 12 group2 12 group2 5 1 2 88 Oct04
  2820. 1 5141 12 group2 12 group2 5 1 2 89 Oct04
  2821. drop table t1, t2, t3, t4, t5;
  2822. create table t1 (a int);
  2823. insert into t1 values (1), (2), (3);
  2824. SELECT 1 FROM t1 WHERE (SELECT 1) in (SELECT 1);
  2825. 1
  2826. 1
  2827. 1
  2828. 1
  2829. drop table t1;
  2830. create table t1 (a int);
  2831. create table t2 (a int);
  2832. insert into t1 values (1),(2);
  2833. insert into t2 values (0),(1),(2),(3);
  2834. select a from t2 where a in (select a from t1);
  2835. a
  2836. 1
  2837. 2
  2838. select a from t2 having a in (select a from t1);
  2839. a
  2840. 1
  2841. 2
  2842. prepare stmt1 from "select a from t2 where a in (select a from t1)";
  2843. execute stmt1;
  2844. a
  2845. 1
  2846. 2
  2847. execute stmt1;
  2848. a
  2849. 1
  2850. 2
  2851. deallocate prepare stmt1;
  2852. prepare stmt1 from "select a from t2 having a in (select a from t1)";
  2853. execute stmt1;
  2854. a
  2855. 1
  2856. 2
  2857. execute stmt1;
  2858. a
  2859. 1
  2860. 2
  2861. deallocate prepare stmt1;
  2862. drop table t1, t2;
  2863. create table t1 (a int, b int);
  2864. insert into t1 values (1,2);
  2865. select 1 = (select * from t1);
  2866. ERROR 21000: Operand should contain 1 column(s)
  2867. select (select * from t1) = 1;
  2868. ERROR 21000: Operand should contain 2 column(s)
  2869. select (1,2) = (select a from t1);
  2870. ERROR 21000: Operand should contain 2 column(s)
  2871. select (select a from t1) = (1,2);
  2872. ERROR 21000: Operand should contain 1 column(s)
  2873. select (1,2,3) = (select * from t1);
  2874. ERROR 21000: Operand should contain 3 column(s)
  2875. select (select * from t1) = (1,2,3);
  2876. ERROR 21000: Operand should contain 2 column(s)
  2877. drop table t1;
  2878. CREATE TABLE `t1` (
  2879. `itemid` bigint(20) unsigned NOT NULL auto_increment,
  2880. `sessionid` bigint(20) unsigned default NULL,
  2881. `time` int(10) unsigned NOT NULL default '0',
  2882. `type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT
  2883. NULL default '',
  2884. `data` text collate latin1_general_ci NOT NULL,
  2885. PRIMARY KEY (`itemid`)
  2886. ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
  2887. INSERT INTO `t1` VALUES (1, 1, 1, 'D', '');
  2888. CREATE TABLE `t2` (
  2889. `sessionid` bigint(20) unsigned NOT NULL auto_increment,
  2890. `pid` int(10) unsigned NOT NULL default '0',
  2891. `date` int(10) unsigned NOT NULL default '0',
  2892. `ip` varchar(15) collate latin1_general_ci NOT NULL default '',
  2893. PRIMARY KEY (`sessionid`)
  2894. ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
  2895. INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
  2896. 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;
  2897. ip count( e.itemid )
  2898. 10.10.10.1 1
  2899. drop tables t1,t2;
  2900. create table t1 (fld enum('0','1'));
  2901. insert into t1 values ('1');
  2902. select * from (select max(fld) from t1) as foo;
  2903. max(fld)
  2904. 1
  2905. drop table t1;
  2906. set @tmp11867_optimizer_switch=@@optimizer_switch;
  2907. set optimizer_switch='semijoin_with_cache=off';
  2908. CREATE TABLE t1 (one int, two int, flag char(1));
  2909. CREATE TABLE t2 (one int, two int, flag char(1));
  2910. INSERT INTO t1 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N');
  2911. INSERT INTO t2 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N');
  2912. SELECT * FROM t1
  2913. WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t2 WHERE flag = 'N');
  2914. one two flag
  2915. 5 6 N
  2916. 7 8 N
  2917. SELECT * FROM t1
  2918. WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t1 WHERE flag = 'N');
  2919. one two flag
  2920. 5 6 N
  2921. 7 8 N
  2922. insert into t2 values (null,null,'N');
  2923. insert into t2 values (null,3,'0');
  2924. insert into t2 values (null,5,'0');
  2925. insert into t2 values (10,null,'0');
  2926. insert into t1 values (10,3,'0');
  2927. insert into t1 values (10,5,'0');
  2928. insert into t1 values (10,10,'0');
  2929. SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N') as 'test' from t1;
  2930. one two test
  2931. 1 2 NULL
  2932. 2 3 NULL
  2933. 3 4 NULL
  2934. 5 6 1
  2935. 7 8 1
  2936. 10 3 NULL
  2937. 10 5 NULL
  2938. 10 10 NULL
  2939. SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N');
  2940. one two
  2941. 5 6
  2942. 7 8
  2943. SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N' group by one,two) as 'test' from t1;
  2944. one two test
  2945. 1 2 NULL
  2946. 2 3 NULL
  2947. 3 4 NULL
  2948. 5 6 1
  2949. 7 8 1
  2950. 10 3 NULL
  2951. 10 5 NULL
  2952. 10 10 NULL
  2953. SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1;
  2954. one two test
  2955. 1 2 0
  2956. 2 3 NULL
  2957. 3 4 0
  2958. 5 6 0
  2959. 7 8 0
  2960. 10 3 NULL
  2961. 10 5 NULL
  2962. 10 10 NULL
  2963. SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1;
  2964. one two test
  2965. 1 2 0
  2966. 2 3 NULL
  2967. 3 4 0
  2968. 5 6 0
  2969. 7 8 0
  2970. 10 3 NULL
  2971. 10 5 NULL
  2972. 10 10 NULL
  2973. explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1;
  2974. id select_type table type possible_keys key key_len ref rows filtered Extra
  2975. 1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00
  2976. 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where
  2977. Warnings:
  2978. Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `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`
  2979. explain extended SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N');
  2980. id select_type table type possible_keys key key_len ref rows filtered Extra
  2981. 1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00
  2982. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1 100.00
  2983. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 9 100.00 Using where
  2984. Warnings:
  2985. 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'))
  2986. 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;
  2987. id select_type table type possible_keys key key_len ref rows filtered Extra
  2988. 1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00
  2989. 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 9 100.00 Using where
  2990. Warnings:
  2991. Note 1003 select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `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`
  2992. DROP TABLE t1,t2;
  2993. set optimizer_switch=@tmp11867_optimizer_switch;
  2994. CREATE TABLE t1 (a char(5), b char(5));
  2995. INSERT INTO t1 VALUES (NULL,'aaa'), ('aaa','aaa');
  2996. SELECT * FROM t1 WHERE (a,b) IN (('aaa','aaa'), ('aaa','bbb'));
  2997. a b
  2998. aaa aaa
  2999. DROP TABLE t1;
  3000. CREATE TABLE t1 (a int);
  3001. CREATE TABLE t2 (a int, b int);
  3002. CREATE TABLE t3 (b int NOT NULL);
  3003. INSERT INTO t1 VALUES (1), (2), (3), (4);
  3004. INSERT INTO t2 VALUES (1,10), (3,30);
  3005. SELECT * FROM t2 LEFT JOIN t3 ON t2.b=t3.b
  3006. WHERE t3.b IS NOT NULL OR t2.a > 10;
  3007. a b b
  3008. SELECT * FROM t1
  3009. WHERE t1.a NOT IN (SELECT a FROM t2 LEFT JOIN t3 ON t2.b=t3.b
  3010. WHERE t3.b IS NOT NULL OR t2.a > 10);
  3011. a
  3012. 1
  3013. 2
  3014. 3
  3015. 4
  3016. DROP TABLE t1,t2,t3;
  3017. CREATE TABLE t1 (f1 INT);
  3018. CREATE TABLE t2 (f2 INT);
  3019. INSERT INTO t1 VALUES (1);
  3020. SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2);
  3021. f1
  3022. 1
  3023. SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE 1=0);
  3024. f1
  3025. 1
  3026. INSERT INTO t2 VALUES (1);
  3027. INSERT INTO t2 VALUES (2);
  3028. SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE f2=0);
  3029. f1
  3030. 1
  3031. DROP TABLE t1, t2;
  3032. select 1 from dual where 1 < any (select 2);
  3033. 1
  3034. 1
  3035. select 1 from dual where 1 < all (select 2);
  3036. 1
  3037. 1
  3038. select 1 from dual where 2 > any (select 1);
  3039. 1
  3040. 1
  3041. select 1 from dual where 2 > all (select 1);
  3042. 1
  3043. 1
  3044. select 1 from dual where 1 < any (select 2 from dual);
  3045. 1
  3046. 1
  3047. select 1 from dual where 1 < all (select 2 from dual where 1!=1);
  3048. 1
  3049. 1
  3050. create table t1 (s1 char);
  3051. insert into t1 values (1),(2);
  3052. select * from t1 where (s1 < any (select s1 from t1));
  3053. s1
  3054. 1
  3055. select * from t1 where not (s1 < any (select s1 from t1));
  3056. s1
  3057. 2
  3058. select * from t1 where (s1 < ALL (select s1+1 from t1));
  3059. s1
  3060. 1
  3061. select * from t1 where not(s1 < ALL (select s1+1 from t1));
  3062. s1
  3063. 2
  3064. select * from t1 where (s1+1 = ANY (select s1 from t1));
  3065. s1
  3066. 1
  3067. select * from t1 where NOT(s1+1 = ANY (select s1 from t1));
  3068. s1
  3069. 2
  3070. select * from t1 where (s1 = ALL (select s1/s1 from t1));
  3071. s1
  3072. 1
  3073. select * from t1 where NOT(s1 = ALL (select s1/s1 from t1));
  3074. s1
  3075. 2
  3076. drop table t1;
  3077. create table t1 (
  3078. retailerID varchar(8) NOT NULL,
  3079. statusID int(10) unsigned NOT NULL,
  3080. changed datetime NOT NULL,
  3081. UNIQUE KEY retailerID (retailerID, statusID, changed)
  3082. );
  3083. INSERT INTO t1 VALUES("0026", "1", "2005-12-06 12:18:56");
  3084. INSERT INTO t1 VALUES("0026", "2", "2006-01-06 12:25:53");
  3085. INSERT INTO t1 VALUES("0037", "1", "2005-12-06 12:18:56");
  3086. INSERT INTO t1 VALUES("0037", "2", "2006-01-06 12:25:53");
  3087. INSERT INTO t1 VALUES("0048", "1", "2006-01-06 12:37:50");
  3088. INSERT INTO t1 VALUES("0059", "1", "2006-01-06 12:37:50");
  3089. select * from t1 r1
  3090. where (r1.retailerID,(r1.changed)) in
  3091. (SELECT r2.retailerId,(max(changed)) from t1 r2
  3092. group by r2.retailerId);
  3093. retailerID statusID changed
  3094. 0026 2 2006-01-06 12:25:53
  3095. 0037 2 2006-01-06 12:25:53
  3096. 0048 1 2006-01-06 12:37:50
  3097. 0059 1 2006-01-06 12:37:50
  3098. drop table t1;
  3099. create table t1(a int, primary key (a));
  3100. insert into t1 values (10);
  3101. create table t2 (a int primary key, b varchar(32), c int, unique key b(c, b));
  3102. insert into t2(a, c, b) values (1,10,'359'), (2,10,'35988'), (3,10,'35989');
  3103. explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
  3104. ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
  3105. ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
  3106. id select_type table type possible_keys key key_len ref rows Extra
  3107. 1 PRIMARY t1 system PRIMARY NULL NULL NULL 1
  3108. 1 PRIMARY r eq_ref PRIMARY PRIMARY 4 const 1 Using where
  3109. 2 DEPENDENT SUBQUERY t2 range b b 40 NULL 2 Using index condition
  3110. SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
  3111. ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
  3112. ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
  3113. a a b
  3114. 10 3 35989
  3115. explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
  3116. ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
  3117. ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
  3118. id select_type table type possible_keys key key_len ref rows Extra
  3119. 1 PRIMARY t1 system PRIMARY NULL NULL NULL 1
  3120. 1 PRIMARY r eq_ref PRIMARY PRIMARY 4 const 1 Using where
  3121. 2 DEPENDENT SUBQUERY t2 range b b 40 NULL 2 Using index condition
  3122. SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
  3123. ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
  3124. ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
  3125. a a b
  3126. 10 1 359
  3127. drop table t1,t2;
  3128. CREATE TABLE t1 (
  3129. field1 int NOT NULL,
  3130. field2 int NOT NULL,
  3131. field3 int NOT NULL,
  3132. PRIMARY KEY (field1,field2,field3)
  3133. );
  3134. CREATE TABLE t2 (
  3135. fieldA int NOT NULL,
  3136. fieldB int NOT NULL,
  3137. PRIMARY KEY (fieldA,fieldB)
  3138. );
  3139. INSERT INTO t1 VALUES
  3140. (1,1,1), (1,1,2), (1,2,1), (1,2,2), (1,2,3), (1,3,1);
  3141. INSERT INTO t2 VALUES (1,1), (1,2), (1,3);
  3142. SELECT field1, field2, COUNT(*)
  3143. FROM t1 GROUP BY field1, field2;
  3144. field1 field2 COUNT(*)
  3145. 1 1 2
  3146. 1 2 3
  3147. 1 3 1
  3148. SELECT field1, field2
  3149. FROM t1
  3150. GROUP BY field1, field2
  3151. HAVING COUNT(*) >= ALL (SELECT fieldB
  3152. FROM t2 WHERE fieldA = field1);
  3153. field1 field2
  3154. 1 2
  3155. SELECT field1, field2
  3156. FROM t1
  3157. GROUP BY field1, field2
  3158. HAVING COUNT(*) < ANY (SELECT fieldB
  3159. FROM t2 WHERE fieldA = field1);
  3160. field1 field2
  3161. 1 1
  3162. 1 3
  3163. DROP TABLE t1, t2;
  3164. CREATE TABLE t1(a int, INDEX (a));
  3165. INSERT INTO t1 VALUES (1), (3), (5), (7);
  3166. INSERT INTO t1 VALUES (NULL);
  3167. CREATE TABLE t2(a int);
  3168. INSERT INTO t2 VALUES (1),(2),(3);
  3169. EXPLAIN SELECT a, a IN (SELECT a FROM t1) FROM t2;
  3170. id select_type table type possible_keys key key_len ref rows Extra
  3171. 1 PRIMARY t2 ALL NULL NULL NULL NULL 3
  3172. 2 DEPENDENT SUBQUERY t1 index_subquery a a 5 func 2 Using index; Full scan on NULL key
  3173. SELECT a, a IN (SELECT a FROM t1) FROM t2;
  3174. a a IN (SELECT a FROM t1)
  3175. 1 1
  3176. 2 NULL
  3177. 3 1
  3178. DROP TABLE t1,t2;
  3179. CREATE TABLE t1 (a DATETIME);
  3180. INSERT INTO t1 VALUES ('1998-09-23'), ('2003-03-25');
  3181. CREATE TABLE t2 AS SELECT
  3182. (SELECT a FROM t1 WHERE a < '2000-01-01') AS sub_a
  3183. FROM t1 WHERE a > '2000-01-01';
  3184. SHOW CREATE TABLE t2;
  3185. Table Create Table
  3186. t2 CREATE TABLE `t2` (
  3187. `sub_a` datetime DEFAULT NULL
  3188. ) ENGINE=MyISAM DEFAULT CHARSET=latin1
  3189. CREATE TABLE t3 AS (SELECT a FROM t1 WHERE a < '2000-01-01') UNION (SELECT a FROM t1 WHERE a > '2000-01-01');
  3190. SHOW CREATE TABLE t3;
  3191. Table Create Table
  3192. t3 CREATE TABLE `t3` (
  3193. `a` datetime DEFAULT NULL
  3194. ) ENGINE=MyISAM DEFAULT CHARSET=latin1
  3195. DROP TABLE t1,t2,t3;
  3196. CREATE TABLE t1 (a int);
  3197. INSERT INTO t1 VALUES (1), (2);
  3198. SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) > 0;
  3199. a
  3200. SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL;
  3201. a
  3202. 1
  3203. 2
  3204. EXPLAIN SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL;
  3205. id select_type table type possible_keys key key_len ref rows Extra
  3206. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2
  3207. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  3208. DROP TABLE t1;
  3209. CREATE TABLE t1 (a int);
  3210. INSERT INTO t1 VALUES (2), (4), (1), (3);
  3211. CREATE TABLE t2 (b int, c int);
  3212. INSERT INTO t2 VALUES
  3213. (2,1), (1,3), (2,1), (4,4), (2,2), (1,4);
  3214. SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2 );
  3215. a
  3216. 2
  3217. 4
  3218. 1
  3219. 3
  3220. SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1);
  3221. ERROR 21000: Subquery returns more than 1 row
  3222. SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2), a;
  3223. a
  3224. 1
  3225. 2
  3226. 3
  3227. 4
  3228. SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1), a;
  3229. ERROR 21000: Subquery returns more than 1 row
  3230. SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 2);
  3231. b MAX(c)
  3232. 1 4
  3233. 2 2
  3234. 4 4
  3235. SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 1);
  3236. ERROR 21000: Subquery returns more than 1 row
  3237. SELECT a FROM t1 GROUP BY a
  3238. HAVING IFNULL((SELECT b FROM t2 WHERE b > 2),
  3239. (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
  3240. a
  3241. 1
  3242. 2
  3243. 3
  3244. 4
  3245. SELECT a FROM t1 GROUP BY a
  3246. HAVING IFNULL((SELECT b FROM t2 WHERE b > 1),
  3247. (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
  3248. ERROR 21000: Subquery returns more than 1 row
  3249. SELECT a FROM t1 GROUP BY a
  3250. HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
  3251. (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
  3252. a
  3253. 4
  3254. SELECT a FROM t1 GROUP BY a
  3255. HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
  3256. (SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)) > 3;
  3257. ERROR 21000: Subquery returns more than 1 row
  3258. SELECT a FROM t1
  3259. ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 2),
  3260. (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b));
  3261. a
  3262. 1
  3263. 2
  3264. 3
  3265. 4
  3266. SELECT a FROM t1
  3267. ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 1),
  3268. (SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b));
  3269. ERROR 21000: Subquery returns more than 1 row
  3270. SELECT a FROM t1
  3271. ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
  3272. (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b));
  3273. a
  3274. 1
  3275. 2
  3276. 3
  3277. 4
  3278. SELECT a FROM t1
  3279. ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
  3280. (SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b));
  3281. ERROR 21000: Subquery returns more than 1 row
  3282. DROP TABLE t1,t2;
  3283. create table t1 (df decimal(5,1));
  3284. insert into t1 values(1.1);
  3285. insert into t1 values(2.2);
  3286. select * from t1 where df <= all (select avg(df) from t1 group by df);
  3287. df
  3288. 1.1
  3289. select * from t1 where df >= all (select avg(df) from t1 group by df);
  3290. df
  3291. 2.2
  3292. drop table t1;
  3293. create table t1 (df decimal(5,1));
  3294. insert into t1 values(1.1);
  3295. select 1.1 * exists(select * from t1);
  3296. 1.1 * exists(select * from t1)
  3297. 1.1
  3298. drop table t1;
  3299. CREATE TABLE t1 (
  3300. grp int(11) default NULL,
  3301. a decimal(10,2) default NULL);
  3302. insert into t1 values (1, 1), (2, 2), (2, 3), (3, 4), (3, 5), (3, 6), (NULL, NULL);
  3303. select * from t1;
  3304. grp a
  3305. 1 1.00
  3306. 2 2.00
  3307. 2 3.00
  3308. 3 4.00
  3309. 3 5.00
  3310. 3 6.00
  3311. NULL NULL
  3312. select min(a) from t1 group by grp;
  3313. min(a)
  3314. NULL
  3315. 1.00
  3316. 2.00
  3317. 4.00
  3318. drop table t1;
  3319. CREATE table t1 ( c1 integer );
  3320. INSERT INTO t1 VALUES ( 1 );
  3321. INSERT INTO t1 VALUES ( 2 );
  3322. INSERT INTO t1 VALUES ( 3 );
  3323. CREATE TABLE t2 ( c2 integer );
  3324. INSERT INTO t2 VALUES ( 1 );
  3325. INSERT INTO t2 VALUES ( 4 );
  3326. INSERT INTO t2 VALUES ( 5 );
  3327. SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2 WHERE c2 IN (1);
  3328. c1 c2
  3329. 1 1
  3330. SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2
  3331. WHERE c2 IN ( SELECT c2 FROM t2 WHERE c2 IN ( 1 ) );
  3332. c1 c2
  3333. 1 1
  3334. DROP TABLE t1,t2;
  3335. CREATE TABLE t1 ( c1 integer );
  3336. INSERT INTO t1 VALUES ( 1 );
  3337. INSERT INTO t1 VALUES ( 2 );
  3338. INSERT INTO t1 VALUES ( 3 );
  3339. INSERT INTO t1 VALUES ( 6 );
  3340. CREATE TABLE t2 ( c2 integer );
  3341. INSERT INTO t2 VALUES ( 1 );
  3342. INSERT INTO t2 VALUES ( 4 );
  3343. INSERT INTO t2 VALUES ( 5 );
  3344. INSERT INTO t2 VALUES ( 6 );
  3345. CREATE TABLE t3 ( c3 integer );
  3346. INSERT INTO t3 VALUES ( 7 );
  3347. INSERT INTO t3 VALUES ( 8 );
  3348. SELECT c1,c2 FROM t1 LEFT JOIN t2 ON c1 = c2
  3349. WHERE EXISTS (SELECT c3 FROM t3 WHERE c2 IS NULL );
  3350. c1 c2
  3351. 2 NULL
  3352. 3 NULL
  3353. DROP TABLE t1,t2,t3;
  3354. CREATE TABLE `t1` (
  3355. `itemid` bigint(20) unsigned NOT NULL auto_increment,
  3356. `sessionid` bigint(20) unsigned default NULL,
  3357. `time` int(10) unsigned NOT NULL default '0',
  3358. `type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT
  3359. NULL default '',
  3360. `data` text collate latin1_general_ci NOT NULL,
  3361. PRIMARY KEY (`itemid`)
  3362. ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
  3363. INSERT INTO `t1` VALUES (1, 1, 1, 'D', '');
  3364. CREATE TABLE `t2` (
  3365. `sessionid` bigint(20) unsigned NOT NULL auto_increment,
  3366. `pid` int(10) unsigned NOT NULL default '0',
  3367. `date` int(10) unsigned NOT NULL default '0',
  3368. `ip` varchar(15) collate latin1_general_ci NOT NULL default '',
  3369. PRIMARY KEY (`sessionid`)
  3370. ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
  3371. INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
  3372. 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;
  3373. ip count( e.itemid )
  3374. 10.10.10.1 1
  3375. drop tables t1,t2;
  3376. CREATE TABLE t1 (EMPNUM CHAR(3));
  3377. CREATE TABLE t2 (EMPNUM CHAR(3) );
  3378. INSERT INTO t1 VALUES ('E1'),('E2');
  3379. INSERT INTO t2 VALUES ('E1');
  3380. DELETE FROM t1
  3381. WHERE t1.EMPNUM NOT IN
  3382. (SELECT t2.EMPNUM
  3383. FROM t2
  3384. WHERE t1.EMPNUM = t2.EMPNUM);
  3385. select * from t1;
  3386. EMPNUM
  3387. E1
  3388. DROP TABLE t1,t2;
  3389. CREATE TABLE t1(select_id BIGINT, values_id BIGINT);
  3390. INSERT INTO t1 VALUES (1, 1);
  3391. CREATE TABLE t2 (select_id BIGINT, values_id BIGINT,
  3392. PRIMARY KEY(select_id,values_id));
  3393. INSERT INTO t2 VALUES (0, 1), (0, 2), (0, 3), (1, 5);
  3394. SELECT values_id FROM t1
  3395. WHERE values_id IN (SELECT values_id FROM t2
  3396. WHERE select_id IN (1, 0));
  3397. values_id
  3398. 1
  3399. SELECT values_id FROM t1
  3400. WHERE values_id IN (SELECT values_id FROM t2
  3401. WHERE select_id BETWEEN 0 AND 1);
  3402. values_id
  3403. 1
  3404. SELECT values_id FROM t1
  3405. WHERE values_id IN (SELECT values_id FROM t2
  3406. WHERE select_id = 0 OR select_id = 1);
  3407. values_id
  3408. 1
  3409. DROP TABLE t1, t2;
  3410. create table t1 (fld enum('0','1'));
  3411. insert into t1 values ('1');
  3412. select * from (select max(fld) from t1) as foo;
  3413. max(fld)
  3414. 1
  3415. drop table t1;
  3416. CREATE TABLE t1 (a int, b int);
  3417. CREATE TABLE t2 (c int, d int);
  3418. CREATE TABLE t3 (e int);
  3419. INSERT INTO t1 VALUES
  3420. (1,10), (2,10), (1,20), (2,20), (3,20), (2,30), (4,40);
  3421. INSERT INTO t2 VALUES
  3422. (2,10), (2,20), (4,10), (5,10), (3,20), (2,40);
  3423. INSERT INTO t3 VALUES (10), (30), (10), (20) ;
  3424. SELECT a, MAX(b), MIN(b) FROM t1 GROUP BY a;
  3425. a MAX(b) MIN(b)
  3426. 1 20 10
  3427. 2 30 10
  3428. 3 20 20
  3429. 4 40 40
  3430. SELECT * FROM t2;
  3431. c d
  3432. 2 10
  3433. 2 20
  3434. 4 10
  3435. 5 10
  3436. 3 20
  3437. 2 40
  3438. SELECT * FROM t3;
  3439. e
  3440. 10
  3441. 30
  3442. 10
  3443. 20
  3444. SELECT a FROM t1 GROUP BY a
  3445. HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20);
  3446. a
  3447. 2
  3448. 4
  3449. SELECT a FROM t1 GROUP BY a
  3450. HAVING a IN (SELECT c FROM t2 WHERE MAX(b)<d);
  3451. a
  3452. 2
  3453. SELECT a FROM t1 GROUP BY a
  3454. HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>d);
  3455. a
  3456. 2
  3457. 4
  3458. SELECT a FROM t1 GROUP BY a
  3459. HAVING a IN (SELECT c FROM t2
  3460. WHERE d >= SOME(SELECT e FROM t3 WHERE MAX(b)=e));
  3461. a
  3462. 2
  3463. 3
  3464. SELECT a FROM t1 GROUP BY a
  3465. HAVING a IN (SELECT c FROM t2
  3466. WHERE EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
  3467. a
  3468. 2
  3469. 3
  3470. SELECT a FROM t1 GROUP BY a
  3471. HAVING a IN (SELECT c FROM t2
  3472. WHERE d > SOME(SELECT e FROM t3 WHERE MAX(b)=e));
  3473. a
  3474. 2
  3475. SELECT a FROM t1 GROUP BY a
  3476. HAVING a IN (SELECT c FROM t2
  3477. WHERE EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e < d));
  3478. a
  3479. 2
  3480. SELECT a FROM t1 GROUP BY a
  3481. HAVING a IN (SELECT c FROM t2
  3482. WHERE MIN(b) < d AND
  3483. EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
  3484. a
  3485. 2
  3486. SELECT a, SUM(a) FROM t1 GROUP BY a;
  3487. a SUM(a)
  3488. 1 2
  3489. 2 6
  3490. 3 3
  3491. 4 4
  3492. SELECT a FROM t1
  3493. WHERE EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c) GROUP BY a;
  3494. a
  3495. 3
  3496. 4
  3497. SELECT a FROM t1 GROUP BY a
  3498. HAVING EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c);
  3499. a
  3500. 1
  3501. 3
  3502. 4
  3503. SELECT a FROM t1
  3504. WHERE a < 3 AND
  3505. EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c) GROUP BY a;
  3506. a
  3507. 1
  3508. 2
  3509. SELECT a FROM t1
  3510. WHERE a < 3 AND
  3511. EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c);
  3512. a
  3513. 1
  3514. 2
  3515. 1
  3516. 2
  3517. 2
  3518. SELECT t1.a FROM t1 GROUP BY t1.a
  3519. HAVING t1.a < ALL(SELECT t2.c FROM t2 GROUP BY t2.c
  3520. HAVING EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
  3521. HAVING SUM(t1.a+t2.c) < t3.e/4));
  3522. a
  3523. 1
  3524. 2
  3525. SELECT t1.a FROM t1 GROUP BY t1.a
  3526. HAVING t1.a > ALL(SELECT t2.c FROM t2
  3527. WHERE EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
  3528. HAVING SUM(t1.a+t2.c) < t3.e/4));
  3529. a
  3530. 4
  3531. SELECT t1.a FROM t1 GROUP BY t1.a
  3532. HAVING t1.a > ALL(SELECT t2.c FROM t2
  3533. WHERE EXISTS(SELECT t3.e FROM t3
  3534. WHERE SUM(t1.a+t2.c) < t3.e/4));
  3535. ERROR HY000: Invalid use of group function
  3536. SELECT t1.a from t1 GROUP BY t1.a HAVING AVG(SUM(t1.b)) > 20;
  3537. ERROR HY000: Invalid use of group function
  3538. SELECT t1.a FROM t1 GROUP BY t1.a
  3539. HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
  3540. HAVING AVG(t2.c+SUM(t1.b)) > 20);
  3541. a
  3542. 2
  3543. 3
  3544. 4
  3545. SELECT t1.a FROM t1 GROUP BY t1.a
  3546. HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
  3547. HAVING AVG(SUM(t1.b)) > 20);
  3548. a
  3549. 2
  3550. 4
  3551. SELECT t1.a, SUM(b) AS sum FROM t1 GROUP BY t1.a
  3552. HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
  3553. HAVING t2.c+sum > 20);
  3554. a sum
  3555. 2 60
  3556. 3 20
  3557. 4 40
  3558. DROP TABLE t1,t2,t3;
  3559. CREATE TABLE t1 (a varchar(5), b varchar(10));
  3560. INSERT INTO t1 VALUES
  3561. ('AAA', 5), ('BBB', 4), ('BBB', 1), ('CCC', 2),
  3562. ('CCC', 7), ('AAA', 2), ('AAA', 4), ('BBB', 3), ('AAA', 8);
  3563. SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
  3564. a b
  3565. BBB 4
  3566. CCC 7
  3567. AAA 8
  3568. EXPLAIN
  3569. SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
  3570. id select_type table type possible_keys key key_len ref rows Extra
  3571. 1 PRIMARY t1 ALL NULL NULL NULL NULL 9 Using where
  3572. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 21 test.t1.a,test.t1.b 1
  3573. 2 MATERIALIZED t1 ALL NULL NULL NULL NULL 9 Using temporary
  3574. ALTER TABLE t1 ADD INDEX(a);
  3575. SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
  3576. a b
  3577. BBB 4
  3578. CCC 7
  3579. AAA 8
  3580. EXPLAIN
  3581. SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
  3582. id select_type table type possible_keys key key_len ref rows Extra
  3583. 1 PRIMARY t1 ALL a NULL NULL NULL 9 Using where
  3584. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 21 test.t1.a,test.t1.b 1
  3585. 2 MATERIALIZED t1 ALL NULL NULL NULL NULL 9 Using temporary
  3586. DROP TABLE t1;
  3587. create table t1( f1 int,f2 int);
  3588. insert into t1 values (1,1),(2,2);
  3589. 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';
  3590. t
  3591. crash1
  3592. crash1
  3593. drop table t1;
  3594. create table t1 (c int, key(c));
  3595. insert into t1 values (1142477582), (1142455969);
  3596. create table t2 (a int, b int);
  3597. insert into t2 values (2, 1), (1, 0);
  3598. delete from t1 where c <= 1140006215 and (select b from t2 where a = 2) = 1;
  3599. drop table t1, t2;
  3600. CREATE TABLE t1 (a INT);
  3601. CREATE VIEW v1 AS SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1);
  3602. ERROR 42S22: Unknown column 'no_such_column' in 'where clause'
  3603. CREATE VIEW v2 AS SELECT * FROM t1 WHERE no_such_column = (SELECT 1);
  3604. ERROR 42S22: Unknown column 'no_such_column' in 'where clause'
  3605. SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1);
  3606. ERROR 42S22: Unknown column 'no_such_column' in 'IN/ALL/ANY subquery'
  3607. DROP TABLE t1;
  3608. create table t1 (i int, j bigint);
  3609. insert into t1 values (1, 2), (2, 2), (3, 2);
  3610. select * from (select min(i) from t1 where j=(select * from (select min(j) from t1) t2)) t3;
  3611. min(i)
  3612. 1
  3613. drop table t1;
  3614. CREATE TABLE t1 (i BIGINT UNSIGNED);
  3615. INSERT INTO t1 VALUES (10000000000000000000);
  3616. INSERT INTO t1 VALUES (1);
  3617. CREATE TABLE t2 (i BIGINT UNSIGNED);
  3618. INSERT INTO t2 VALUES (10000000000000000000);
  3619. INSERT INTO t2 VALUES (1);
  3620. /* simple test */
  3621. SELECT t1.i FROM t1 JOIN t2 ON t1.i = t2.i;
  3622. i
  3623. 10000000000000000000
  3624. 1
  3625. /* subquery test */
  3626. SELECT t1.i FROM t1 WHERE t1.i = (SELECT MAX(i) FROM t2);
  3627. i
  3628. 10000000000000000000
  3629. /* subquery test with cast*/
  3630. SELECT t1.i FROM t1 WHERE t1.i = CAST((SELECT MAX(i) FROM t2) AS UNSIGNED);
  3631. i
  3632. 10000000000000000000
  3633. DROP TABLE t1;
  3634. DROP TABLE t2;
  3635. CREATE TABLE t1 (
  3636. id bigint(20) unsigned NOT NULL auto_increment,
  3637. name varchar(255) NOT NULL,
  3638. PRIMARY KEY (id)
  3639. );
  3640. INSERT INTO t1 VALUES
  3641. (1, 'Balazs'), (2, 'Joe'), (3, 'Frank');
  3642. CREATE TABLE t2 (
  3643. id bigint(20) unsigned NOT NULL auto_increment,
  3644. mid bigint(20) unsigned NOT NULL,
  3645. date date NOT NULL,
  3646. PRIMARY KEY (id)
  3647. );
  3648. INSERT INTO t2 VALUES
  3649. (1, 1, '2006-03-30'), (2, 2, '2006-04-06'), (3, 3, '2006-04-13'),
  3650. (4, 2, '2006-04-20'), (5, 1, '2006-05-01');
  3651. SELECT *,
  3652. (SELECT date FROM t2 WHERE mid = t1.id
  3653. ORDER BY date DESC LIMIT 0, 1) AS date_last,
  3654. (SELECT date FROM t2 WHERE mid = t1.id
  3655. ORDER BY date DESC LIMIT 3, 1) AS date_next_to_last
  3656. FROM t1;
  3657. id name date_last date_next_to_last
  3658. 1 Balazs 2006-05-01 NULL
  3659. 2 Joe 2006-04-20 NULL
  3660. 3 Frank 2006-04-13 NULL
  3661. SELECT *,
  3662. (SELECT COUNT(*) FROM t2 WHERE mid = t1.id
  3663. ORDER BY date DESC LIMIT 1, 1) AS date_count
  3664. FROM t1;
  3665. id name date_count
  3666. 1 Balazs NULL
  3667. 2 Joe NULL
  3668. 3 Frank NULL
  3669. SELECT *,
  3670. (SELECT date FROM t2 WHERE mid = t1.id
  3671. ORDER BY date DESC LIMIT 0, 1) AS date_last,
  3672. (SELECT date FROM t2 WHERE mid = t1.id
  3673. ORDER BY date DESC LIMIT 1, 1) AS date_next_to_last
  3674. FROM t1;
  3675. id name date_last date_next_to_last
  3676. 1 Balazs 2006-05-01 2006-03-30
  3677. 2 Joe 2006-04-20 2006-04-06
  3678. 3 Frank 2006-04-13 NULL
  3679. DROP TABLE t1,t2;
  3680. CREATE TABLE t1 (
  3681. i1 int(11) NOT NULL default '0',
  3682. i2 int(11) NOT NULL default '0',
  3683. t datetime NOT NULL default '0000-00-00 00:00:00',
  3684. PRIMARY KEY (i1,i2,t)
  3685. );
  3686. INSERT INTO t1 VALUES
  3687. (24,1,'2005-03-03 16:31:31'),(24,1,'2005-05-27 12:40:07'),
  3688. (24,1,'2005-05-27 12:40:08'),(24,1,'2005-05-27 12:40:10'),
  3689. (24,1,'2005-05-27 12:40:25'),(24,1,'2005-05-27 12:40:30'),
  3690. (24,2,'2005-03-03 13:43:05'),(24,2,'2005-03-03 16:23:31'),
  3691. (24,2,'2005-03-03 16:31:30'),(24,2,'2005-05-27 12:37:02'),
  3692. (24,2,'2005-05-27 12:40:06');
  3693. CREATE TABLE t2 (
  3694. i1 int(11) NOT NULL default '0',
  3695. i2 int(11) NOT NULL default '0',
  3696. t datetime default NULL,
  3697. PRIMARY KEY (i1)
  3698. );
  3699. INSERT INTO t2 VALUES (24,1,'2006-06-20 12:29:40');
  3700. EXPLAIN
  3701. SELECT * FROM t1,t2
  3702. WHERE t1.t = (SELECT t1.t FROM t1
  3703. WHERE t1.t < t2.t AND t1.i2=1 AND t2.i1=t1.i1
  3704. ORDER BY t1.t DESC LIMIT 1);
  3705. id select_type table type possible_keys key key_len ref rows Extra
  3706. 1 PRIMARY t2 system NULL NULL NULL NULL 1
  3707. 1 PRIMARY t1 index NULL PRIMARY 16 NULL 11 Using where; Using index
  3708. 2 DEPENDENT SUBQUERY t1 range PRIMARY PRIMARY 16 NULL 5 Using where; Using index
  3709. SELECT * FROM t1,t2
  3710. WHERE t1.t = (SELECT t1.t FROM t1
  3711. WHERE t1.t < t2.t AND t1.i2=1 AND t2.i1=t1.i1
  3712. ORDER BY t1.t DESC LIMIT 1);
  3713. i1 i2 t i1 i2 t
  3714. 24 1 2005-05-27 12:40:30 24 1 2006-06-20 12:29:40
  3715. DROP TABLE t1, t2;
  3716. CREATE TABLE t1 (a VARCHAR(250), b INT auto_increment, PRIMARY KEY (b));
  3717. insert into t1 (a) values (FLOOR(rand() * 100));
  3718. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3719. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3720. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3721. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3722. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3723. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3724. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3725. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3726. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3727. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3728. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3729. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3730. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3731. SELECT a,
  3732. (SELECT REPEAT(' ',250) FROM t1 i1
  3733. WHERE i1.b=t1.a ORDER BY RAND() LIMIT 1) AS a
  3734. FROM t1 ORDER BY a LIMIT 5;
  3735. a a
  3736. 0 NULL
  3737. 0 NULL
  3738. 0 NULL
  3739. 0 NULL
  3740. 0 NULL
  3741. DROP TABLE t1;
  3742. CREATE TABLE t1 (a INT, b INT);
  3743. CREATE TABLE t2 (a INT);
  3744. INSERT INTO t2 values (1);
  3745. INSERT INTO t1 VALUES (1,1),(1,2),(2,3),(3,4);
  3746. SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a;
  3747. (SELECT COUNT(DISTINCT t1.b) from t2)
  3748. 2
  3749. 1
  3750. 1
  3751. SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
  3752. FROM t1 GROUP BY t1.a;
  3753. (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
  3754. 2
  3755. 1
  3756. 1
  3757. SELECT COUNT(DISTINCT t1.b), (SELECT COUNT(DISTINCT t1.b)) FROM t1 GROUP BY t1.a;
  3758. COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b))
  3759. 2 2
  3760. 1 1
  3761. 1 1
  3762. SELECT COUNT(DISTINCT t1.b),
  3763. (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
  3764. FROM t1 GROUP BY t1.a;
  3765. COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
  3766. 2 2
  3767. 1 1
  3768. 1 1
  3769. SELECT (
  3770. SELECT (
  3771. SELECT COUNT(DISTINCT t1.b)
  3772. )
  3773. )
  3774. FROM t1 GROUP BY t1.a;
  3775. (
  3776. SELECT (
  3777. SELECT COUNT(DISTINCT t1.b)
  3778. )
  3779. )
  3780. 2
  3781. 1
  3782. 1
  3783. SELECT (
  3784. SELECT (
  3785. SELECT (
  3786. SELECT COUNT(DISTINCT t1.b)
  3787. )
  3788. )
  3789. FROM t1 GROUP BY t1.a LIMIT 1)
  3790. FROM t1 t2
  3791. GROUP BY t2.a;
  3792. (
  3793. SELECT (
  3794. SELECT (
  3795. SELECT COUNT(DISTINCT t1.b)
  3796. )
  3797. )
  3798. FROM t1 GROUP BY t1.a LIMIT 1)
  3799. 2
  3800. 2
  3801. 2
  3802. DROP TABLE t1,t2;
  3803. CREATE TABLE t1 (a int, b int, PRIMARY KEY (b));
  3804. CREATE TABLE t2 (x int auto_increment, y int, z int,
  3805. PRIMARY KEY (x), FOREIGN KEY (y) REFERENCES t1 (b));
  3806. create table t3 (a int);
  3807. insert into t3 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
  3808. insert into t1 select RAND()*1000, A.a + 10*(B.a+10*(C.a+10*D.a))
  3809. from t3 A, t3 B, t3 C, t3 D where D.a<3;
  3810. insert into t2(y,z) select t1.b, RAND()*1000 from t1, t3;
  3811. SET SESSION sort_buffer_size = 32 * 1024;
  3812. SELECT SQL_NO_CACHE COUNT(*)
  3813. FROM (SELECT a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c
  3814. FROM t1) t;
  3815. COUNT(*)
  3816. 3000
  3817. SET SESSION sort_buffer_size = 8 * 1024 * 1024;
  3818. SELECT SQL_NO_CACHE COUNT(*)
  3819. FROM (SELECT a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c
  3820. FROM t1) t;
  3821. COUNT(*)
  3822. 3000
  3823. DROP TABLE t1,t2,t3;
  3824. CREATE TABLE t1 (id char(4) PRIMARY KEY, c int);
  3825. CREATE TABLE t2 (c int);
  3826. INSERT INTO t1 VALUES ('aa', 1);
  3827. INSERT INTO t2 VALUES (1);
  3828. SELECT * FROM t1
  3829. WHERE EXISTS (SELECT c FROM t2 WHERE c=1
  3830. UNION
  3831. SELECT c from t2 WHERE c=t1.c);
  3832. id c
  3833. aa 1
  3834. INSERT INTO t1 VALUES ('bb', 2), ('cc', 3), ('dd',1);
  3835. SELECT * FROM t1
  3836. WHERE EXISTS (SELECT c FROM t2 WHERE c=1
  3837. UNION
  3838. SELECT c from t2 WHERE c=t1.c);
  3839. id c
  3840. aa 1
  3841. bb 2
  3842. cc 3
  3843. dd 1
  3844. INSERT INTO t2 VALUES (2);
  3845. CREATE TABLE t3 (c int);
  3846. INSERT INTO t3 VALUES (1);
  3847. SELECT * FROM t1
  3848. WHERE EXISTS (SELECT t2.c FROM t2 JOIN t3 ON t2.c=t3.c WHERE t2.c=1
  3849. UNION
  3850. SELECT c from t2 WHERE c=t1.c);
  3851. id c
  3852. aa 1
  3853. bb 2
  3854. cc 3
  3855. dd 1
  3856. DROP TABLE t1,t2,t3;
  3857. CREATE TABLE t1(f1 int);
  3858. CREATE TABLE t2(f2 int, f21 int, f3 timestamp);
  3859. INSERT INTO t1 VALUES (1),(1),(2),(2);
  3860. INSERT INTO t2 VALUES (1,1,"2004-02-29 11:11:11"), (2,2,"2004-02-29 11:11:11");
  3861. SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1;
  3862. sq
  3863. 2
  3864. 4
  3865. SELECT (SELECT SUM(1) FROM t2 ttt GROUP BY t2.f3 LIMIT 1) AS tt FROM t2;
  3866. tt
  3867. 2
  3868. 2
  3869. PREPARE stmt1 FROM 'SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1';
  3870. EXECUTE stmt1;
  3871. sq
  3872. 2
  3873. 4
  3874. EXECUTE stmt1;
  3875. sq
  3876. 2
  3877. 4
  3878. DEALLOCATE PREPARE stmt1;
  3879. SELECT f2, AVG(f21),
  3880. (SELECT t.f3 FROM t2 AS t WHERE t2.f2=t.f2 AND t.f3=MAX(t2.f3)) AS test
  3881. FROM t2 GROUP BY f2;
  3882. f2 AVG(f21) test
  3883. 1 1.0000 2004-02-29 11:11:11
  3884. 2 2.0000 2004-02-29 11:11:11
  3885. DROP TABLE t1,t2;
  3886. CREATE TABLE t1 (a int, b INT, c CHAR(10) NOT NULL);
  3887. INSERT INTO t1 VALUES
  3888. (1,1,'a'), (1,2,'b'), (1,3,'c'), (1,4,'d'), (1,5,'e'),
  3889. (2,1,'f'), (2,2,'g'), (2,3,'h'), (3,4,'i'), (3,3,'j'),
  3890. (3,2,'k'), (3,1,'l'), (1,9,'m');
  3891. SELECT a, MAX(b),
  3892. (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b)) AS test
  3893. FROM t1 GROUP BY a;
  3894. a MAX(b) test
  3895. 1 9 m
  3896. 2 3 h
  3897. 3 4 i
  3898. DROP TABLE t1;
  3899. DROP TABLE IF EXISTS t1;
  3900. DROP TABLE IF EXISTS t2;
  3901. DROP TABLE IF EXISTS t1xt2;
  3902. CREATE TABLE t1 (
  3903. id_1 int(5) NOT NULL,
  3904. t varchar(4) DEFAULT NULL
  3905. );
  3906. CREATE TABLE t2 (
  3907. id_2 int(5) NOT NULL,
  3908. t varchar(4) DEFAULT NULL
  3909. );
  3910. CREATE TABLE t1xt2 (
  3911. id_1 int(5) NOT NULL,
  3912. id_2 int(5) NOT NULL
  3913. );
  3914. INSERT INTO t1 VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd');
  3915. INSERT INTO t2 VALUES (2, 'bb'), (3, 'cc'), (4, 'dd'), (12, 'aa');
  3916. INSERT INTO t1xt2 VALUES (2, 2), (3, 3), (4, 4);
  3917. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3918. (12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
  3919. id_1
  3920. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3921. (12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
  3922. id_1
  3923. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3924. (12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
  3925. id_1
  3926. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3927. (12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
  3928. id_1
  3929. 1
  3930. 2
  3931. 3
  3932. 4
  3933. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3934. (12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1)));
  3935. id_1
  3936. 1
  3937. 2
  3938. 3
  3939. 4
  3940. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3941. (12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1))));
  3942. id_1
  3943. 1
  3944. 2
  3945. 3
  3946. 4
  3947. insert INTO t1xt2 VALUES (1, 12);
  3948. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3949. (12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
  3950. id_1
  3951. 1
  3952. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3953. (12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
  3954. id_1
  3955. 1
  3956. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3957. (12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
  3958. id_1
  3959. 1
  3960. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3961. (12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
  3962. id_1
  3963. 2
  3964. 3
  3965. 4
  3966. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3967. (12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
  3968. id_1
  3969. 2
  3970. 3
  3971. 4
  3972. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3973. (12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
  3974. id_1
  3975. 2
  3976. 3
  3977. 4
  3978. insert INTO t1xt2 VALUES (2, 12);
  3979. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3980. (12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
  3981. id_1
  3982. 1
  3983. 2
  3984. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3985. (12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
  3986. id_1
  3987. 1
  3988. 2
  3989. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3990. (12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
  3991. id_1
  3992. 1
  3993. 2
  3994. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3995. (12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
  3996. id_1
  3997. 3
  3998. 4
  3999. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  4000. (12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
  4001. id_1
  4002. 3
  4003. 4
  4004. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  4005. (12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
  4006. id_1
  4007. 3
  4008. 4
  4009. DROP TABLE t1;
  4010. DROP TABLE t2;
  4011. DROP TABLE t1xt2;
  4012. CREATE TABLE t1 (a int);
  4013. INSERT INTO t1 VALUES (3), (1), (2);
  4014. SELECT 'this is ' 'a test.' AS col1, a AS col2 FROM t1;
  4015. col1 col2
  4016. this is a test. 3
  4017. this is a test. 1
  4018. this is a test. 2
  4019. SELECT * FROM (SELECT 'this is ' 'a test.' AS col1, a AS t2 FROM t1) t;
  4020. col1 t2
  4021. this is a test. 3
  4022. this is a test. 1
  4023. this is a test. 2
  4024. DROP table t1;
  4025. CREATE TABLE t1 (a int, b int);
  4026. CREATE TABLE t2 (m int, n int);
  4027. INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
  4028. INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
  4029. SELECT COUNT(*), a,
  4030. (SELECT m FROM t2 WHERE m = count(*) LIMIT 1)
  4031. FROM t1 GROUP BY a;
  4032. COUNT(*) a (SELECT m FROM t2 WHERE m = count(*) LIMIT 1)
  4033. 2 2 2
  4034. 3 3 3
  4035. 1 4 1
  4036. SELECT COUNT(*), a,
  4037. (SELECT MIN(m) FROM t2 WHERE m = count(*))
  4038. FROM t1 GROUP BY a;
  4039. COUNT(*) a (SELECT MIN(m) FROM t2 WHERE m = count(*))
  4040. 2 2 2
  4041. 3 3 3
  4042. 1 4 1
  4043. SELECT COUNT(*), a
  4044. FROM t1 GROUP BY a
  4045. HAVING (SELECT MIN(m) FROM t2 WHERE m = count(*)) > 1;
  4046. COUNT(*) a
  4047. 2 2
  4048. 3 3
  4049. DROP TABLE t1,t2;
  4050. CREATE TABLE t1 (a int, b int);
  4051. CREATE TABLE t2 (m int, n int);
  4052. INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
  4053. INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
  4054. SELECT COUNT(*) c, a,
  4055. (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a)
  4056. FROM t1 GROUP BY a;
  4057. c a (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a)
  4058. 2 2 2
  4059. 3 3 3
  4060. 1 4 1,1
  4061. SELECT COUNT(*) c, a,
  4062. (SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a)
  4063. FROM t1 GROUP BY a;
  4064. c a (SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a)
  4065. 2 2 3
  4066. 3 3 4
  4067. 1 4 2,2
  4068. DROP table t1,t2;
  4069. CREATE TABLE t1 (a int, b INT, d INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b));
  4070. INSERT INTO t1 VALUES (1,1,0,'a'), (1,2,0,'b'), (1,3,0,'c'), (1,4,0,'d'),
  4071. (1,5,0,'e'), (2,1,0,'f'), (2,2,0,'g'), (2,3,0,'h'), (3,4,0,'i'), (3,3,0,'j'),
  4072. (3,2,0,'k'), (3,1,0,'l'), (1,9,0,'m'), (1,0,10,'n'), (2,0,5,'o'), (3,0,7,'p');
  4073. SELECT a, MAX(b),
  4074. (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b + 0)) as test
  4075. FROM t1 GROUP BY a;
  4076. a MAX(b) test
  4077. 1 9 m
  4078. 2 3 h
  4079. 3 4 i
  4080. SELECT a x, MAX(b),
  4081. (SELECT t.c FROM t1 AS t WHERE x=t.a AND t.b=MAX(t1.b + 0)) as test
  4082. FROM t1 GROUP BY a;
  4083. x MAX(b) test
  4084. 1 9 m
  4085. 2 3 h
  4086. 3 4 i
  4087. SELECT a, AVG(b),
  4088. (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=AVG(t1.b)) AS test
  4089. FROM t1 WHERE t1.d=0 GROUP BY a;
  4090. a AVG(b) test
  4091. 1 4.0000 d
  4092. 2 2.0000 g
  4093. 3 2.5000 NULL
  4094. SELECT tt.a,
  4095. (SELECT (SELECT c FROM t1 as t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
  4096. LIMIT 1) FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test
  4097. FROM t1 as tt;
  4098. a test
  4099. 1 n
  4100. 1 n
  4101. 1 n
  4102. 1 n
  4103. 1 n
  4104. 1 n
  4105. 1 n
  4106. 2 o
  4107. 2 o
  4108. 2 o
  4109. 2 o
  4110. 3 p
  4111. 3 p
  4112. 3 p
  4113. 3 p
  4114. 3 p
  4115. SELECT tt.a,
  4116. (SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
  4117. LIMIT 1)
  4118. FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test
  4119. FROM t1 as tt GROUP BY tt.a;
  4120. a test
  4121. 1 n
  4122. 2 o
  4123. 3 p
  4124. SELECT tt.a, MAX(
  4125. (SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
  4126. LIMIT 1)
  4127. FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1)) as test
  4128. FROM t1 as tt GROUP BY tt.a;
  4129. a test
  4130. 1 n
  4131. 2 o
  4132. 3 p
  4133. DROP TABLE t1;
  4134. CREATE TABLE t1 (a int, b int);
  4135. INSERT INTO t1 VALUES (2,22),(1,11),(2,22);
  4136. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
  4137. a
  4138. 1
  4139. 2
  4140. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
  4141. a
  4142. SELECT a FROM t1 t0
  4143. WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
  4144. a
  4145. 1
  4146. 2
  4147. SET @@sql_mode='ansi';
  4148. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
  4149. ERROR HY000: Invalid use of group function
  4150. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
  4151. ERROR HY000: Invalid use of group function
  4152. SELECT a FROM t1 t0
  4153. WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
  4154. ERROR HY000: Invalid use of group function
  4155. SET @@sql_mode=default;
  4156. DROP TABLE t1;
  4157. CREATE TABLE t1 (a INT);
  4158. INSERT INTO t1 values (1),(1),(1),(1);
  4159. CREATE TABLE t2 (x INT);
  4160. INSERT INTO t1 values (1000),(1001),(1002);
  4161. SELECT SUM( (SELECT COUNT(a) FROM t2) ) FROM t1;
  4162. ERROR HY000: Invalid use of group function
  4163. SELECT SUM( (SELECT SUM(COUNT(a)) FROM t2) ) FROM t1;
  4164. ERROR HY000: Invalid use of group function
  4165. SELECT COUNT(1) FROM DUAL;
  4166. COUNT(1)
  4167. 1
  4168. SELECT SUM( (SELECT AVG( (SELECT t1.a FROM t2) ) FROM DUAL) ) FROM t1;
  4169. ERROR HY000: Invalid use of group function
  4170. SELECT
  4171. SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING t1.a < 12) ) FROM t2) )
  4172. FROM t1;
  4173. ERROR HY000: Invalid use of group function
  4174. SELECT t1.a as XXA,
  4175. SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING XXA < 12) ) FROM t2) )
  4176. FROM t1;
  4177. ERROR HY000: Invalid use of group function
  4178. DROP TABLE t1,t2;
  4179. CREATE TABLE t1 (a int, b int, KEY (a));
  4180. INSERT INTO t1 VALUES (1,1),(2,1);
  4181. EXPLAIN SELECT 1 FROM t1 WHERE a = (SELECT COUNT(*) FROM t1 GROUP BY b);
  4182. id select_type table type possible_keys key key_len ref rows Extra
  4183. 1 PRIMARY t1 ref a a 5 const 0 Using where; Using index
  4184. 2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using temporary; Using filesort
  4185. DROP TABLE t1;
  4186. CREATE TABLE t1 (id int NOT NULL, st CHAR(2), INDEX idx(id));
  4187. INSERT INTO t1 VALUES
  4188. (3,'FL'), (2,'GA'), (4,'FL'), (1,'GA'), (5,'NY'), (7,'FL'), (6,'NY');
  4189. CREATE TABLE t2 (id int NOT NULL, INDEX idx(id));
  4190. INSERT INTO t2 VALUES (7), (5), (1), (3);
  4191. SELECT id, st FROM t1
  4192. WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id);
  4193. id st
  4194. 3 FL
  4195. 1 GA
  4196. 7 FL
  4197. SELECT id, st FROM t1
  4198. WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id)
  4199. GROUP BY id;
  4200. id st
  4201. 1 GA
  4202. 3 FL
  4203. 7 FL
  4204. SELECT id, st FROM t1
  4205. WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id);
  4206. id st
  4207. 2 GA
  4208. 4 FL
  4209. SELECT id, st FROM t1
  4210. WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id)
  4211. GROUP BY id;
  4212. id st
  4213. 2 GA
  4214. 4 FL
  4215. DROP TABLE t1,t2;
  4216. CREATE TABLE t1 (a int);
  4217. INSERT INTO t1 VALUES (1), (2);
  4218. EXPLAIN EXTENDED
  4219. SELECT * FROM (SELECT count(*) FROM t1 GROUP BY a) as res;
  4220. id select_type table type possible_keys key key_len ref rows filtered Extra
  4221. 1 PRIMARY <derived2> ALL NULL NULL NULL NULL 2 100.00
  4222. 2 DERIVED t1 ALL NULL NULL NULL NULL 2 100.00 Using temporary; Using filesort
  4223. Warnings:
  4224. Note 1003 select `res`.`count(*)` AS `count(*)` from (select count(0) AS `count(*)` from `test`.`t1` group by `test`.`t1`.`a`) `res`
  4225. DROP TABLE t1;
  4226. CREATE TABLE t1 (
  4227. a varchar(255) default NULL,
  4228. b timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  4229. INDEX idx(a,b)
  4230. );
  4231. CREATE TABLE t2 (
  4232. a varchar(255) default NULL
  4233. );
  4234. INSERT INTO t1 VALUES ('abcdefghijk','2007-05-07 06:00:24');
  4235. INSERT INTO t1 SELECT * FROM t1;
  4236. INSERT INTO t1 SELECT * FROM t1;
  4237. INSERT INTO t1 SELECT * FROM t1;
  4238. INSERT INTO t1 SELECT * FROM t1;
  4239. INSERT INTO t1 SELECT * FROM t1;
  4240. INSERT INTO t1 SELECT * FROM t1;
  4241. INSERT INTO t1 SELECT * FROM t1;
  4242. INSERT INTO t1 SELECT * FROM t1;
  4243. INSERT INTO `t1` VALUES ('asdf','2007-02-08 01:11:26');
  4244. INSERT INTO `t2` VALUES ('abcdefghijk');
  4245. INSERT INTO `t2` VALUES ('asdf');
  4246. SET session sort_buffer_size=8192;
  4247. SELECT (SELECT 1 FROM t1 WHERE t1.a=t2.a ORDER BY t1.b LIMIT 1) AS d1 FROM t2;
  4248. d1
  4249. 1
  4250. 1
  4251. DROP TABLE t1,t2;
  4252. CREATE TABLE t1 (a INTEGER, b INTEGER);
  4253. CREATE TABLE t2 (x INTEGER);
  4254. INSERT INTO t1 VALUES (1,11), (2,22), (2,22);
  4255. INSERT INTO t2 VALUES (1), (2);
  4256. SELECT a, COUNT(b), (SELECT COUNT(b) FROM t2) FROM t1 GROUP BY a;
  4257. ERROR 21000: Subquery returns more than 1 row
  4258. SELECT a, COUNT(b), (SELECT COUNT(b)+0 FROM t2) FROM t1 GROUP BY a;
  4259. ERROR 21000: Subquery returns more than 1 row
  4260. SELECT (SELECT SUM(t1.a)/AVG(t2.x) FROM t2) FROM t1;
  4261. (SELECT SUM(t1.a)/AVG(t2.x) FROM t2)
  4262. 3.3333
  4263. DROP TABLE t1,t2;
  4264. CREATE TABLE t1 (a INT, b INT);
  4265. INSERT INTO t1 VALUES (1, 2), (1,3), (1,4), (2,1), (2,2);
  4266. SELECT a1.a, COUNT(*) FROM t1 a1 WHERE a1.a = 1
  4267. AND EXISTS( SELECT a2.a FROM t1 a2 WHERE a2.a = a1.a)
  4268. GROUP BY a1.a;
  4269. a COUNT(*)
  4270. 1 3
  4271. DROP TABLE t1;
  4272. CREATE TABLE t1 (a INT);
  4273. CREATE TABLE t2 (a INT);
  4274. INSERT INTO t1 VALUES (1),(2);
  4275. INSERT INTO t2 VALUES (1),(2);
  4276. SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=0) FROM t1;
  4277. (SELECT SUM(t1.a) FROM t2 WHERE a=0)
  4278. NULL
  4279. SELECT (SELECT SUM(t1.a) FROM t2 WHERE a!=0) FROM t1;
  4280. ERROR 21000: Subquery returns more than 1 row
  4281. SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=1) FROM t1;
  4282. (SELECT SUM(t1.a) FROM t2 WHERE a=1)
  4283. 3
  4284. DROP TABLE t1,t2;
  4285. CREATE TABLE t1 (a1 INT, a2 INT);
  4286. CREATE TABLE t2 (b1 INT, b2 INT);
  4287. INSERT INTO t1 VALUES (100, 200);
  4288. INSERT INTO t1 VALUES (101, 201);
  4289. INSERT INTO t2 VALUES (101, 201);
  4290. INSERT INTO t2 VALUES (103, 203);
  4291. SELECT ((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL FROM t1;
  4292. ((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL
  4293. 0
  4294. 0
  4295. DROP TABLE t1, t2;
  4296. CREATE TABLE t1 (s1 BINARY(5), s2 VARBINARY(5));
  4297. INSERT INTO t1 VALUES (0x41,0x41), (0x42,0x42), (0x43,0x43);
  4298. SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
  4299. s1 s2
  4300. SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
  4301. s1 s2
  4302. CREATE INDEX I1 ON t1 (s1);
  4303. CREATE INDEX I2 ON t1 (s2);
  4304. SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
  4305. s1 s2
  4306. SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
  4307. s1 s2
  4308. TRUNCATE t1;
  4309. INSERT INTO t1 VALUES (0x41,0x41);
  4310. SELECT * FROM t1 WHERE s1 = (SELECT s2 FROM t1);
  4311. s1 s2
  4312. DROP TABLE t1;
  4313. CREATE TABLE t1 (a1 VARBINARY(2) NOT NULL DEFAULT '0', PRIMARY KEY (a1));
  4314. CREATE TABLE t2 (a2 BINARY(2) default '0', INDEX (a2));
  4315. CREATE TABLE t3 (a3 BINARY(2) default '0');
  4316. INSERT INTO t1 VALUES (1),(2),(3),(4);
  4317. INSERT INTO t2 VALUES (1),(2),(3);
  4318. INSERT INTO t3 VALUES (1),(2),(3);
  4319. SELECT LEFT(t2.a2, 1) FROM t2,t3 WHERE t3.a3=t2.a2;
  4320. LEFT(t2.a2, 1)
  4321. 1
  4322. 2
  4323. 3
  4324. SELECT t1.a1, t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2) FROM t1;
  4325. a1 t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2)
  4326. 1 0
  4327. 2 0
  4328. 3 0
  4329. 4 0
  4330. DROP TABLE t1,t2,t3;
  4331. CREATE TABLE t1 (a1 BINARY(3) PRIMARY KEY, b1 VARBINARY(3));
  4332. CREATE TABLE t2 (a2 VARBINARY(3) PRIMARY KEY);
  4333. CREATE TABLE t3 (a3 VARBINARY(3) PRIMARY KEY);
  4334. INSERT INTO t1 VALUES (1,10), (2,20), (3,30), (4,40);
  4335. INSERT INTO t2 VALUES (2), (3), (4), (5);
  4336. INSERT INTO t3 VALUES (10), (20), (30);
  4337. SELECT LEFT(t1.a1,1) FROM t1,t3 WHERE t1.b1=t3.a3;
  4338. LEFT(t1.a1,1)
  4339. 1
  4340. 2
  4341. 3
  4342. SELECT a2 FROM t2 WHERE t2.a2 IN (SELECT t1.a1 FROM t1,t3 WHERE t1.b1=t3.a3);
  4343. a2
  4344. DROP TABLE t1, t2, t3;
  4345. SET @save_optimizer_switch=@@optimizer_switch;
  4346. SET optimizer_switch='semijoin_with_cache=off';
  4347. SET optimizer_switch='materialization=off';
  4348. CREATE TABLE t1 (a CHAR(1), b VARCHAR(10));
  4349. INSERT INTO t1 VALUES ('a', 'aa');
  4350. INSERT INTO t1 VALUES ('a', 'aaa');
  4351. SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
  4352. a b
  4353. CREATE INDEX I1 ON t1 (a);
  4354. CREATE INDEX I2 ON t1 (b);
  4355. EXPLAIN SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
  4356. id select_type table type possible_keys key key_len ref rows Extra
  4357. 1 PRIMARY t1 ALL I2 NULL NULL NULL 2 Using where
  4358. 1 PRIMARY t1 ref I1 I1 2 test.t1.b 2 Using where; Using index; FirstMatch(t1)
  4359. SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
  4360. a b
  4361. CREATE TABLE t2 (a VARCHAR(1), b VARCHAR(10));
  4362. INSERT INTO t2 SELECT * FROM t1;
  4363. CREATE INDEX I1 ON t2 (a);
  4364. CREATE INDEX I2 ON t2 (b);
  4365. EXPLAIN SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
  4366. id select_type table type possible_keys key key_len ref rows Extra
  4367. 1 PRIMARY t2 ALL I2 NULL NULL NULL 2 Using where
  4368. 1 PRIMARY t2 ref I1 I1 4 test.t2.b 2 Using where; Using index; FirstMatch(t2)
  4369. SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
  4370. a b
  4371. EXPLAIN
  4372. SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
  4373. id select_type table type possible_keys key key_len ref rows Extra
  4374. 1 PRIMARY t1 ALL I2 NULL NULL NULL 2 Using where
  4375. 1 PRIMARY t1 ref I1 I1 2 test.t1.b 2 Using where; Using index; FirstMatch(t1)
  4376. SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
  4377. a b
  4378. DROP TABLE t1,t2;
  4379. SET optimizer_switch= @save_optimizer_switch;
  4380. CREATE TABLE t1(a INT, b INT);
  4381. INSERT INTO t1 VALUES (1,1), (1,2), (2,3), (2,4);
  4382. EXPLAIN
  4383. SELECT a AS out_a, MIN(b) FROM t1
  4384. WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
  4385. GROUP BY a;
  4386. ERROR 42S22: Unknown column 'out_a' in 'where clause'
  4387. SELECT a AS out_a, MIN(b) FROM t1
  4388. WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
  4389. GROUP BY a;
  4390. ERROR 42S22: Unknown column 'out_a' in 'where clause'
  4391. EXPLAIN
  4392. SELECT a AS out_a, MIN(b) FROM t1 t1_outer
  4393. WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
  4394. GROUP BY a;
  4395. id select_type table type possible_keys key key_len ref rows Extra
  4396. 1 PRIMARY t1_outer ALL NULL NULL NULL NULL 4 Using where; Using temporary; Using filesort
  4397. 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 4 Using where
  4398. SELECT a AS out_a, MIN(b) FROM t1 t1_outer
  4399. WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
  4400. GROUP BY a;
  4401. out_a MIN(b)
  4402. 1 2
  4403. 2 4
  4404. DROP TABLE t1;
  4405. create table t0(a int);
  4406. insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
  4407. create table t1(f11 int, f12 int);
  4408. create table t2(f21 int unsigned not null, f22 int, f23 varchar(10));
  4409. insert into t1 values(1,1),(2,2), (3, 3);
  4410. insert into t2
  4411. select -1 , (@a:=(A.a + 10 * (B.a + 10 * (C.a+10*D.a))))/5000 + 1, @a
  4412. from t0 A, t0 B, t0 C, t0 D;
  4413. set session sort_buffer_size= 33*1024;
  4414. select count(*) from t1 where f12 =
  4415. (select f22 from t2 where f22 = f12 order by f21 desc, f22, f23 limit 1);
  4416. count(*)
  4417. 3
  4418. drop table t0,t1,t2;
  4419. CREATE TABLE t4 (
  4420. f7 varchar(32) collate utf8_bin NOT NULL default '',
  4421. f10 varchar(32) collate utf8_bin default NULL,
  4422. PRIMARY KEY (f7)
  4423. );
  4424. INSERT INTO t4 VALUES(1,1), (2,null);
  4425. CREATE TABLE t2 (
  4426. f4 varchar(32) collate utf8_bin NOT NULL default '',
  4427. f2 varchar(50) collate utf8_bin default NULL,
  4428. f3 varchar(10) collate utf8_bin default NULL,
  4429. PRIMARY KEY (f4),
  4430. UNIQUE KEY uk1 (f2)
  4431. );
  4432. INSERT INTO t2 VALUES(1,1,null), (2,2,null);
  4433. CREATE TABLE t1 (
  4434. f8 varchar(32) collate utf8_bin NOT NULL default '',
  4435. f1 varchar(10) collate utf8_bin default NULL,
  4436. f9 varchar(32) collate utf8_bin default NULL,
  4437. PRIMARY KEY (f8)
  4438. );
  4439. INSERT INTO t1 VALUES (1,'P',1), (2,'P',1), (3,'R',2);
  4440. CREATE TABLE t3 (
  4441. f6 varchar(32) collate utf8_bin NOT NULL default '',
  4442. f5 varchar(50) collate utf8_bin default NULL,
  4443. PRIMARY KEY (f6)
  4444. );
  4445. INSERT INTO t3 VALUES (1,null), (2,null);
  4446. SELECT
  4447. IF(t1.f1 = 'R', a1.f2, t2.f2) AS a4,
  4448. IF(t1.f1 = 'R', a1.f3, t2.f3) AS f3,
  4449. SUM(
  4450. IF(
  4451. (SELECT VPC.f2
  4452. FROM t2 VPC, t4 a2, t2 a3
  4453. WHERE
  4454. VPC.f4 = a2.f10 AND a3.f2 = a4
  4455. LIMIT 1) IS NULL,
  4456. 0,
  4457. t3.f5
  4458. )
  4459. ) AS a6
  4460. FROM
  4461. t2, t3, t1 JOIN t2 a1 ON t1.f9 = a1.f4
  4462. GROUP BY a4;
  4463. a4 f3 a6
  4464. 1 NULL NULL
  4465. 2 NULL NULL
  4466. DROP TABLE t1, t2, t3, t4;
  4467. create table t1 (a float(5,4) zerofill);
  4468. create table t2 (a float(5,4),b float(2,0));
  4469. select t1.a from t1 where
  4470. t1.a= (select b from t2 limit 1) and not
  4471. t1.a= (select a from t2 limit 1) ;
  4472. a
  4473. drop table t1, t2;
  4474. CREATE TABLE t1 (a INT);
  4475. INSERT INTO t1 VALUES (1),(2);
  4476. SET @save_join_cache_level=@@join_cache_level;
  4477. SET join_cache_level=0;
  4478. EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT min(a) FROM t1 GROUP BY a);
  4479. id select_type table type possible_keys key key_len ref rows filtered Extra
  4480. 1 PRIMARY <subquery2> const distinct_key distinct_key 4 const 1 100.00
  4481. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  4482. 2 MATERIALIZED t1 ALL NULL NULL NULL NULL 2 100.00 Using temporary
  4483. Warnings:
  4484. Note 1003 select 1 AS `1` from <materialize> (select min(`test`.`t1`.`a`) from `test`.`t1` group by `test`.`t1`.`a`) join `test`.`t1` where (`<subquery2>`.`min(a)` = 1)
  4485. EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT min(a) FROM t1 WHERE a > 3 GROUP BY a);
  4486. id select_type table type possible_keys key key_len ref rows filtered Extra
  4487. 1 PRIMARY <subquery2> const distinct_key distinct_key 4 const 1 100.00
  4488. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  4489. 2 MATERIALIZED t1 ALL NULL NULL NULL NULL 2 100.00 Using where; Using temporary
  4490. Warnings:
  4491. Note 1003 select 1 AS `1` from <materialize> (select min(`test`.`t1`.`a`) from `test`.`t1` where (`test`.`t1`.`a` > 3) group by `test`.`t1`.`a`) join `test`.`t1` where (`<subquery2>`.`min(a)` = 1)
  4492. SET join_cache_level=@save_join_cache_level;
  4493. DROP TABLE t1;
  4494. #
  4495. # Bug#45061: Incorrectly market field caused wrong result.
  4496. #
  4497. CREATE TABLE `C` (
  4498. `int_nokey` int(11) NOT NULL,
  4499. `int_key` int(11) NOT NULL,
  4500. KEY `int_key` (`int_key`)
  4501. );
  4502. INSERT INTO `C` VALUES (9,9), (0,0), (8,6), (3,6), (7,6), (0,4),
  4503. (1,7), (9,4), (0,8), (9,4), (0,7), (5,5), (0,0), (8,5), (8,7),
  4504. (5,2), (1,8), (7,0), (0,9), (9,5);
  4505. SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`);
  4506. int_nokey int_key
  4507. 9 9
  4508. 0 0
  4509. 5 5
  4510. 0 0
  4511. EXPLAIN EXTENDED SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`);
  4512. id select_type table type possible_keys key key_len ref rows filtered Extra
  4513. 1 PRIMARY C ALL NULL NULL NULL NULL 20 100.00 Using where
  4514. DROP TABLE C;
  4515. # End of test for bug#45061.
  4516. #
  4517. # Bug #46749: Segfault in add_key_fields() with outer subquery level
  4518. # field references
  4519. #
  4520. CREATE TABLE t1 (
  4521. a int,
  4522. b int,
  4523. UNIQUE (a), KEY (b)
  4524. );
  4525. INSERT INTO t1 VALUES (1,1), (2,1);
  4526. CREATE TABLE st1 like t1;
  4527. INSERT INTO st1 VALUES (1,1), (2,1);
  4528. CREATE TABLE st2 like t1;
  4529. INSERT INTO st2 VALUES (1,1), (2,1);
  4530. EXPLAIN
  4531. SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
  4532. FROM t1
  4533. WHERE a = 230;
  4534. id select_type table type possible_keys key key_len ref rows Extra
  4535. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  4536. 2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  4537. SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
  4538. FROM t1
  4539. WHERE a = 230;
  4540. MAX(b) (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
  4541. NULL 0
  4542. DROP TABLE t1, st1, st2;
  4543. #
  4544. # Bug #48709: Assertion failed in sql_select.cc:11782:
  4545. # int join_read_key(JOIN_TAB*)
  4546. #
  4547. CREATE TABLE t1 (pk int PRIMARY KEY, int_key int);
  4548. INSERT INTO t1 VALUES (10,1), (14,1);
  4549. CREATE TABLE t2 (pk int PRIMARY KEY, int_key int);
  4550. INSERT INTO t2 VALUES (3,3), (5,NULL), (7,3);
  4551. # should have eq_ref for t1
  4552. EXPLAIN
  4553. SELECT * FROM t2 outr
  4554. WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2)
  4555. ORDER BY outr.pk;
  4556. id select_type table type possible_keys key key_len ref rows Extra
  4557. x x outr ALL x x x x x x
  4558. x x t1 eq_ref x x x x x x
  4559. x x t2 index x x x x x x
  4560. # should not crash on debug binaries
  4561. SELECT * FROM t2 outr
  4562. WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2)
  4563. ORDER BY outr.pk;
  4564. pk int_key
  4565. 3 3
  4566. 7 3
  4567. DROP TABLE t1,t2;
  4568. #
  4569. # Bug#12329653
  4570. # EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY
  4571. #
  4572. CREATE TABLE t1(a1 int);
  4573. INSERT INTO t1 VALUES (1),(2);
  4574. SELECT @@session.sql_mode INTO @old_sql_mode;
  4575. SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
  4576. EXPLAIN EXTENDED
  4577. SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
  4578. id select_type table type possible_keys key key_len ref rows filtered Extra
  4579. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  4580. 2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00
  4581. Warnings:
  4582. Note 1003 select 1 AS `1` from `test`.`t1` where <nop>(<in_optimizer>(1,((select max(`test`.`t1`.`a1`) from `test`.`t1`) > 1)))
  4583. SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
  4584. 1
  4585. 1
  4586. 1
  4587. PREPARE stmt FROM
  4588. 'SELECT 1 UNION ALL
  4589. SELECT 1 FROM t1
  4590. ORDER BY
  4591. (SELECT 1 FROM t1 AS t1_0
  4592. WHERE 1 < SOME (SELECT a1 FROM t1)
  4593. )' ;
  4594. EXECUTE stmt ;
  4595. ERROR 21000: Subquery returns more than 1 row
  4596. EXECUTE stmt ;
  4597. ERROR 21000: Subquery returns more than 1 row
  4598. SET SESSION sql_mode=@old_sql_mode;
  4599. DEALLOCATE PREPARE stmt;
  4600. DROP TABLE t1;
  4601. End of 5.0 tests.
  4602. create table t_out (subcase char(3),
  4603. a1 char(2), b1 char(2), c1 char(2));
  4604. create table t_in (a2 char(2), b2 char(2), c2 char(2));
  4605. insert into t_out values ('A.1','2a', NULL, '2a');
  4606. insert into t_out values ('A.3', '2a', NULL, '2a');
  4607. insert into t_out values ('A.4', '2a', NULL, 'xx');
  4608. insert into t_out values ('B.1', '2a', '2a', '2a');
  4609. insert into t_out values ('B.2', '2a', '2a', '2a');
  4610. insert into t_out values ('B.3', '3a', 'xx', '3a');
  4611. insert into t_out values ('B.4', 'xx', '3a', '3a');
  4612. insert into t_in values ('1a', '1a', '1a');
  4613. insert into t_in values ('2a', '2a', '2a');
  4614. insert into t_in values (NULL, '2a', '2a');
  4615. insert into t_in values ('3a', NULL, '3a');
  4616. Test general IN semantics (not top-level)
  4617. case A.1
  4618. select subcase,
  4619. (a1, b1, c1) IN (select * from t_in where a2 = 'no_match') pred_in,
  4620. (a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in
  4621. from t_out where subcase = 'A.1';
  4622. subcase pred_in pred_not_in
  4623. A.1 0 1
  4624. case A.2 - impossible
  4625. case A.3
  4626. select subcase,
  4627. (a1, b1, c1) IN (select * from t_in) pred_in,
  4628. (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
  4629. from t_out where subcase = 'A.3';
  4630. subcase pred_in pred_not_in
  4631. A.3 NULL NULL
  4632. case A.4
  4633. select subcase,
  4634. (a1, b1, c1) IN (select * from t_in) pred_in,
  4635. (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
  4636. from t_out where subcase = 'A.4';
  4637. subcase pred_in pred_not_in
  4638. A.4 0 1
  4639. case B.1
  4640. select subcase,
  4641. (a1, b1, c1) IN (select * from t_in where a2 = 'no_match') pred_in,
  4642. (a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in
  4643. from t_out where subcase = 'B.1';
  4644. subcase pred_in pred_not_in
  4645. B.1 0 1
  4646. case B.2
  4647. select subcase,
  4648. (a1, b1, c1) IN (select * from t_in) pred_in,
  4649. (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
  4650. from t_out where subcase = 'B.2';
  4651. subcase pred_in pred_not_in
  4652. B.2 1 0
  4653. case B.3
  4654. select subcase,
  4655. (a1, b1, c1) IN (select * from t_in) pred_in,
  4656. (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
  4657. from t_out where subcase = 'B.3';
  4658. subcase pred_in pred_not_in
  4659. B.3 NULL NULL
  4660. case B.4
  4661. select subcase,
  4662. (a1, b1, c1) IN (select * from t_in) pred_in,
  4663. (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
  4664. from t_out where subcase = 'B.4';
  4665. subcase pred_in pred_not_in
  4666. B.4 0 1
  4667. Test IN as top-level predicate, and
  4668. as non-top level for cases A.3, B.3 (the only cases with NULL result).
  4669. case A.1
  4670. select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
  4671. where subcase = 'A.1' and
  4672. (a1, b1, c1) IN (select * from t_in where a1 = 'no_match');
  4673. pred_in
  4674. F
  4675. select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
  4676. where subcase = 'A.1' and
  4677. (a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match');
  4678. pred_not_in
  4679. T
  4680. select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
  4681. where subcase = 'A.1' and
  4682. NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match'));
  4683. not_pred_in
  4684. T
  4685. case A.3
  4686. select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
  4687. where subcase = 'A.3' and
  4688. (a1, b1, c1) IN (select * from t_in);
  4689. pred_in
  4690. F
  4691. select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
  4692. where subcase = 'A.3' and
  4693. (a1, b1, c1) NOT IN (select * from t_in);
  4694. pred_not_in
  4695. F
  4696. select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
  4697. where subcase = 'A.3' and
  4698. NOT((a1, b1, c1) IN (select * from t_in));
  4699. not_pred_in
  4700. F
  4701. select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out
  4702. where subcase = 'A.3' and
  4703. ((a1, b1, c1) IN (select * from t_in)) is NULL and
  4704. ((a1, b1, c1) NOT IN (select * from t_in)) is NULL;
  4705. pred_in
  4706. N
  4707. case A.4
  4708. select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
  4709. where subcase = 'A.4' and
  4710. (a1, b1, c1) IN (select * from t_in);
  4711. pred_in
  4712. F
  4713. select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
  4714. where subcase = 'A.4' and
  4715. (a1, b1, c1) NOT IN (select * from t_in);
  4716. pred_not_in
  4717. T
  4718. select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
  4719. where subcase = 'A.4' and
  4720. NOT((a1, b1, c1) IN (select * from t_in));
  4721. not_pred_in
  4722. T
  4723. case B.1
  4724. select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
  4725. where subcase = 'B.1' and
  4726. (a1, b1, c1) IN (select * from t_in where a1 = 'no_match');
  4727. pred_in
  4728. F
  4729. select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
  4730. where subcase = 'B.1' and
  4731. (a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match');
  4732. pred_not_in
  4733. T
  4734. select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
  4735. where subcase = 'B.1' and
  4736. NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match'));
  4737. not_pred_in
  4738. T
  4739. case B.2
  4740. select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
  4741. where subcase = 'B.2' and
  4742. (a1, b1, c1) IN (select * from t_in);
  4743. pred_in
  4744. T
  4745. select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
  4746. where subcase = 'B.2' and
  4747. (a1, b1, c1) NOT IN (select * from t_in);
  4748. pred_not_in
  4749. F
  4750. select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
  4751. where subcase = 'B.2' and
  4752. NOT((a1, b1, c1) IN (select * from t_in));
  4753. not_pred_in
  4754. F
  4755. case B.3
  4756. select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
  4757. where subcase = 'B.3' and
  4758. (a1, b1, c1) IN (select * from t_in);
  4759. pred_in
  4760. F
  4761. select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
  4762. where subcase = 'B.3' and
  4763. (a1, b1, c1) NOT IN (select * from t_in);
  4764. pred_not_in
  4765. F
  4766. select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
  4767. where subcase = 'B.3' and
  4768. NOT((a1, b1, c1) IN (select * from t_in));
  4769. not_pred_in
  4770. F
  4771. select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out
  4772. where subcase = 'B.3' and
  4773. ((a1, b1, c1) IN (select * from t_in)) is NULL and
  4774. ((a1, b1, c1) NOT IN (select * from t_in)) is NULL;
  4775. pred_in
  4776. N
  4777. case B.4
  4778. select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
  4779. where subcase = 'B.4' and
  4780. (a1, b1, c1) IN (select * from t_in);
  4781. pred_in
  4782. F
  4783. select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
  4784. where subcase = 'B.4' and
  4785. (a1, b1, c1) NOT IN (select * from t_in);
  4786. pred_not_in
  4787. T
  4788. select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
  4789. where subcase = 'B.4' and
  4790. NOT((a1, b1, c1) IN (select * from t_in));
  4791. not_pred_in
  4792. T
  4793. drop table t_out;
  4794. drop table t_in;
  4795. CREATE TABLE t1 (a INT, b INT);
  4796. INSERT INTO t1 VALUES (2,22),(1,11),(2,22);
  4797. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
  4798. a
  4799. 1
  4800. 2
  4801. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
  4802. a
  4803. SELECT a FROM t1 t0
  4804. WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
  4805. a
  4806. 1
  4807. 2
  4808. SET @@sql_mode='ansi';
  4809. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
  4810. ERROR HY000: Invalid use of group function
  4811. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
  4812. ERROR HY000: Invalid use of group function
  4813. SELECT a FROM t1 t0
  4814. WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
  4815. ERROR HY000: Invalid use of group function
  4816. SET @@sql_mode=default;
  4817. DROP TABLE t1;
  4818. CREATE TABLE t1 (s1 CHAR(1));
  4819. INSERT INTO t1 VALUES ('a');
  4820. SELECT * FROM t1 WHERE _utf8'a' = ANY (SELECT s1 FROM t1);
  4821. s1
  4822. a
  4823. DROP TABLE t1;
  4824. CREATE TABLE t1(c INT, KEY(c));
  4825. CREATE TABLE t2(a INT, b INT);
  4826. INSERT INTO t2 VALUES (1, 10), (2, NULL);
  4827. INSERT INTO t1 VALUES (1), (3);
  4828. SELECT * FROM t2 WHERE b NOT IN (SELECT max(t.c) FROM t1, t1 t WHERE t.c>10);
  4829. a b
  4830. DROP TABLE t1,t2;
  4831. CREATE TABLE t1(pk INT PRIMARY KEY, a INT, INDEX idx(a));
  4832. INSERT INTO t1 VALUES (1, 10), (3, 30), (2, 20);
  4833. CREATE TABLE t2(pk INT PRIMARY KEY, a INT, b INT, INDEX idxa(a));
  4834. INSERT INTO t2 VALUES (2, 20, 700), (1, 10, 200), (4, 10, 100);
  4835. SELECT * FROM t1
  4836. WHERE EXISTS (SELECT DISTINCT a FROM t2 WHERE t1.a < t2.a ORDER BY b);
  4837. pk a
  4838. 1 10
  4839. DROP TABLE t1,t2;
  4840. CREATE TABLE t1 (a INT, b INT, PRIMARY KEY (a), KEY b (b));
  4841. INSERT INTO t1 VALUES (1,NULL), (9,NULL);
  4842. CREATE TABLE t2 (
  4843. a INT,
  4844. b INT,
  4845. c INT,
  4846. d INT,
  4847. PRIMARY KEY (a),
  4848. UNIQUE KEY b (b,c,d),
  4849. KEY b_2 (b),
  4850. KEY c (c),
  4851. KEY d (d)
  4852. );
  4853. INSERT INTO t2 VALUES
  4854. (43, 2, 11 ,30),
  4855. (44, 2, 12 ,30),
  4856. (45, 1, 1 ,10000),
  4857. (46, 1, 2 ,10000),
  4858. (556,1, 32 ,10000);
  4859. CREATE TABLE t3 (
  4860. a INT,
  4861. b INT,
  4862. c INT,
  4863. PRIMARY KEY (a),
  4864. UNIQUE KEY b (b,c),
  4865. KEY c (c),
  4866. KEY b_2 (b)
  4867. );
  4868. INSERT INTO t3 VALUES (1,1,1), (2,32,1);
  4869. explain
  4870. SELECT t1.a, (SELECT 1 FROM t2 WHERE t2.b=t3.c AND t2.c=t1.a ORDER BY t2.d LIMIT 1) AS incorrect FROM t1, t3 WHERE t3.b=t1.a;
  4871. id select_type table type possible_keys key key_len ref rows Extra
  4872. 1 PRIMARY t1 index PRIMARY PRIMARY 4 NULL 2 Using index
  4873. 1 PRIMARY t3 ref b,b_2 b 5 test.t1.a 1 Using index
  4874. 2 DEPENDENT SUBQUERY t2 ref b,b_2,c b 10 test.t3.c,test.t1.a 1 Using where; Using index; Using filesort
  4875. SELECT t1.a, (SELECT 1 FROM t2 WHERE t2.b=t3.c AND t2.c=t1.a ORDER BY t2.d LIMIT 1) AS incorrect FROM t1, t3 WHERE t3.b=t1.a;
  4876. a incorrect
  4877. 1 1
  4878. DROP TABLE t1,t2,t3;
  4879. CREATE TABLE t1 (id int);
  4880. CREATE TABLE t2 (id int, c int);
  4881. INSERT INTO t1 (id) VALUES (1);
  4882. INSERT INTO t2 (id) VALUES (1);
  4883. INSERT INTO t1 (id) VALUES (1);
  4884. INSERT INTO t2 (id) VALUES (1);
  4885. CREATE VIEW v1 AS
  4886. SELECT t2.c AS c FROM t1, t2
  4887. WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
  4888. UPDATE v1 SET c=1;
  4889. CREATE VIEW v2 (a,b) AS
  4890. SELECT t2.id, t2.c AS c FROM t1, t2
  4891. WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
  4892. INSERT INTO v2(a,b) VALUES (2,2);
  4893. ERROR HY000: CHECK OPTION failed 'test.v2'
  4894. SELECT * FROM v1;
  4895. c
  4896. 1
  4897. 1
  4898. 1
  4899. 1
  4900. CREATE VIEW v3 AS
  4901. SELECT t2.c AS c FROM t2
  4902. WHERE 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
  4903. DELETE FROM v3;
  4904. DROP VIEW v1,v2,v3;
  4905. DROP TABLE t1,t2;
  4906. #
  4907. # BUG#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result
  4908. #
  4909. create table t1(id integer primary key, g integer, v integer, s char(1));
  4910. create table t2(id integer primary key, g integer, v integer, s char(1));
  4911. insert into t1 values
  4912. (10, 10, 10, 'l'),
  4913. (20, 20, 20, 'l'),
  4914. (40, 40, 40, 'l'),
  4915. (41, 40, null, 'l'),
  4916. (50, 50, 50, 'l'),
  4917. (51, 50, null, 'l'),
  4918. (60, 60, 60, 'l'),
  4919. (61, 60, null, 'l'),
  4920. (70, 70, 70, 'l'),
  4921. (90, 90, null, 'l');
  4922. insert into t2 values
  4923. (10, 10, 10, 'r'),
  4924. (30, 30, 30, 'r'),
  4925. (50, 50, 50, 'r'),
  4926. (60, 60, 60, 'r'),
  4927. (61, 60, null, 'r'),
  4928. (70, 70, 70, 'r'),
  4929. (71, 70, null, 'r'),
  4930. (80, 80, 80, 'r'),
  4931. (81, 80, null, 'r'),
  4932. (100,100,null, 'r');
  4933. select *
  4934. from t1
  4935. where v in(select v
  4936. from t2
  4937. where t1.g=t2.g) is unknown;
  4938. id g v s
  4939. 51 50 NULL l
  4940. 61 60 NULL l
  4941. drop table t1, t2;
  4942. #
  4943. # Bug#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result
  4944. #
  4945. create table t1(id integer primary key, g integer, v integer, s char(1));
  4946. create table t2(id integer primary key, g integer, v integer, s char(1));
  4947. insert into t1 values
  4948. (10, 10, 10, 'l'),
  4949. (20, 20, 20, 'l'),
  4950. (40, 40, 40, 'l'),
  4951. (41, 40, null, 'l'),
  4952. (50, 50, 50, 'l'),
  4953. (51, 50, null, 'l'),
  4954. (60, 60, 60, 'l'),
  4955. (61, 60, null, 'l'),
  4956. (70, 70, 70, 'l'),
  4957. (90, 90, null, 'l');
  4958. insert into t2 values
  4959. (10, 10, 10, 'r'),
  4960. (30, 30, 30, 'r'),
  4961. (50, 50, 50, 'r'),
  4962. (60, 60, 60, 'r'),
  4963. (61, 60, null, 'r'),
  4964. (70, 70, 70, 'r'),
  4965. (71, 70, null, 'r'),
  4966. (80, 80, 80, 'r'),
  4967. (81, 80, null, 'r'),
  4968. (100,100,null, 'r');
  4969. select *
  4970. from t1
  4971. where v in(select v
  4972. from t2
  4973. where t1.g=t2.g) is unknown;
  4974. id g v s
  4975. 51 50 NULL l
  4976. 61 60 NULL l
  4977. drop table t1, t2;
  4978. CREATE TABLE t1 (a ENUM('rainbow'));
  4979. INSERT INTO t1 VALUES (),(),(),(),();
  4980. SELECT 1 FROM t1 GROUP BY (SELECT 1 FROM t1 ORDER BY AVG(LAST_INSERT_ID()));
  4981. 1
  4982. 1
  4983. DROP TABLE t1;
  4984. CREATE TABLE t1 (a LONGBLOB);
  4985. INSERT INTO t1 SET a = 'aaaa';
  4986. INSERT INTO t1 SET a = 'aaaa';
  4987. SELECT 1 FROM t1 GROUP BY
  4988. (SELECT LAST_INSERT_ID() FROM t1 ORDER BY MIN(a) ASC LIMIT 1);
  4989. 1
  4990. 1
  4991. DROP TABLE t1;
  4992. #
  4993. # Bug #49512 : subquery with aggregate function crash
  4994. # subselect_single_select_engine::exec()
  4995. CREATE TABLE t1(a INT);
  4996. INSERT INTO t1 VALUES();
  4997. # should not crash
  4998. SELECT 1 FROM t1 WHERE a <> SOME
  4999. (
  5000. SELECT MAX((SELECT a FROM t1 LIMIT 1)) AS d
  5001. FROM t1,t1 a
  5002. );
  5003. 1
  5004. DROP TABLE t1;
  5005. #
  5006. # Bug #45989 take 2 : memory leak after explain encounters an
  5007. # error in the query
  5008. #
  5009. CREATE TABLE t1(a LONGTEXT);
  5010. INSERT INTO t1 VALUES (repeat('a',@@global.max_allowed_packet));
  5011. INSERT INTO t1 VALUES (repeat('b',@@global.max_allowed_packet));
  5012. EXPLAIN EXTENDED SELECT DISTINCT 1 FROM t1,
  5013. (SELECT DISTINCTROW a AS away FROM t1 GROUP BY a WITH ROLLUP) AS d1
  5014. WHERE t1.a = d1.a;
  5015. ERROR 42S22: Unknown column 'd1.a' in 'where clause'
  5016. DROP TABLE t1;
  5017. Set up test tables.
  5018. CREATE TABLE t1 (
  5019. t1_id INT UNSIGNED,
  5020. PRIMARY KEY(t1_id)
  5021. ) Engine=MyISAM;
  5022. INSERT INTO t1 (t1_id) VALUES (1), (2), (3), (4), (5);
  5023. CREATE TABLE t2 SELECT * FROM t1;
  5024. CREATE TABLE t3 (
  5025. t3_id INT UNSIGNED AUTO_INCREMENT,
  5026. t1_id INT UNSIGNED,
  5027. amount DECIMAL(16,2),
  5028. PRIMARY KEY(t3_id),
  5029. KEY(t1_id)
  5030. ) Engine=MyISAM;
  5031. INSERT INTO t3 (t1_id, t3_id, amount)
  5032. VALUES (1, 1, 100.00), (2, 2, 200.00), (4, 4, 400.00);
  5033. This is the 'inner query' running by itself.
  5034. Produces correct results.
  5035. SELECT
  5036. t1.t1_id,
  5037. IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
  5038. FROM
  5039. t1
  5040. LEFT JOIN t2 ON t2.t1_id=t1.t1_id
  5041. GROUP BY
  5042. t1.t1_id
  5043. ;
  5044. t1_id total_amount
  5045. 1 100.00
  5046. 2 200.00
  5047. 3 0.00
  5048. 4 400.00
  5049. 5 0.00
  5050. SELECT * FROM (the same inner query)
  5051. Produces correct results.
  5052. SELECT * FROM (
  5053. SELECT
  5054. t1.t1_id,
  5055. IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
  5056. FROM
  5057. t1
  5058. LEFT JOIN t2 ON t2.t1_id=t1.t1_id
  5059. GROUP BY
  5060. t1.t1_id
  5061. ) AS t;
  5062. t1_id total_amount
  5063. 1 100.00
  5064. 2 200.00
  5065. 3 0.00
  5066. 4 400.00
  5067. 5 0.00
  5068. Now make t2.t1_id part of a key.
  5069. ALTER TABLE t2 ADD PRIMARY KEY(t1_id);
  5070. Same inner query by itself.
  5071. Still correct results.
  5072. SELECT
  5073. t1.t1_id,
  5074. IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
  5075. FROM
  5076. t1
  5077. LEFT JOIN t2 ON t2.t1_id=t1.t1_id
  5078. GROUP BY
  5079. t1.t1_id;
  5080. t1_id total_amount
  5081. 1 100.00
  5082. 2 200.00
  5083. 3 0.00
  5084. 4 400.00
  5085. 5 0.00
  5086. SELECT * FROM (the same inner query), now with indexes on the LEFT JOIN
  5087. SELECT * FROM (
  5088. SELECT
  5089. t1.t1_id,
  5090. IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
  5091. FROM
  5092. t1
  5093. LEFT JOIN t2 ON t2.t1_id=t1.t1_id
  5094. GROUP BY
  5095. t1.t1_id
  5096. ) AS t;
  5097. t1_id total_amount
  5098. 1 100.00
  5099. 2 200.00
  5100. 3 0.00
  5101. 4 400.00
  5102. 5 0.00
  5103. DROP TABLE t3;
  5104. DROP TABLE t2;
  5105. DROP TABLE t1;
  5106. #
  5107. # Bug #52711: Segfault when doing EXPLAIN SELECT with
  5108. # union...order by (select... where...)
  5109. #
  5110. CREATE TABLE t1 (a VARCHAR(10), FULLTEXT KEY a (a));
  5111. INSERT INTO t1 VALUES (1),(2);
  5112. CREATE TABLE t2 (b INT);
  5113. INSERT INTO t2 VALUES (1),(2);
  5114. # Should not crash
  5115. EXPLAIN
  5116. SELECT * FROM t2 UNION SELECT * FROM t2
  5117. ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE));
  5118. # Should not crash
  5119. SELECT * FROM t2 UNION SELECT * FROM t2
  5120. ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE));
  5121. DROP TABLE t1,t2;
  5122. # LP BUG#675248 - select->prep_where references on freed memory
  5123. CREATE TABLE t1 (a int, b int);
  5124. insert into t1 values (1,1),(0,0);
  5125. CREATE TABLE t2 (c int);
  5126. insert into t2 values (1),(2);
  5127. prepare stmt1 from "select sum(a),(select sum(c) from t2 where table1.b) as sub
  5128. from t1 as table1 group by sub";
  5129. execute stmt1;
  5130. sum(a) sub
  5131. 0 NULL
  5132. 1 3
  5133. deallocate prepare stmt1;
  5134. prepare stmt1 from "select sum(a),(select sum(c) from t2 having table1.b) as sub
  5135. from t1 as table1";
  5136. execute stmt1;
  5137. sum(a) sub
  5138. 1 3
  5139. deallocate prepare stmt1;
  5140. drop table t1,t2;
  5141. #
  5142. # Bug LP#693935/#58727: Assertion failure with
  5143. # a single row subquery returning more than one row
  5144. #
  5145. create table t1 (a char(1) charset utf8);
  5146. insert into t1 values ('a'), ('b');
  5147. create table t2 (a binary(1));
  5148. insert into t2 values ('x'), ('y');
  5149. select * from t2 where a=(select a from t1) and a='x';
  5150. ERROR 21000: Subquery returns more than 1 row
  5151. drop table t1,t2;
  5152. End of 5.1 tests
  5153. #
  5154. # No BUG#, a case brought from 5.2's innodb_mysql_lock.test
  5155. #
  5156. create table t1 (i int not null primary key);
  5157. insert into t1 values (1),(2),(3),(4),(5);
  5158. create table t2 (j int not null primary key);
  5159. insert into t2 values (1),(2),(3),(4),(5);
  5160. create table t3 (k int not null primary key);
  5161. insert into t3 values (1),(2),(3);
  5162. create view v2 as select t2.j as j from t2 where t2.j in (select t1.i from t1);
  5163. select * from t3 where k in (select j from v2);
  5164. k
  5165. 1
  5166. 2
  5167. 3
  5168. drop table t1,t2,t3;
  5169. drop view v2;
  5170. #
  5171. # Bug#52068: Optimizer generates invalid semijoin materialization plan
  5172. #
  5173. drop table if exists ot1, ot2, it1, it2;
  5174. CREATE TABLE ot1(a INTEGER);
  5175. INSERT INTO ot1 VALUES(5), (8);
  5176. CREATE TABLE it2(a INTEGER);
  5177. INSERT INTO it2 VALUES(9), (5), (1), (8);
  5178. CREATE TABLE it3(a INTEGER);
  5179. INSERT INTO it3 VALUES(7), (1), (0), (5), (1), (4);
  5180. CREATE TABLE ot4(a INTEGER);
  5181. INSERT INTO ot4 VALUES(1), (3), (5), (7), (9), (7), (3), (1);
  5182. SELECT * FROM ot1,ot4
  5183. WHERE (ot1.a,ot4.a) IN (SELECT it2.a,it3.a
  5184. FROM it2,it3);
  5185. a a
  5186. 5 1
  5187. 8 1
  5188. 5 5
  5189. 8 5
  5190. 5 7
  5191. 8 7
  5192. 5 7
  5193. 8 7
  5194. 5 1
  5195. 8 1
  5196. explain SELECT * FROM ot1,ot4
  5197. WHERE (ot1.a,ot4.a) IN (SELECT it2.a,it3.a
  5198. FROM it2,it3);
  5199. id select_type table type possible_keys key key_len ref rows Extra
  5200. 1 PRIMARY ot1 ALL NULL NULL NULL NULL 2
  5201. 1 PRIMARY <subquery2> ALL distinct_key NULL NULL NULL 24 Using where
  5202. 1 PRIMARY ot4 ALL NULL NULL NULL NULL 8 Using where; Using join buffer (flat, BNL join)
  5203. 2 MATERIALIZED it2 ALL NULL NULL NULL NULL 4
  5204. 2 MATERIALIZED it3 ALL NULL NULL NULL NULL 6 Using join buffer (flat, BNL join)
  5205. DROP TABLE IF EXISTS ot1, ot4, it2, it3;
  5206. #
  5207. # Bug#729039: NULL keys used to evaluate subquery
  5208. #
  5209. CREATE TABLE t1 (a int) ;
  5210. INSERT INTO t1 VALUES (NULL), (1), (NULL), (2);
  5211. CREATE TABLE t2 (a int, INDEX idx(a)) ;
  5212. INSERT INTO t2 VALUES (NULL), (1), (NULL);
  5213. SELECT * FROM t1
  5214. WHERE EXISTS (SELECT a FROM t2 USE INDEX () WHERE t2.a = t1.a);
  5215. a
  5216. 1
  5217. EXPLAIN
  5218. SELECT * FROM t1
  5219. WHERE EXISTS (SELECT a FROM t2 USE INDEX() WHERE t2.a = t1.a);
  5220. id select_type table type possible_keys key key_len ref rows Extra
  5221. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where
  5222. 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 3 Using where
  5223. SELECT * FROM t1
  5224. WHERE EXISTS (SELECT a FROM t2 WHERE t2.a = t1.a);
  5225. a
  5226. 1
  5227. EXPLAIN
  5228. SELECT * FROM t1
  5229. WHERE EXISTS (SELECT a FROM t2 WHERE t2.a = t1.a);
  5230. id select_type table type possible_keys key key_len ref rows Extra
  5231. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where
  5232. 2 DEPENDENT SUBQUERY t2 ref idx idx 5 test.t1.a 2 Using index
  5233. DROP TABLE t1,t2;
  5234. #
  5235. # BUG#752992: Wrong results for a subquery with 'semijoin=on'
  5236. #
  5237. CREATE TABLE t1 (pk INTEGER PRIMARY KEY, i INTEGER NOT NULL);
  5238. INSERT INTO t1 VALUES (11,0);
  5239. INSERT INTO t1 VALUES (12,5);
  5240. INSERT INTO t1 VALUES (15,0);
  5241. CREATE TABLE t2 (pk INTEGER PRIMARY KEY, i INTEGER NOT NULL);
  5242. INSERT INTO t2 VALUES (11,1);
  5243. INSERT INTO t2 VALUES (12,2);
  5244. INSERT INTO t2 VALUES (15,4);
  5245. SET @save_join_cache_level=@@join_cache_level;
  5246. SET join_cache_level=0;
  5247. EXPLAIN SELECT * FROM t1 WHERE pk IN (SELECT it.pk FROM t2 JOIN t2 AS it ON 1);
  5248. id select_type table type possible_keys key key_len ref rows Extra
  5249. 1 PRIMARY t1 ALL PRIMARY NULL NULL NULL 3
  5250. 1 PRIMARY it eq_ref PRIMARY PRIMARY 4 test.t1.pk 1 Using index
  5251. 1 PRIMARY t2 index NULL PRIMARY 4 NULL 3 Using index; FirstMatch(it)
  5252. SELECT * FROM t1 WHERE pk IN (SELECT it.pk FROM t2 JOIN t2 AS it ON 1);
  5253. pk i
  5254. 11 0
  5255. 12 5
  5256. 15 0
  5257. SET join_cache_level=@save_join_cache_level;
  5258. DROP table t1,t2;
  5259. #
  5260. # Bug#751350: crash with pushed condition for outer references when
  5261. # there should be none of such conditions
  5262. #
  5263. CREATE TABLE t1 (a int, b int) ;
  5264. INSERT INTO t1 VALUES (0,0),(0,0);
  5265. set @optimizer_switch_save=@@optimizer_switch;
  5266. set @@optimizer_switch='semijoin=off,materialization=on,in_to_exists=on';
  5267. EXPLAIN
  5268. SELECT b FROM t1
  5269. WHERE ('0') IN ( SELECT a FROM t1 GROUP BY a )
  5270. GROUP BY b;
  5271. id select_type table type possible_keys key key_len ref rows Extra
  5272. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using temporary; Using filesort
  5273. 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where
  5274. SELECT b FROM t1
  5275. WHERE ('0') IN ( SELECT a FROM t1 GROUP BY a )
  5276. GROUP BY b;
  5277. b
  5278. 0
  5279. set @@optimizer_switch=@optimizer_switch_save;
  5280. DROP TABLE t1;
  5281. #
  5282. # Bug #11765713 58705:
  5283. # OPTIMIZER LET ENGINE DEPEND ON UNINITIALIZED VALUES
  5284. # CREATED BY OPT_SUM_QUERY
  5285. #
  5286. CREATE TABLE t1(a INT NOT NULL, KEY (a));
  5287. INSERT INTO t1 VALUES (0), (1);
  5288. SELECT 1 as foo FROM t1 WHERE a < SOME
  5289. (SELECT a FROM t1 WHERE a <=>
  5290. (SELECT a FROM t1)
  5291. );
  5292. ERROR 21000: Subquery returns more than 1 row
  5293. SELECT 1 as foo FROM t1 WHERE a < SOME
  5294. (SELECT a FROM t1 WHERE a <=>
  5295. (SELECT a FROM t1 where a is null)
  5296. );
  5297. foo
  5298. DROP TABLE t1;
  5299. CREATE TABLE t1 (a int(11), b varchar(1));
  5300. INSERT INTO t1 VALUES (2,NULL),(5,'d'),(7,'g');
  5301. SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 GROUP BY b );
  5302. a
  5303. 5
  5304. SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 );
  5305. a
  5306. 5
  5307. SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 GROUP BY b );
  5308. a
  5309. 7
  5310. SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 );
  5311. a
  5312. 7
  5313. SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 GROUP BY b );
  5314. a
  5315. 5
  5316. 7
  5317. SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 );
  5318. a
  5319. 5
  5320. 7
  5321. SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 GROUP BY b );
  5322. a
  5323. 5
  5324. 7
  5325. SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 );
  5326. a
  5327. 5
  5328. 7
  5329. SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 );
  5330. a
  5331. 5
  5332. 7
  5333. SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 GROUP BY b );
  5334. a
  5335. 5
  5336. 7
  5337. SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 );
  5338. a
  5339. 5
  5340. 7
  5341. SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 GROUP BY b );
  5342. a
  5343. 5
  5344. 7
  5345. SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 GROUP BY b );
  5346. a
  5347. SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 );
  5348. a
  5349. SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 GROUP BY b );
  5350. a
  5351. SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 );
  5352. a
  5353. SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 GROUP BY b );
  5354. a
  5355. SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 );
  5356. a
  5357. SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 GROUP BY b );
  5358. a
  5359. SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 );
  5360. a
  5361. SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 );
  5362. a
  5363. SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 GROUP BY b );
  5364. a
  5365. SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 );
  5366. a
  5367. SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 GROUP BY b );
  5368. a
  5369. delete from t1;
  5370. INSERT INTO t1 VALUES (2,NULL),(5,'d'),(7,'g');
  5371. SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 GROUP BY b );
  5372. a
  5373. 5
  5374. SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 );
  5375. a
  5376. 5
  5377. SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 GROUP BY b );
  5378. a
  5379. 7
  5380. SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 );
  5381. a
  5382. 7
  5383. SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 GROUP BY b );
  5384. a
  5385. 5
  5386. 7
  5387. SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 );
  5388. a
  5389. 5
  5390. 7
  5391. SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 GROUP BY b );
  5392. a
  5393. 5
  5394. 7
  5395. SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 );
  5396. a
  5397. 5
  5398. 7
  5399. SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 );
  5400. a
  5401. 5
  5402. 7
  5403. SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 GROUP BY b );
  5404. a
  5405. 5
  5406. 7
  5407. SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 );
  5408. a
  5409. 5
  5410. 7
  5411. SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 GROUP BY b );
  5412. a
  5413. 5
  5414. 7
  5415. SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 GROUP BY b );
  5416. a
  5417. SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 );
  5418. a
  5419. SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 GROUP BY b );
  5420. a
  5421. SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 );
  5422. a
  5423. SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 GROUP BY b );
  5424. a
  5425. SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 );
  5426. a
  5427. SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 GROUP BY b );
  5428. a
  5429. SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 );
  5430. a
  5431. SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 );
  5432. a
  5433. SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 GROUP BY b );
  5434. a
  5435. SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 );
  5436. a
  5437. SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 GROUP BY b );
  5438. a
  5439. drop table t1;
  5440. #
  5441. # Fix of LP BUG#780386 (NULL left part with empty ALL subquery).
  5442. #
  5443. CREATE TABLE t1 ( f11 int) ;
  5444. INSERT IGNORE INTO t1 VALUES (0),(0);
  5445. CREATE TABLE t2 ( f3 int, f10 int, KEY (f10,f3)) ;
  5446. INSERT IGNORE INTO t2 VALUES (NULL,NULL),(5,0);
  5447. DROP TABLE IF EXISTS t3;
  5448. Warnings:
  5449. Note 1051 Unknown table 't3'
  5450. CREATE TABLE t3 ( f3 int) ;
  5451. INSERT INTO t3 VALUES (0),(0);
  5452. SELECT a1.f3 AS r FROM t2 AS a1 , t1 WHERE a1.f3 < ALL ( SELECT f3 FROM t3 WHERE f3 = 1 ) ;
  5453. r
  5454. NULL
  5455. 5
  5456. NULL
  5457. 5
  5458. DROP TABLE t1, t2, t3;
  5459. #
  5460. # Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
  5461. #
  5462. CREATE TABLE t1(a1 int);
  5463. INSERT INTO t1 VALUES (1),(2);
  5464. CREATE TABLE t2(a1 int);
  5465. INSERT INTO t2 VALUES (3);
  5466. SELECT @@session.sql_mode INTO @old_sql_mode;
  5467. SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
  5468. SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2 FROM t2);
  5469. 1
  5470. 1
  5471. 1
  5472. SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2.0 FROM t2);
  5473. 1
  5474. 1
  5475. 1
  5476. SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 'a' FROM t2);
  5477. 1
  5478. SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t2);
  5479. 1
  5480. 1
  5481. 1
  5482. SET SESSION sql_mode=@old_sql_mode;
  5483. DROP TABLE t1, t2;
  5484. create table t2(i int);
  5485. insert into t2 values(0);
  5486. SELECT @@session.sql_mode INTO @old_sql_mode;
  5487. SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
  5488. CREATE VIEW v1 AS
  5489. SELECT 'f' FROM t2 UNION SELECT 'x' FROM t2
  5490. ;
  5491. CREATE TABLE t1 (
  5492. pk int NOT NULL,
  5493. col_varchar_key varchar(1) DEFAULT NULL,
  5494. PRIMARY KEY (pk),
  5495. KEY col_varchar_key (col_varchar_key)
  5496. );
  5497. SELECT t1.pk
  5498. FROM t1
  5499. WHERE t1.col_varchar_key < ALL ( SELECT * FROM v1 )
  5500. ;
  5501. pk
  5502. SET SESSION sql_mode=@old_sql_mode;
  5503. drop table t2, t1;
  5504. drop view v1;
  5505. #
  5506. # BUG#50257: Missing info in REF column of the EXPLAIN
  5507. # lines for subselects
  5508. #
  5509. CREATE TABLE t1 (a INT, b INT, INDEX (a));
  5510. INSERT INTO t1 VALUES (3, 10), (2, 20), (7, 10), (5, 20);
  5511. EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a=7) t;
  5512. id select_type table type possible_keys key key_len ref rows Extra
  5513. 1 SIMPLE t1 ref a a 5 const 1
  5514. EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE a=7);
  5515. id select_type table type possible_keys key key_len ref rows Extra
  5516. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4
  5517. 2 SUBQUERY t1 ref a a 5 const 1 Using index
  5518. DROP TABLE t1;
  5519. #
  5520. # BUG#12616253 - WRONG RESULT WITH EXISTS(SUBQUERY) (MISSING ROWS)
  5521. # (duplicate of LP bug #888456)
  5522. #
  5523. CREATE TABLE t1 (f1 varchar(1));
  5524. INSERT INTO t1 VALUES ('v'),('s');
  5525. CREATE TABLE t2 (f1_key varchar(1), KEY (f1_key));
  5526. INSERT INTO t2 VALUES ('j'),('v'),('c'),('m'),('d'),
  5527. ('d'),('y'),('t'),('d'),('s');
  5528. EXPLAIN
  5529. SELECT table1.f1, table2.f1_key FROM t1 AS table1, t2 AS table2
  5530. WHERE EXISTS (SELECT DISTINCT f1_key FROM t2
  5531. WHERE f1_key != table2.f1_key AND f1_key >= table1.f1);
  5532. id select_type table type possible_keys key key_len ref rows Extra
  5533. 1 PRIMARY table1 ALL NULL NULL NULL NULL 2
  5534. 1 PRIMARY table2 index NULL f1_key 4 NULL 10 Using where; Using index; Using join buffer (flat, BNL join)
  5535. 2 DEPENDENT SUBQUERY t2 index f1_key f1_key 4 NULL 10 Using where; Using index
  5536. SELECT table1.f1, table2.f1_key FROM t1 AS table1, t2 AS table2
  5537. WHERE EXISTS (SELECT DISTINCT f1_key FROM t2
  5538. WHERE f1_key != table2.f1_key AND f1_key >= table1.f1);
  5539. f1 f1_key
  5540. v j
  5541. s j
  5542. v v
  5543. s v
  5544. v c
  5545. s c
  5546. v m
  5547. s m
  5548. v d
  5549. s d
  5550. v d
  5551. s d
  5552. v y
  5553. s y
  5554. v t
  5555. s t
  5556. v d
  5557. s d
  5558. v s
  5559. s s
  5560. DROP TABLE t1,t2;
  5561. #
  5562. # LP bug 919427: EXPLAIN for a query over a single-row table
  5563. # with IN subquery in WHERE condition
  5564. #
  5565. CREATE TABLE ot (
  5566. col_int_nokey int(11),
  5567. col_varchar_nokey varchar(1)
  5568. ) ;
  5569. INSERT INTO ot VALUES (1,'x');
  5570. CREATE TABLE it1(
  5571. col_int_key int(11),
  5572. col_varchar_key varchar(1),
  5573. KEY idx_cvk_cik (col_varchar_key,col_int_key)
  5574. );
  5575. INSERT INTO it1 VALUES (NULL,'x'), (NULL,'f');
  5576. CREATE TABLE it2 (
  5577. col_int_key int(11),
  5578. col_varchar_key varchar(1),
  5579. col_varchar_key2 varchar(1),
  5580. KEY idx_cvk_cvk2_cik (col_varchar_key, col_varchar_key2, col_int_key),
  5581. KEY idx_cvk_cik (col_varchar_key, col_int_key)
  5582. );
  5583. INSERT INTO it2 VALUES (NULL,'x','x'), (NULL,'f','f');
  5584. EXPLAIN
  5585. SELECT col_int_nokey FROM ot
  5586. WHERE col_varchar_nokey IN
  5587. (SELECT col_varchar_key FROM it1 WHERE col_int_key IS NULL);
  5588. id select_type table type possible_keys key key_len ref rows Extra
  5589. 1 PRIMARY ot system NULL NULL NULL NULL 1
  5590. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
  5591. 2 MATERIALIZED it1 ref idx_cvk_cik idx_cvk_cik 9 const,const 1 Using where; Using index
  5592. SELECT col_int_nokey FROM ot
  5593. WHERE col_varchar_nokey IN
  5594. (SELECT col_varchar_key FROM it1 WHERE col_int_key IS NULL);
  5595. col_int_nokey
  5596. 1
  5597. EXPLAIN
  5598. SELECT col_int_nokey FROM ot
  5599. WHERE (col_varchar_nokey, 'x') IN
  5600. (SELECT col_varchar_key, col_varchar_key2 FROM it2);
  5601. id select_type table type possible_keys key key_len ref rows Extra
  5602. 1 PRIMARY ot system NULL NULL NULL NULL 1
  5603. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1
  5604. 2 MATERIALIZED it2 ref idx_cvk_cvk2_cik,idx_cvk_cik idx_cvk_cvk2_cik 8 const,const 1 Using where; Using index
  5605. SELECT col_int_nokey FROM ot
  5606. WHERE (col_varchar_nokey, 'x') IN
  5607. (SELECT col_varchar_key, col_varchar_key2 FROM it2);
  5608. col_int_nokey
  5609. 1
  5610. DROP TABLE ot,it1,it2;
  5611. End of 5.2 tests
  5612. #
  5613. # BUG#779885: Crash in eliminate_item_equal with materialization=on in
  5614. #
  5615. CREATE TABLE t1 ( f1 int );
  5616. INSERT INTO t1 VALUES (19), (20);
  5617. CREATE TABLE t2 ( f10 varchar(32) );
  5618. INSERT INTO t2 VALUES ('c'),('d');
  5619. CREATE TABLE t3 ( f10 varchar(32) );
  5620. INSERT INTO t3 VALUES ('a'),('b');
  5621. SELECT *
  5622. FROM t1
  5623. WHERE
  5624. ( 't' ) IN (
  5625. SELECT t3.f10
  5626. FROM t3
  5627. JOIN t2
  5628. ON t2.f10 = t3.f10
  5629. );
  5630. f1
  5631. DROP TABLE t1,t2,t3;
  5632. #
  5633. # BUG LP:813473: Wrong result with outer join + NOT IN subquery
  5634. # This bug is a duplicate of Bug#11764086 whose test case is added below
  5635. #
  5636. CREATE TABLE t1 (c int) ;
  5637. INSERT INTO t1 VALUES (5),(6);
  5638. CREATE TABLE t2 (a int, b int) ;
  5639. INSERT INTO t2 VALUES (20,9),(20,9);
  5640. create table t3 (d int, e int);
  5641. insert into t3 values (2, 9), (3,10);
  5642. SET @save_optimizer_switch=@@optimizer_switch;
  5643. SET optimizer_switch='outer_join_with_cache=off';
  5644. EXPLAIN
  5645. SELECT t2.b , t1.c
  5646. FROM t2 LEFT JOIN t1 ON t1.c < 3
  5647. WHERE (t2.b , t1.c) NOT IN (SELECT * from t3);
  5648. id select_type table type possible_keys key key_len ref rows Extra
  5649. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2
  5650. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
  5651. 2 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 2 Using where
  5652. SELECT t2.b , t1.c
  5653. FROM t2 LEFT JOIN t1 ON t1.c < 3
  5654. WHERE (t2.b, t1.c) NOT IN (SELECT * from t3);
  5655. b c
  5656. 9 NULL
  5657. 9 NULL
  5658. SET optimizer_switch=@save_optimizer_switch;
  5659. drop table t1, t2, t3;
  5660. #
  5661. # Bug#11764086: Null left operand to NOT IN in WHERE clause
  5662. # behaves differently than real NULL
  5663. #
  5664. CREATE TABLE parent (id int);
  5665. INSERT INTO parent VALUES (1), (2);
  5666. CREATE TABLE child (parent_id int, other int);
  5667. INSERT INTO child VALUES (1,NULL);
  5668. # Offending query (c.parent_id is NULL for null-complemented rows only)
  5669. SELECT p.id, c.parent_id
  5670. FROM parent p
  5671. LEFT JOIN child c
  5672. ON p.id = c.parent_id
  5673. WHERE c.parent_id NOT IN (
  5674. SELECT parent_id
  5675. FROM child
  5676. WHERE parent_id = 3
  5677. );
  5678. id parent_id
  5679. 1 1
  5680. 2 NULL
  5681. # Some syntactic variations with IS FALSE and IS NOT TRUE
  5682. SELECT p.id, c.parent_id
  5683. FROM parent p
  5684. LEFT JOIN child c
  5685. ON p.id = c.parent_id
  5686. WHERE c.parent_id IN (
  5687. SELECT parent_id
  5688. FROM child
  5689. WHERE parent_id = 3
  5690. ) IS NOT TRUE;
  5691. id parent_id
  5692. 1 1
  5693. 2 NULL
  5694. SELECT p.id, c.parent_id
  5695. FROM parent p
  5696. LEFT JOIN child c
  5697. ON p.id = c.parent_id
  5698. WHERE c.parent_id IN (
  5699. SELECT parent_id
  5700. FROM child
  5701. WHERE parent_id = 3
  5702. ) IS FALSE;
  5703. id parent_id
  5704. 1 1
  5705. 2 NULL
  5706. DROP TABLE parent, child;
  5707. # End of test for bug#11764086.
  5708. #
  5709. # Bug 11765699 - 58690: !TABLE || (!TABLE->READ_SET ||
  5710. # BITMAP_IS_SET(TABLE->READ_SET, FIELD_INDEX
  5711. #
  5712. CREATE TABLE t1(a INT);
  5713. INSERT INTO t1 VALUES (0), (1);
  5714. CREATE TABLE t2(
  5715. b TEXT,
  5716. c INT,
  5717. PRIMARY KEY (b(1))
  5718. );
  5719. INSERT INTO t2 VALUES ('a', 2), ('b', 3);
  5720. SELECT 1 FROM t1 WHERE a =
  5721. (SELECT 1 FROM t2 WHERE b =
  5722. (SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2)
  5723. ORDER BY b
  5724. );
  5725. 1
  5726. SELECT 1 FROM t1 WHERE a =
  5727. (SELECT 1 FROM t2 WHERE b =
  5728. (SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2)
  5729. GROUP BY b
  5730. );
  5731. 1
  5732. DROP TABLE t1, t2;
  5733. #
  5734. # LP bug #826279: assertion failure with GROUP BY a result of subquery
  5735. #
  5736. CREATE TABLE t1 (a int);
  5737. INSERT INTO t1 VALUES (0), (0);
  5738. CREATE TABLE t2 (a int, b int, c int);
  5739. INSERT INTO t2 VALUES (10,7,0), (0,7,0);
  5740. CREATE TABLE t3 (a int, b int);
  5741. INSERT INTO t3 VALUES (10,7), (0,7);
  5742. SELECT SUM(DISTINCT b),
  5743. (SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
  5744. WHERE t.a != 0 AND t2.a != 0)
  5745. FROM (SELECT * FROM t3) AS t
  5746. GROUP BY 2;
  5747. SUM(DISTINCT b) (SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
  5748. WHERE t.a != 0 AND t2.a != 0)
  5749. 7 NULL
  5750. SELECT SUM(DISTINCT b),
  5751. (SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1)
  5752. FROM (SELECT * FROM t3) AS t
  5753. GROUP BY 2;
  5754. SUM(DISTINCT b) (SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1)
  5755. 7 NULL
  5756. 7 10
  5757. DROP TABLE t1,t2,t3;
  5758. #
  5759. # Bug#12329653
  5760. # EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY
  5761. #
  5762. CREATE TABLE t1(a1 int);
  5763. INSERT INTO t1 VALUES (1),(2);
  5764. SELECT @@session.sql_mode INTO @old_sql_mode;
  5765. SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
  5766. SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
  5767. 1
  5768. 1
  5769. 1
  5770. PREPARE stmt FROM
  5771. 'SELECT 1 UNION ALL
  5772. SELECT 1 FROM t1
  5773. ORDER BY
  5774. (SELECT 1 FROM t1 AS t1_0
  5775. WHERE 1 < SOME (SELECT a1 FROM t1)
  5776. )' ;
  5777. EXECUTE stmt ;
  5778. ERROR 21000: Subquery returns more than 1 row
  5779. EXECUTE stmt ;
  5780. ERROR 21000: Subquery returns more than 1 row
  5781. SET SESSION sql_mode=@old_sql_mode;
  5782. DEALLOCATE PREPARE stmt;
  5783. DROP TABLE t1;
  5784. #
  5785. # LP BUG#833777 Performance regression with deeply nested subqueries
  5786. #
  5787. create table t1 (a int not null, b char(10) not null);
  5788. insert into t1 values (1, 'a');
  5789. set @@optimizer_switch='in_to_exists=on,semijoin=off,materialization=off,subquery_cache=off';
  5790. select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1 where a in (select a from t1)))))))))))))))))))))))))))));
  5791. a
  5792. 1
  5793. set @@optimizer_switch=@subselect_tmp;
  5794. drop table t1;
  5795. #
  5796. # LP BUG#894397 Wrong result with in_to_exists, constant table , semijoin=OFF,materialization=OFF
  5797. #
  5798. CREATE TABLE t1 (a varchar(3));
  5799. INSERT INTO t1 VALUES ('AAA'),('BBB');
  5800. CREATE TABLE t2 (a varchar(3));
  5801. INSERT INTO t2 VALUES ('CCC');
  5802. set @@optimizer_switch='semijoin=off,materialization=off,in_to_exists=on,subquery_cache=off';
  5803. SELECT * FROM t1 WHERE t1.a IN (SELECT t2.a FROM t2 WHERE t2.a < 'ZZZ');
  5804. a
  5805. set @@optimizer_switch=@subselect_tmp;
  5806. drop table t1, t2;
  5807. #
  5808. # LP bug #859375: Assertion `0' failed in st_select_lex_unit::optimize
  5809. # with view , UNION and prepared statement (rewriting fake_select
  5810. # condition).
  5811. #
  5812. CREATE TABLE t1 ( f1 int NOT NULL, f4 varchar(1) NOT NULL) ;
  5813. INSERT INTO t1 VALUES (6,'d'),(7,'y');
  5814. CREATE TABLE t2 ( f1 int NOT NULL, f2 int NOT NULL) ;
  5815. INSERT INTO t2 VALUES (10,7);
  5816. CREATE VIEW v2 AS SELECT * FROM t2;
  5817. PREPARE st1 FROM "
  5818. SELECT *
  5819. FROM t1
  5820. LEFT JOIN v2 ON ( v2.f2 = t1.f1 )
  5821. WHERE v2.f1 NOT IN (
  5822. SELECT 1 UNION
  5823. SELECT 247
  5824. )
  5825. ";
  5826. EXECUTE st1;
  5827. f1 f4 f1 f2
  5828. 7 y 10 7
  5829. deallocate prepare st1;
  5830. DROP VIEW v2;
  5831. DROP TABLE t1,t2;
  5832. #
  5833. # LP bug #887458 Crash in subselect_union_engine::no_rows with
  5834. # double UNION and join_cache_level=3,8
  5835. # (IN/ALL/ANY optimizations should not be applied to fake_select)
  5836. CREATE TABLE t2 ( a int, b varchar(1)) ;
  5837. INSERT IGNORE INTO t2 VALUES (8,'y'),(8,'y');
  5838. CREATE TABLE t1 ( b varchar(1)) ;
  5839. INSERT IGNORE INTO t1 VALUES (NULL),(NULL);
  5840. set @save_join_cache_level=@@join_cache_level;
  5841. SET SESSION join_cache_level=3;
  5842. SELECT *
  5843. FROM t1, t2
  5844. WHERE t2.b IN (
  5845. SELECT 'm' UNION
  5846. SELECT 'm'
  5847. ) OR t1.b <> SOME (
  5848. SELECT 'v' UNION
  5849. SELECT 't'
  5850. );
  5851. b a b
  5852. set @@join_cache_level= @save_join_cache_level;
  5853. drop table t1,t2;
  5854. #
  5855. # LP bug #885162 Got error 124 from storage engine with UNION inside
  5856. # subquery and join_cache_level=3..8
  5857. # (IN/ALL/ANY optimizations should not be applied to fake_select)
  5858. #
  5859. CREATE TABLE t1 (
  5860. f1 varchar(1) DEFAULT NULL
  5861. );
  5862. INSERT INTO t1 VALUES ('c');
  5863. set @save_join_cache_level=@@join_cache_level;
  5864. SET SESSION join_cache_level=8;
  5865. SELECT * FROM t1 WHERE t1.f1 IN ( SELECT 'k' UNION SELECT 'e' );
  5866. f1
  5867. set @@join_cache_level= @save_join_cache_level;
  5868. drop table t1;
  5869. #
  5870. # LP BUG#747278 incorrect values of the NULL (no rows) single
  5871. # row subquery requested via element_index() interface
  5872. #
  5873. CREATE TABLE t1 (f1a int, f1b int) ;
  5874. INSERT IGNORE INTO t1 VALUES (1,1),(2,2);
  5875. CREATE TABLE t2 ( f2 int);
  5876. INSERT IGNORE INTO t2 VALUES (3),(4);
  5877. CREATE TABLE t3 (f3a int default 1, f3b int default 2);
  5878. INSERT INTO t3 VALUES (1,1),(2,2);
  5879. set @old_optimizer_switch = @@session.optimizer_switch;
  5880. set @@optimizer_switch='materialization=on,partial_match_rowid_merge=on,partial_match_table_scan=off,subquery_cache=off,semijoin=off';
  5881. SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
  5882. (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
  5883. NULL
  5884. NULL
  5885. SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
  5886. (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
  5887. NULL
  5888. NULL
  5889. SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
  5890. (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
  5891. NULL
  5892. NULL
  5893. SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
  5894. (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
  5895. NULL
  5896. SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
  5897. (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
  5898. NULL
  5899. NULL
  5900. SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
  5901. (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
  5902. NULL
  5903. NULL
  5904. SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
  5905. (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
  5906. NULL
  5907. NULL
  5908. SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
  5909. (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
  5910. NULL
  5911. set @@session.optimizer_switch=@old_optimizer_switch;
  5912. SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
  5913. (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
  5914. NULL
  5915. NULL
  5916. SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
  5917. (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
  5918. NULL
  5919. NULL
  5920. SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
  5921. (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
  5922. NULL
  5923. NULL
  5924. SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
  5925. (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
  5926. NULL
  5927. SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
  5928. (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
  5929. NULL
  5930. NULL
  5931. SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
  5932. (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
  5933. NULL
  5934. NULL
  5935. SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
  5936. (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
  5937. NULL
  5938. NULL
  5939. SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
  5940. (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
  5941. NULL
  5942. select (null, null) = (null, null);
  5943. (null, null) = (null, null)
  5944. NULL
  5945. SELECT (SELECT f3a, f3a FROM t3 where f3a > 3) = (0, 0);
  5946. (SELECT f3a, f3a FROM t3 where f3a > 3) = (0, 0)
  5947. NULL
  5948. drop tables t1,t2,t3;
  5949. #
  5950. # LP BUG#825051 Wrong result with date/datetime and subquery with GROUP BY and in_to_exists
  5951. #
  5952. CREATE TABLE t1 (a date, KEY (a)) ;
  5953. INSERT INTO t1 VALUES ('2009-01-01'),('2009-02-02');
  5954. set @old_optimizer_switch = @@optimizer_switch;
  5955. SET @@optimizer_switch='semijoin=off,materialization=off,in_to_exists=on,subquery_cache=off';
  5956. EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
  5957. id select_type table type possible_keys key key_len ref rows Extra
  5958. 1 PRIMARY t1 index NULL a 4 NULL 2 Using where; Using index
  5959. 2 DEPENDENT SUBQUERY t1 index_subquery a a 4 func 2 Using index
  5960. SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
  5961. a
  5962. 2009-01-01
  5963. 2009-02-02
  5964. SET @@optimizer_switch='semijoin=off,materialization=on,in_to_exists=off,subquery_cache=off';
  5965. EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
  5966. id select_type table type possible_keys key key_len ref rows Extra
  5967. 1 PRIMARY t1 index NULL a 4 NULL 2 Using where; Using index
  5968. 2 MATERIALIZED t1 index NULL a 4 NULL 2 Using index
  5969. SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
  5970. a
  5971. 2009-01-01
  5972. 2009-02-02
  5973. set @@optimizer_switch=@old_optimizer_switch;
  5974. drop table t1;
  5975. #
  5976. # LP BUG#908269 incorrect condition in case of subqueries depending
  5977. # on constant tables
  5978. #
  5979. CREATE TABLE t1 ( a INT );
  5980. INSERT INTO t1 VALUES (1),(5);
  5981. CREATE TABLE t2 ( b INT ) ENGINE=MyISAM;
  5982. INSERT INTO t2 VALUES (1);
  5983. CREATE TABLE t3 ( c INT );
  5984. INSERT INTO t3 VALUES (4),(5);
  5985. SET optimizer_switch='subquery_cache=off';
  5986. SELECT ( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1;
  5987. ( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) )
  5988. 1
  5989. NULL
  5990. SELECT ( SELECT b FROM t2 WHERE b = a OR b * 0) FROM t1;
  5991. ( SELECT b FROM t2 WHERE b = a OR b * 0)
  5992. 1
  5993. NULL
  5994. SELECT ( SELECT b FROM t2 WHERE b = a OR rand() * 0) FROM t1;
  5995. ( SELECT b FROM t2 WHERE b = a OR rand() * 0)
  5996. 1
  5997. NULL
  5998. drop table t1,t2,t3;
  5999. #
  6000. # LP BUG#905353 Wrong non-empty result with a constant table,
  6001. # aggregate function in subquery, MyISAM or Aria
  6002. #
  6003. CREATE TABLE t1 ( a INT ) ENGINE=MyISAM;
  6004. INSERT INTO t1 VALUES (1);
  6005. SELECT a FROM t1 WHERE ( SELECT MIN(a) = 100 );
  6006. a
  6007. drop table t1;
  6008. # return optimizer switch changed in the beginning of this test
  6009. set optimizer_switch=@subselect_tmp;
  6010. set optimizer_switch=default;
  6011. select @@optimizer_switch like '%subquery_cache=on%';
  6012. @@optimizer_switch like '%subquery_cache=on%'
  6013. 1
  6014. set @join_cache_level_for_subselect_test=NULL;