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.

7306 lines
253 KiB

Patch for mdev-287: CHEAP SQ: A query with subquery in SELECT list, EXISTS, inner joins takes hundreds times longer Analysis: The fix for lp:944706 introduces early subquery optimization. While a subquery is being optimized some of its predicates may be removed. In the test case, the EXISTS subquery is constant, and is evaluated to TRUE. As a result the whole OR is TRUE, and thus the correlated condition "b = alias1.b" is optimized away. The subquery becomes non-correlated. The subquery cache is designed to work only for correlated subqueries. If constant subquery optimization is disallowed, then the constant subquery is not evaluated, the subquery remains correlated, and its execution is cached. As a result execution is fast. However, when the constant subquery was optimized away, it was neither cached by the subquery cache, nor it was cached by the internal subquery caching. The latter was due to the fact that the subquery still appeared as correlated to the subselect_XYZ_engine::exec methods, and they re-executed the subquery on each call to Item_subselect::exec. Solution: The solution is to update the correlated status of the subquery after it has been optimized. This status consists of: - st_select_lex::is_correlated - Item_subselect::is_correlated - SELECT_LEX::uncacheable - SELECT_LEX_UNIT::uncacheable The status is updated by st_select_lex::update_correlated_cache(), and its caller st_select_lex::optimize_unflattened_subqueries. The solution relies on the fact that the optimizer already called st_select_lex::update_used_tables() for each subquery. This allows to efficiently update the correlated status of each subquery without walking the whole subquery tree. Notice that his patch is an improvement over MySQL 5.6 and older, where subqueries are not pre-optimized, and the above analysis is not possible.
14 years ago
Merge with 5.1-microseconds A lot of small fixes and new test cases. client/mysqlbinlog.cc: Cast removed client/mysqltest.cc: Added missing DBUG_RETURN include/my_pthread.h: set_timespec_time_nsec() now only takes one argument mysql-test/t/date_formats.test: Remove --disable_ps_protocl as now also ps supports microseconds mysys/my_uuid.c: Changed to use my_interval_timer() instead of my_getsystime() mysys/waiting_threads.c: Changed to use my_hrtime() sql/field.h: Added bool special_const_compare() for fields that may convert values before compare (like year) sql/field_conv.cc: Added test to get optimal copying of identical temporal values. sql/item.cc: Return that item_int is equal if it's positive, even if unsigned flag is different. Fixed Item_cache_str::save_in_field() to have identical null check as other similar functions Added proper NULL check to Item_cache_int::save_in_field() sql/item_cmpfunc.cc: Don't call convert_constant_item() if there is nothing that is worth converting. Simplified test when years should be converted sql/item_sum.cc: Mark cache values in Item_sum_hybrid as not constants to ensure they are not replaced by other cache values in compare_datetime() sql/item_timefunc.cc: Changed sec_to_time() to take a my_decimal argument to ensure we don't loose any sub seconds. Added Item_temporal_func::get_time() (This simplifies some things) sql/mysql_priv.h: Added Lazy_string_decimal() sql/mysqld.cc: Added my_decimal constants max_seconds_for_time_type, time_second_part_factor sql/table.cc: Changed expr_arena to be of type CONVENTIONAL_EXECUTION to ensure that we don't loose any items that are created by fix_fields() sql/tztime.cc: TIME_to_gmt_sec() now sets *in_dst_time_gap in case of errors This is needed to be able to detect if timestamp is 0 storage/maria/lockman.c: Changed from my_getsystime() to set_timespec_time_nsec() storage/maria/ma_loghandler.c: Changed from my_getsystime() to my_hrtime() storage/maria/ma_recovery.c: Changed from my_getsystime() to mmicrosecond_interval_timer() storage/maria/unittest/trnman-t.c: Changed from my_getsystime() to mmicrosecond_interval_timer() storage/xtradb/handler/ha_innodb.cc: Added support for new time,datetime and timestamp unittest/mysys/thr_template.c: my_getsystime() -> my_interval_timer() unittest/mysys/waiting_threads-t.c: my_getsystime() -> my_interval_timer()
15 years ago
Fixes for CREATE_OR_REPLACE - MDEV-5587 Server crashes in Locked_tables_list::restore_lock on CREATE OR REPLACE .. SELECT under LOCK - MDEV-5586 Assertion `share->tdc.all_tables.is_empty() || remove_type != TDC_RT_REMOVE_ALL' fails in tdc_remove_table - MDEV-5588 Strange error on CREATE OR REPLACE table over an existing view mysql-test/r/create_or_replace.result: Added test cases mysql-test/r/lowercase_view.result: New error message mysql-test/r/merge.result: New error message mysql-test/r/multi_update.result: New error message mysql-test/r/subselect.result: New error message mysql-test/r/subselect_exists_to_in.result: New error message mysql-test/r/subselect_no_mat.result: New error message mysql-test/r/subselect_no_opts.result: New error message mysql-test/r/subselect_no_scache.result: New error message mysql-test/r/subselect_no_semijoin.result: New error message mysql-test/r/view.result: New error message mysql-test/suite/funcs_1/r/myisam_views-big.result: New error message mysql-test/t/create_or_replace.test: New tests mysql-test/t/view.test: New error message sql/share/errmsg-utf8.txt: Added new error message sql/sql_base.cc: Updated error message Do an automatic UNLOCK TABLES if we don't have any locked tables (safety fix) sql/sql_db.cc: Updated arguments sql/sql_load.cc: New error message sql/sql_parse.cc: Check that we are not using a table we are dropping and re-creating sql/sql_table.cc: Added parameter to mysql_rm_table_no_locks() to not automaticly do UNLOCK TABLES Added better error message if trying to drop a view with DROP TABLE Don't try to create something we select from sql/sql_table.h: Updated prototypes
12 years ago
Fixes for CREATE_OR_REPLACE - MDEV-5587 Server crashes in Locked_tables_list::restore_lock on CREATE OR REPLACE .. SELECT under LOCK - MDEV-5586 Assertion `share->tdc.all_tables.is_empty() || remove_type != TDC_RT_REMOVE_ALL' fails in tdc_remove_table - MDEV-5588 Strange error on CREATE OR REPLACE table over an existing view mysql-test/r/create_or_replace.result: Added test cases mysql-test/r/lowercase_view.result: New error message mysql-test/r/merge.result: New error message mysql-test/r/multi_update.result: New error message mysql-test/r/subselect.result: New error message mysql-test/r/subselect_exists_to_in.result: New error message mysql-test/r/subselect_no_mat.result: New error message mysql-test/r/subselect_no_opts.result: New error message mysql-test/r/subselect_no_scache.result: New error message mysql-test/r/subselect_no_semijoin.result: New error message mysql-test/r/view.result: New error message mysql-test/suite/funcs_1/r/myisam_views-big.result: New error message mysql-test/t/create_or_replace.test: New tests mysql-test/t/view.test: New error message sql/share/errmsg-utf8.txt: Added new error message sql/sql_base.cc: Updated error message Do an automatic UNLOCK TABLES if we don't have any locked tables (safety fix) sql/sql_db.cc: Updated arguments sql/sql_load.cc: New error message sql/sql_parse.cc: Check that we are not using a table we are dropping and re-creating sql/sql_table.cc: Added parameter to mysql_rm_table_no_locks() to not automaticly do UNLOCK TABLES Added better error message if trying to drop a view with DROP TABLE Don't try to create something we select from sql/sql_table.h: Updated prototypes
12 years ago
Fixes for CREATE_OR_REPLACE - MDEV-5587 Server crashes in Locked_tables_list::restore_lock on CREATE OR REPLACE .. SELECT under LOCK - MDEV-5586 Assertion `share->tdc.all_tables.is_empty() || remove_type != TDC_RT_REMOVE_ALL' fails in tdc_remove_table - MDEV-5588 Strange error on CREATE OR REPLACE table over an existing view mysql-test/r/create_or_replace.result: Added test cases mysql-test/r/lowercase_view.result: New error message mysql-test/r/merge.result: New error message mysql-test/r/multi_update.result: New error message mysql-test/r/subselect.result: New error message mysql-test/r/subselect_exists_to_in.result: New error message mysql-test/r/subselect_no_mat.result: New error message mysql-test/r/subselect_no_opts.result: New error message mysql-test/r/subselect_no_scache.result: New error message mysql-test/r/subselect_no_semijoin.result: New error message mysql-test/r/view.result: New error message mysql-test/suite/funcs_1/r/myisam_views-big.result: New error message mysql-test/t/create_or_replace.test: New tests mysql-test/t/view.test: New error message sql/share/errmsg-utf8.txt: Added new error message sql/sql_base.cc: Updated error message Do an automatic UNLOCK TABLES if we don't have any locked tables (safety fix) sql/sql_db.cc: Updated arguments sql/sql_load.cc: New error message sql/sql_parse.cc: Check that we are not using a table we are dropping and re-creating sql/sql_table.cc: Added parameter to mysql_rm_table_no_locks() to not automaticly do UNLOCK TABLES Added better error message if trying to drop a view with DROP TABLE Don't try to create something we select from sql/sql_table.h: Updated prototypes
12 years ago
Fixes for CREATE_OR_REPLACE - MDEV-5587 Server crashes in Locked_tables_list::restore_lock on CREATE OR REPLACE .. SELECT under LOCK - MDEV-5586 Assertion `share->tdc.all_tables.is_empty() || remove_type != TDC_RT_REMOVE_ALL' fails in tdc_remove_table - MDEV-5588 Strange error on CREATE OR REPLACE table over an existing view mysql-test/r/create_or_replace.result: Added test cases mysql-test/r/lowercase_view.result: New error message mysql-test/r/merge.result: New error message mysql-test/r/multi_update.result: New error message mysql-test/r/subselect.result: New error message mysql-test/r/subselect_exists_to_in.result: New error message mysql-test/r/subselect_no_mat.result: New error message mysql-test/r/subselect_no_opts.result: New error message mysql-test/r/subselect_no_scache.result: New error message mysql-test/r/subselect_no_semijoin.result: New error message mysql-test/r/view.result: New error message mysql-test/suite/funcs_1/r/myisam_views-big.result: New error message mysql-test/t/create_or_replace.test: New tests mysql-test/t/view.test: New error message sql/share/errmsg-utf8.txt: Added new error message sql/sql_base.cc: Updated error message Do an automatic UNLOCK TABLES if we don't have any locked tables (safety fix) sql/sql_db.cc: Updated arguments sql/sql_load.cc: New error message sql/sql_parse.cc: Check that we are not using a table we are dropping and re-creating sql/sql_table.cc: Added parameter to mysql_rm_table_no_locks() to not automaticly do UNLOCK TABLES Added better error message if trying to drop a view with DROP TABLE Don't try to create something we select from sql/sql_table.h: Updated prototypes
12 years ago
Fixes for CREATE_OR_REPLACE - MDEV-5587 Server crashes in Locked_tables_list::restore_lock on CREATE OR REPLACE .. SELECT under LOCK - MDEV-5586 Assertion `share->tdc.all_tables.is_empty() || remove_type != TDC_RT_REMOVE_ALL' fails in tdc_remove_table - MDEV-5588 Strange error on CREATE OR REPLACE table over an existing view mysql-test/r/create_or_replace.result: Added test cases mysql-test/r/lowercase_view.result: New error message mysql-test/r/merge.result: New error message mysql-test/r/multi_update.result: New error message mysql-test/r/subselect.result: New error message mysql-test/r/subselect_exists_to_in.result: New error message mysql-test/r/subselect_no_mat.result: New error message mysql-test/r/subselect_no_opts.result: New error message mysql-test/r/subselect_no_scache.result: New error message mysql-test/r/subselect_no_semijoin.result: New error message mysql-test/r/view.result: New error message mysql-test/suite/funcs_1/r/myisam_views-big.result: New error message mysql-test/t/create_or_replace.test: New tests mysql-test/t/view.test: New error message sql/share/errmsg-utf8.txt: Added new error message sql/sql_base.cc: Updated error message Do an automatic UNLOCK TABLES if we don't have any locked tables (safety fix) sql/sql_db.cc: Updated arguments sql/sql_load.cc: New error message sql/sql_parse.cc: Check that we are not using a table we are dropping and re-creating sql/sql_table.cc: Added parameter to mysql_rm_table_no_locks() to not automaticly do UNLOCK TABLES Added better error message if trying to drop a view with DROP TABLE Don't try to create something we select from sql/sql_table.h: Updated prototypes
12 years ago
14 years ago
MDEV-12775 Reuse data type aggregation code for hybrid functions and UNION Introducing a new class Type_holder (used internally in sql_union.cc), to reuse exactly the same data type attribute aggregation Type_handler API for hybrid functions and UNION. This fixes a number of bugs in UNION: - MDEV-9495 Wrong field type for a UNION of a signed and an unsigned INT expression - MDEV-9497 UNION and COALESCE produce different field types for DECIMAL+INT - MDEV-12594 UNION between fixed length double columns does not always preserve scale - MDEV-12595 UNION converts INT to BIGINT - MDEV-12599 UNION is not symmetric when mixing INT and CHAR Details: - sql_union.cc: Reusing attribute aggregation for UNION. Adding new methods: * st_select_lex_unit::join_union_type_handlers() * st_select_lex_unit::join_union_type_attributes() * st_select_lex_unit::join_union_item_types() Removing the old join_types()-based code. - Changing Type_handler::Item_hybrid_func_fix_attributes() to accept "name", Type_handler_hybrid_field_type, Type_all_attributes as three separate parameters instead of a single Item_hybrid_func parameter, to make it possible to pass both Item_hybrid_func and Type_holder. - Moving the former special GEOMETRY and ENUM/SET attribute aggregation code from Item_type_holder::join_types() to * Type_handler_typelib::Item_hybrid_func_fix_attributes(). * Type_handler_geometry::Item_hybrid_func_fix_attrubutes(). This makes GEOMETRY/ENUM/SET symmetric with all other data types (from the UNION point of view). Removing Item_type_holder::join_types() and Item_type_holder::get_full_info(). - Adding new methods into Type_all_attributes: * Type_all_attributes::set_geometry_type() and Item_hybrid_func::set_geometry_type(). * Adding Type_all_attributes::get_typelib(). * Adding Type_all_attributes::set_typelib(). - Adding Type_handler_typelib as a common parent for Type_handler_enum and Type_handler_set, to avoid code duplication: they have already had two common methods, and we're adding one more shared method. - Adding Type_all_attributes::set_maybe_null(), as some type handlers may want to set maybe_null (e.g. Type_handler_geometry) during data type attribute aggregation. - Changing Type_geometry_attributes() to accept Type_handler and Type_all_attributes as two separate parameters, instead of a single Item parameter, to make it possible to pass Type_holder. - Adding Item_args::add_argument(). - Moving Item_args::alloc_arguments() from "protected" to "public". - Moving Item_type_holder::Item_type_holder() from item.cc to item.h, as now it's very simple. Btw, this constructor should probably be eventually removed. It's now used only in sql_show.cc, which could be modified to use Item_return_decimal (for symmetry with Item_return_xxx created for all other data types). Or, another option: remove all Item_return_xxx and use Item_type_holder for all data types instead. - storage/tokudb/mysql-test/tokudb/r/type_float.result Recording new results (MDEV-12594). - mysql-test/r/cte_recursive.result Recording new results (MDEV-9497) - mysql-test/r/subselect*.result Recording new results (MDEV-12595) - mysql-test/r/metadata.result Recording new results (MDEV-9495) - mysql-test/r/temp_table.result Recording new results (MDEV-12594) - mysql-test/r/type_float.result Recording new results (MDEV-12594)
9 years ago
Back-ported the patch of the mysql-5.6 code line that fixed several defects in the greedy optimization: 1) The greedy optimizer calculated the 'compare-cost' (CPU-cost) for iterating over the partial plan result at each level in the query plan as 'record_count / (double) TIME_FOR_COMPARE' This cost was only used locally for 'best' calculation at each level, and *not* accumulated into the total cost for the query plan. This fix added the 'CPU-cost' of processing 'current_record_count' records at each level to 'current_read_time' *before* it is used as 'accumulated cost' argument to recursive best_extension_by_limited_search() calls. This ensured that the cost of a huge join-fanout early in the QEP was correctly reflected in the cost of the final QEP. To get identical cost for a 'best' optimized query and a straight_join with the same join order, the same change was also applied to optimize_straight_join() and get_partial_join_cost() 2) Furthermore to get equal cost for 'best' optimized query and a straight_join the new code substrcated the same '0.001' in optimize_straight_join() as it had been already done in best_extension_by_limited_search() 3) When best_extension_by_limited_search() aggregated the 'best' plan a plan was 'best' by the check : 'if ((search_depth == 1) || (current_read_time < join->best_read))' The term '(search_depth == 1' incorrectly caused a new best plan to be collected whenever the specified 'search_depth' was reached - even if this partial query plan was more expensive than what we had already found.
14 years ago
14 years ago
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
Patch for mdev-287: CHEAP SQ: A query with subquery in SELECT list, EXISTS, inner joins takes hundreds times longer Analysis: The fix for lp:944706 introduces early subquery optimization. While a subquery is being optimized some of its predicates may be removed. In the test case, the EXISTS subquery is constant, and is evaluated to TRUE. As a result the whole OR is TRUE, and thus the correlated condition "b = alias1.b" is optimized away. The subquery becomes non-correlated. The subquery cache is designed to work only for correlated subqueries. If constant subquery optimization is disallowed, then the constant subquery is not evaluated, the subquery remains correlated, and its execution is cached. As a result execution is fast. However, when the constant subquery was optimized away, it was neither cached by the subquery cache, nor it was cached by the internal subquery caching. The latter was due to the fact that the subquery still appeared as correlated to the subselect_XYZ_engine::exec methods, and they re-executed the subquery on each call to Item_subselect::exec. Solution: The solution is to update the correlated status of the subquery after it has been optimized. This status consists of: - st_select_lex::is_correlated - Item_subselect::is_correlated - SELECT_LEX::uncacheable - SELECT_LEX_UNIT::uncacheable The status is updated by st_select_lex::update_correlated_cache(), and its caller st_select_lex::optimize_unflattened_subqueries. The solution relies on the fact that the optimizer already called st_select_lex::update_used_tables() for each subquery. This allows to efficiently update the correlated status of each subquery without walking the whole subquery tree. Notice that his patch is an improvement over MySQL 5.6 and older, where subqueries are not pre-optimized, and the above analysis is not possible.
14 years ago
Patch for mdev-287: CHEAP SQ: A query with subquery in SELECT list, EXISTS, inner joins takes hundreds times longer Analysis: The fix for lp:944706 introduces early subquery optimization. While a subquery is being optimized some of its predicates may be removed. In the test case, the EXISTS subquery is constant, and is evaluated to TRUE. As a result the whole OR is TRUE, and thus the correlated condition "b = alias1.b" is optimized away. The subquery becomes non-correlated. The subquery cache is designed to work only for correlated subqueries. If constant subquery optimization is disallowed, then the constant subquery is not evaluated, the subquery remains correlated, and its execution is cached. As a result execution is fast. However, when the constant subquery was optimized away, it was neither cached by the subquery cache, nor it was cached by the internal subquery caching. The latter was due to the fact that the subquery still appeared as correlated to the subselect_XYZ_engine::exec methods, and they re-executed the subquery on each call to Item_subselect::exec. Solution: The solution is to update the correlated status of the subquery after it has been optimized. This status consists of: - st_select_lex::is_correlated - Item_subselect::is_correlated - SELECT_LEX::uncacheable - SELECT_LEX_UNIT::uncacheable The status is updated by st_select_lex::update_correlated_cache(), and its caller st_select_lex::optimize_unflattened_subqueries. The solution relies on the fact that the optimizer already called st_select_lex::update_used_tables() for each subquery. This allows to efficiently update the correlated status of each subquery without walking the whole subquery tree. Notice that his patch is an improvement over MySQL 5.6 and older, where subqueries are not pre-optimized, and the above analysis is not possible.
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
Patch for mdev-287: CHEAP SQ: A query with subquery in SELECT list, EXISTS, inner joins takes hundreds times longer Analysis: The fix for lp:944706 introduces early subquery optimization. While a subquery is being optimized some of its predicates may be removed. In the test case, the EXISTS subquery is constant, and is evaluated to TRUE. As a result the whole OR is TRUE, and thus the correlated condition "b = alias1.b" is optimized away. The subquery becomes non-correlated. The subquery cache is designed to work only for correlated subqueries. If constant subquery optimization is disallowed, then the constant subquery is not evaluated, the subquery remains correlated, and its execution is cached. As a result execution is fast. However, when the constant subquery was optimized away, it was neither cached by the subquery cache, nor it was cached by the internal subquery caching. The latter was due to the fact that the subquery still appeared as correlated to the subselect_XYZ_engine::exec methods, and they re-executed the subquery on each call to Item_subselect::exec. Solution: The solution is to update the correlated status of the subquery after it has been optimized. This status consists of: - st_select_lex::is_correlated - Item_subselect::is_correlated - SELECT_LEX::uncacheable - SELECT_LEX_UNIT::uncacheable The status is updated by st_select_lex::update_correlated_cache(), and its caller st_select_lex::optimize_unflattened_subqueries. The solution relies on the fact that the optimizer already called st_select_lex::update_used_tables() for each subquery. This allows to efficiently update the correlated status of each subquery without walking the whole subquery tree. Notice that his patch is an improvement over MySQL 5.6 and older, where subqueries are not pre-optimized, and the above analysis is not possible.
14 years ago
Fix bug lp:985667, MDEV-229 Analysis: The reason for the wrong result is the interaction between constant optimization (in this case 1-row table) and subquery optimization. - First the outer query is optimized, and 'make_join_statistics' finds that table t2 has one row, reads that row, and marks the whole table as constant. This also means that all fields of t2 are constant. - Next, we optimize the subquery in the end of the outer 'make_join_statistics'. The field 'f2' is considered constant, with value '3'. The subquery predicate is rewritten as the constant TRUE. - The outer query execution detects early that the whole query result is empty and calls 'return_zero_rows'. Since the query is with implicit grouping, we have to produce one row with special values for the aggregates (depending on each aggregate function), and NULL values for all non-aggregate fields. This function calls 'no_rows_in_result' to set each aggregate function to the default value when it aggregates over an empty result, and then calls 'send_data', which in turn evaluates each Item in the SELECT list. - When evaluation reaches the subquery predicate, it executes the subquery with field 'f2' having a constant value '3', and the subquery produces the incorrect result '7'. Solution: Implement Item::no_rows_in_result for all subquery predicates. In order to make this work, it is also needed to make all val_* methods of all subquery predicates respect the Item_subselect::forced_const flag. Otherwise subqueries are executed anyways, and override the default value set by no_rows_in_result with whatever result is produced from the subquery evaluation.
14 years ago
14 years ago
Back-ported the patch of the mysql-5.6 code line that fixed several defects in the greedy optimization: 1) The greedy optimizer calculated the 'compare-cost' (CPU-cost) for iterating over the partial plan result at each level in the query plan as 'record_count / (double) TIME_FOR_COMPARE' This cost was only used locally for 'best' calculation at each level, and *not* accumulated into the total cost for the query plan. This fix added the 'CPU-cost' of processing 'current_record_count' records at each level to 'current_read_time' *before* it is used as 'accumulated cost' argument to recursive best_extension_by_limited_search() calls. This ensured that the cost of a huge join-fanout early in the QEP was correctly reflected in the cost of the final QEP. To get identical cost for a 'best' optimized query and a straight_join with the same join order, the same change was also applied to optimize_straight_join() and get_partial_join_cost() 2) Furthermore to get equal cost for 'best' optimized query and a straight_join the new code substrcated the same '0.001' in optimize_straight_join() as it had been already done in best_extension_by_limited_search() 3) When best_extension_by_limited_search() aggregated the 'best' plan a plan was 'best' by the check : 'if ((search_depth == 1) || (current_read_time < join->best_read))' The term '(search_depth == 1' incorrectly caused a new best plan to be collected whenever the specified 'search_depth' was reached - even if this partial query plan was more expensive than what we had already found.
14 years ago
Back-ported the patch of the mysql-5.6 code line that fixed several defects in the greedy optimization: 1) The greedy optimizer calculated the 'compare-cost' (CPU-cost) for iterating over the partial plan result at each level in the query plan as 'record_count / (double) TIME_FOR_COMPARE' This cost was only used locally for 'best' calculation at each level, and *not* accumulated into the total cost for the query plan. This fix added the 'CPU-cost' of processing 'current_record_count' records at each level to 'current_read_time' *before* it is used as 'accumulated cost' argument to recursive best_extension_by_limited_search() calls. This ensured that the cost of a huge join-fanout early in the QEP was correctly reflected in the cost of the final QEP. To get identical cost for a 'best' optimized query and a straight_join with the same join order, the same change was also applied to optimize_straight_join() and get_partial_join_cost() 2) Furthermore to get equal cost for 'best' optimized query and a straight_join the new code substrcated the same '0.001' in optimize_straight_join() as it had been already done in best_extension_by_limited_search() 3) When best_extension_by_limited_search() aggregated the 'best' plan a plan was 'best' by the check : 'if ((search_depth == 1) || (current_read_time < join->best_read))' The term '(search_depth == 1' incorrectly caused a new best plan to be collected whenever the specified 'search_depth' was reached - even if this partial query plan was more expensive than what we had already found.
14 years ago
Back-ported the patch of the mysql-5.6 code line that fixed several defects in the greedy optimization: 1) The greedy optimizer calculated the 'compare-cost' (CPU-cost) for iterating over the partial plan result at each level in the query plan as 'record_count / (double) TIME_FOR_COMPARE' This cost was only used locally for 'best' calculation at each level, and *not* accumulated into the total cost for the query plan. This fix added the 'CPU-cost' of processing 'current_record_count' records at each level to 'current_read_time' *before* it is used as 'accumulated cost' argument to recursive best_extension_by_limited_search() calls. This ensured that the cost of a huge join-fanout early in the QEP was correctly reflected in the cost of the final QEP. To get identical cost for a 'best' optimized query and a straight_join with the same join order, the same change was also applied to optimize_straight_join() and get_partial_join_cost() 2) Furthermore to get equal cost for 'best' optimized query and a straight_join the new code substrcated the same '0.001' in optimize_straight_join() as it had been already done in best_extension_by_limited_search() 3) When best_extension_by_limited_search() aggregated the 'best' plan a plan was 'best' by the check : 'if ((search_depth == 1) || (current_read_time < join->best_read))' The term '(search_depth == 1' incorrectly caused a new best plan to be collected whenever the specified 'search_depth' was reached - even if this partial query plan was more expensive than what we had already found.
14 years ago
14 years ago
14 years ago
13 years ago
13 years ago
13 years ago
13 years ago
14 years ago
14 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
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:985667, MDEV-229 Analysis: The reason for the wrong result is the interaction between constant optimization (in this case 1-row table) and subquery optimization. - First the outer query is optimized, and 'make_join_statistics' finds that table t2 has one row, reads that row, and marks the whole table as constant. This also means that all fields of t2 are constant. - Next, we optimize the subquery in the end of the outer 'make_join_statistics'. The field 'f2' is considered constant, with value '3'. The subquery predicate is rewritten as the constant TRUE. - The outer query execution detects early that the whole query result is empty and calls 'return_zero_rows'. Since the query is with implicit grouping, we have to produce one row with special values for the aggregates (depending on each aggregate function), and NULL values for all non-aggregate fields. This function calls 'no_rows_in_result' to set each aggregate function to the default value when it aggregates over an empty result, and then calls 'send_data', which in turn evaluates each Item in the SELECT list. - When evaluation reaches the subquery predicate, it executes the subquery with field 'f2' having a constant value '3', and the subquery produces the incorrect result '7'. Solution: Implement Item::no_rows_in_result for all subquery predicates. In order to make this work, it is also needed to make all val_* methods of all subquery predicates respect the Item_subselect::forced_const flag. Otherwise subqueries are executed anyways, and override the default value set by no_rows_in_result with whatever result is produced from the subquery evaluation.
14 years ago
Patch for mdev-287: CHEAP SQ: A query with subquery in SELECT list, EXISTS, inner joins takes hundreds times longer Analysis: The fix for lp:944706 introduces early subquery optimization. While a subquery is being optimized some of its predicates may be removed. In the test case, the EXISTS subquery is constant, and is evaluated to TRUE. As a result the whole OR is TRUE, and thus the correlated condition "b = alias1.b" is optimized away. The subquery becomes non-correlated. The subquery cache is designed to work only for correlated subqueries. If constant subquery optimization is disallowed, then the constant subquery is not evaluated, the subquery remains correlated, and its execution is cached. As a result execution is fast. However, when the constant subquery was optimized away, it was neither cached by the subquery cache, nor it was cached by the internal subquery caching. The latter was due to the fact that the subquery still appeared as correlated to the subselect_XYZ_engine::exec methods, and they re-executed the subquery on each call to Item_subselect::exec. Solution: The solution is to update the correlated status of the subquery after it has been optimized. This status consists of: - st_select_lex::is_correlated - Item_subselect::is_correlated - SELECT_LEX::uncacheable - SELECT_LEX_UNIT::uncacheable The status is updated by st_select_lex::update_correlated_cache(), and its caller st_select_lex::optimize_unflattened_subqueries. The solution relies on the fact that the optimizer already called st_select_lex::update_used_tables() for each subquery. This allows to efficiently update the correlated status of each subquery without walking the whole subquery tree. Notice that his patch is an improvement over MySQL 5.6 and older, where subqueries are not pre-optimized, and the above analysis is not possible.
14 years ago
Fix bug lp:985667, MDEV-229 Analysis: The reason for the wrong result is the interaction between constant optimization (in this case 1-row table) and subquery optimization. - First the outer query is optimized, and 'make_join_statistics' finds that table t2 has one row, reads that row, and marks the whole table as constant. This also means that all fields of t2 are constant. - Next, we optimize the subquery in the end of the outer 'make_join_statistics'. The field 'f2' is considered constant, with value '3'. The subquery predicate is rewritten as the constant TRUE. - The outer query execution detects early that the whole query result is empty and calls 'return_zero_rows'. Since the query is with implicit grouping, we have to produce one row with special values for the aggregates (depending on each aggregate function), and NULL values for all non-aggregate fields. This function calls 'no_rows_in_result' to set each aggregate function to the default value when it aggregates over an empty result, and then calls 'send_data', which in turn evaluates each Item in the SELECT list. - When evaluation reaches the subquery predicate, it executes the subquery with field 'f2' having a constant value '3', and the subquery produces the incorrect result '7'. Solution: Implement Item::no_rows_in_result for all subquery predicates. In order to make this work, it is also needed to make all val_* methods of all subquery predicates respect the Item_subselect::forced_const flag. Otherwise subqueries are executed anyways, and override the default value set by no_rows_in_result with whatever result is produced from the subquery evaluation.
14 years ago
Patch for mdev-287: CHEAP SQ: A query with subquery in SELECT list, EXISTS, inner joins takes hundreds times longer Analysis: The fix for lp:944706 introduces early subquery optimization. While a subquery is being optimized some of its predicates may be removed. In the test case, the EXISTS subquery is constant, and is evaluated to TRUE. As a result the whole OR is TRUE, and thus the correlated condition "b = alias1.b" is optimized away. The subquery becomes non-correlated. The subquery cache is designed to work only for correlated subqueries. If constant subquery optimization is disallowed, then the constant subquery is not evaluated, the subquery remains correlated, and its execution is cached. As a result execution is fast. However, when the constant subquery was optimized away, it was neither cached by the subquery cache, nor it was cached by the internal subquery caching. The latter was due to the fact that the subquery still appeared as correlated to the subselect_XYZ_engine::exec methods, and they re-executed the subquery on each call to Item_subselect::exec. Solution: The solution is to update the correlated status of the subquery after it has been optimized. This status consists of: - st_select_lex::is_correlated - Item_subselect::is_correlated - SELECT_LEX::uncacheable - SELECT_LEX_UNIT::uncacheable The status is updated by st_select_lex::update_correlated_cache(), and its caller st_select_lex::optimize_unflattened_subqueries. The solution relies on the fact that the optimizer already called st_select_lex::update_used_tables() for each subquery. This allows to efficiently update the correlated status of each subquery without walking the whole subquery tree. Notice that his patch is an improvement over MySQL 5.6 and older, where subqueries are not pre-optimized, and the above analysis is not possible.
14 years ago
Fix bug lp:985667, MDEV-229 Analysis: The reason for the wrong result is the interaction between constant optimization (in this case 1-row table) and subquery optimization. - First the outer query is optimized, and 'make_join_statistics' finds that table t2 has one row, reads that row, and marks the whole table as constant. This also means that all fields of t2 are constant. - Next, we optimize the subquery in the end of the outer 'make_join_statistics'. The field 'f2' is considered constant, with value '3'. The subquery predicate is rewritten as the constant TRUE. - The outer query execution detects early that the whole query result is empty and calls 'return_zero_rows'. Since the query is with implicit grouping, we have to produce one row with special values for the aggregates (depending on each aggregate function), and NULL values for all non-aggregate fields. This function calls 'no_rows_in_result' to set each aggregate function to the default value when it aggregates over an empty result, and then calls 'send_data', which in turn evaluates each Item in the SELECT list. - When evaluation reaches the subquery predicate, it executes the subquery with field 'f2' having a constant value '3', and the subquery produces the incorrect result '7'. Solution: Implement Item::no_rows_in_result for all subquery predicates. In order to make this work, it is also needed to make all val_* methods of all subquery predicates respect the Item_subselect::forced_const flag. Otherwise subqueries are executed anyways, and override the default value set by no_rows_in_result with whatever result is produced from the subquery evaluation.
14 years ago
Fix bug lp:1008686 Analysis: The fix for bug lp:985667 implements the method Item_subselect::no_rows_in_result() for all main kinds of subqueries. The purpose of this method is to be called from return_zero_rows() and set Items to some default value in the case when a query returns no rows. Aggregates and subqueries require special treatment in this case. Every implementation of Item_subselect::no_rows_in_result() called Item_subselect::make_const() to set the subquery predicate to its default value irrespective of where the predicate was located in the query. Once the predicate was set to a constant it was never executed. At the same time, the JOIN object of the fake select for UNIONs (the one used for the final result of the UNION), was set after all subqueries in the union were executed. Since we set the subquery as constant, it was never executed, and the corresponding JOIN was never created. In order to decide whether the result of NOT IN is NULL or FALSE, Item_in_optimizer needs to check if the subquery result was empty or not. This is where we got the crash, because subselect_union_engine::no_rows() checks for unit->fake_select_lex->join->send_records, and the join object was NULL. Solution: If a subquery is in the HAVING clause it must be evaluated in order to know its result, so that we can properly filter the result records. Once subqueries in the HAVING clause are executed even in the case of no result rows, this specific crash will be solved, because the UNION will be executed, and its JOIN will be constructed. Therefore the fix for this crash is to narrow the fix for lp:985667, and to apply Item_subselect::no_rows_in_result() only when the subquery predicate is in the SELECT clause.
14 years ago
13 years ago
13 years ago
13 years ago
Patch for mdev-287: CHEAP SQ: A query with subquery in SELECT list, EXISTS, inner joins takes hundreds times longer Analysis: The fix for lp:944706 introduces early subquery optimization. While a subquery is being optimized some of its predicates may be removed. In the test case, the EXISTS subquery is constant, and is evaluated to TRUE. As a result the whole OR is TRUE, and thus the correlated condition "b = alias1.b" is optimized away. The subquery becomes non-correlated. The subquery cache is designed to work only for correlated subqueries. If constant subquery optimization is disallowed, then the constant subquery is not evaluated, the subquery remains correlated, and its execution is cached. As a result execution is fast. However, when the constant subquery was optimized away, it was neither cached by the subquery cache, nor it was cached by the internal subquery caching. The latter was due to the fact that the subquery still appeared as correlated to the subselect_XYZ_engine::exec methods, and they re-executed the subquery on each call to Item_subselect::exec. Solution: The solution is to update the correlated status of the subquery after it has been optimized. This status consists of: - st_select_lex::is_correlated - Item_subselect::is_correlated - SELECT_LEX::uncacheable - SELECT_LEX_UNIT::uncacheable The status is updated by st_select_lex::update_correlated_cache(), and its caller st_select_lex::optimize_unflattened_subqueries. The solution relies on the fact that the optimizer already called st_select_lex::update_used_tables() for each subquery. This allows to efficiently update the correlated status of each subquery without walking the whole subquery tree. Notice that his patch is an improvement over MySQL 5.6 and older, where subqueries are not pre-optimized, and the above analysis is not possible.
14 years ago
Patch for mdev-287: CHEAP SQ: A query with subquery in SELECT list, EXISTS, inner joins takes hundreds times longer Analysis: The fix for lp:944706 introduces early subquery optimization. While a subquery is being optimized some of its predicates may be removed. In the test case, the EXISTS subquery is constant, and is evaluated to TRUE. As a result the whole OR is TRUE, and thus the correlated condition "b = alias1.b" is optimized away. The subquery becomes non-correlated. The subquery cache is designed to work only for correlated subqueries. If constant subquery optimization is disallowed, then the constant subquery is not evaluated, the subquery remains correlated, and its execution is cached. As a result execution is fast. However, when the constant subquery was optimized away, it was neither cached by the subquery cache, nor it was cached by the internal subquery caching. The latter was due to the fact that the subquery still appeared as correlated to the subselect_XYZ_engine::exec methods, and they re-executed the subquery on each call to Item_subselect::exec. Solution: The solution is to update the correlated status of the subquery after it has been optimized. This status consists of: - st_select_lex::is_correlated - Item_subselect::is_correlated - SELECT_LEX::uncacheable - SELECT_LEX_UNIT::uncacheable The status is updated by st_select_lex::update_correlated_cache(), and its caller st_select_lex::optimize_unflattened_subqueries. The solution relies on the fact that the optimizer already called st_select_lex::update_used_tables() for each subquery. This allows to efficiently update the correlated status of each subquery without walking the whole subquery tree. Notice that his patch is an improvement over MySQL 5.6 and older, where subqueries are not pre-optimized, and the above analysis is not possible.
14 years ago
Patch for mdev-287: CHEAP SQ: A query with subquery in SELECT list, EXISTS, inner joins takes hundreds times longer Analysis: The fix for lp:944706 introduces early subquery optimization. While a subquery is being optimized some of its predicates may be removed. In the test case, the EXISTS subquery is constant, and is evaluated to TRUE. As a result the whole OR is TRUE, and thus the correlated condition "b = alias1.b" is optimized away. The subquery becomes non-correlated. The subquery cache is designed to work only for correlated subqueries. If constant subquery optimization is disallowed, then the constant subquery is not evaluated, the subquery remains correlated, and its execution is cached. As a result execution is fast. However, when the constant subquery was optimized away, it was neither cached by the subquery cache, nor it was cached by the internal subquery caching. The latter was due to the fact that the subquery still appeared as correlated to the subselect_XYZ_engine::exec methods, and they re-executed the subquery on each call to Item_subselect::exec. Solution: The solution is to update the correlated status of the subquery after it has been optimized. This status consists of: - st_select_lex::is_correlated - Item_subselect::is_correlated - SELECT_LEX::uncacheable - SELECT_LEX_UNIT::uncacheable The status is updated by st_select_lex::update_correlated_cache(), and its caller st_select_lex::optimize_unflattened_subqueries. The solution relies on the fact that the optimizer already called st_select_lex::update_used_tables() for each subquery. This allows to efficiently update the correlated status of each subquery without walking the whole subquery tree. Notice that his patch is an improvement over MySQL 5.6 and older, where subqueries are not pre-optimized, and the above analysis is not possible.
14 years ago
Patch for mdev-287: CHEAP SQ: A query with subquery in SELECT list, EXISTS, inner joins takes hundreds times longer Analysis: The fix for lp:944706 introduces early subquery optimization. While a subquery is being optimized some of its predicates may be removed. In the test case, the EXISTS subquery is constant, and is evaluated to TRUE. As a result the whole OR is TRUE, and thus the correlated condition "b = alias1.b" is optimized away. The subquery becomes non-correlated. The subquery cache is designed to work only for correlated subqueries. If constant subquery optimization is disallowed, then the constant subquery is not evaluated, the subquery remains correlated, and its execution is cached. As a result execution is fast. However, when the constant subquery was optimized away, it was neither cached by the subquery cache, nor it was cached by the internal subquery caching. The latter was due to the fact that the subquery still appeared as correlated to the subselect_XYZ_engine::exec methods, and they re-executed the subquery on each call to Item_subselect::exec. Solution: The solution is to update the correlated status of the subquery after it has been optimized. This status consists of: - st_select_lex::is_correlated - Item_subselect::is_correlated - SELECT_LEX::uncacheable - SELECT_LEX_UNIT::uncacheable The status is updated by st_select_lex::update_correlated_cache(), and its caller st_select_lex::optimize_unflattened_subqueries. The solution relies on the fact that the optimizer already called st_select_lex::update_used_tables() for each subquery. This allows to efficiently update the correlated status of each subquery without walking the whole subquery tree. Notice that his patch is an improvement over MySQL 5.6 and older, where subqueries are not pre-optimized, and the above analysis is not possible.
14 years ago
Patch for mdev-287: CHEAP SQ: A query with subquery in SELECT list, EXISTS, inner joins takes hundreds times longer Analysis: The fix for lp:944706 introduces early subquery optimization. While a subquery is being optimized some of its predicates may be removed. In the test case, the EXISTS subquery is constant, and is evaluated to TRUE. As a result the whole OR is TRUE, and thus the correlated condition "b = alias1.b" is optimized away. The subquery becomes non-correlated. The subquery cache is designed to work only for correlated subqueries. If constant subquery optimization is disallowed, then the constant subquery is not evaluated, the subquery remains correlated, and its execution is cached. As a result execution is fast. However, when the constant subquery was optimized away, it was neither cached by the subquery cache, nor it was cached by the internal subquery caching. The latter was due to the fact that the subquery still appeared as correlated to the subselect_XYZ_engine::exec methods, and they re-executed the subquery on each call to Item_subselect::exec. Solution: The solution is to update the correlated status of the subquery after it has been optimized. This status consists of: - st_select_lex::is_correlated - Item_subselect::is_correlated - SELECT_LEX::uncacheable - SELECT_LEX_UNIT::uncacheable The status is updated by st_select_lex::update_correlated_cache(), and its caller st_select_lex::optimize_unflattened_subqueries. The solution relies on the fact that the optimizer already called st_select_lex::update_used_tables() for each subquery. This allows to efficiently update the correlated status of each subquery without walking the whole subquery tree. Notice that his patch is an improvement over MySQL 5.6 and older, where subqueries are not pre-optimized, and the above analysis is not possible.
14 years ago
Fixed the bug mdev-12564. Here's what started happening after the patch that fixed the bug mdev-10454 with query reported for the bug SELECT * FROM t t1 right JOIN t t2 ON (t2.pk = t1.pk) WHERE (t2.i, t2.pk) NOT IN ( SELECT t3.i, t3.i FROM t t3, t t4 ) AND t1.c = 'foo'; The patch added an implementation of propagate_equal_fields() for the class Item_row and thus opened the possibility of equal fields substitutions. At the prepare stage after setup_conds() called for WHERE condition had completed the flag of maybe_null of the Item_row object created for (t2.i, t2.pk) was set to false, because the maybe_null flags of both elements were set to false. However the flag of maybe_null for t1.pk from the ON condition were set to true, because t1 was an inner table of an outer join. At the optimization stage the outer join was converted to inner join, but the maybe_null flags were not corrected and remained the same. So after the substitution t2.pk/t1.pk. the maybe_null flag for the row remained false while the maybe_flag for the second element of the row was true. As a result, when the in-to_exists transformation was performed for the NOT IN predicate the guards variables were not created for the elements of the row, but a guard object for the second element was created. The object were not valid because it referred to NULL as a guard variable. This ultimately caused a crash when the expression with the guard was evaluated at the execution stage. The patch made sure that the guard objects are not created without guard variables. Yet it does not resolve the problem of inconsistent maybe_null flags. and it might be that the problem will pop op in other pieces of code. The resolution of this problem is not easy, but the problem should be resolved in future versions.
9 years ago
  1. set @optimizer_switch_for_subselect_test='semijoin=off,mrr=on,mrr_sort_keys=on,index_condition_pushdown=on';
  2. set join_cache_level=1;
  3. set @join_cache_level_for_subselect_test=@@join_cache_level;
  4. drop table if exists t1,t2,t3,t4,t5,t6,t7,t8,t11,t12;
  5. drop view if exists v2;
  6. call mtr.add_suppression("Sort aborted.*");
  7. set @subselect_tmp=@@optimizer_switch;
  8. set @@optimizer_switch=ifnull(@optimizer_switch_for_subselect_test,
  9. "semijoin=on,firstmatch=on,loosescan=on,semijoin_with_cache=on,partial_match_rowid_merge=off,partial_match_table_scan=off");
  10. set join_cache_level=@join_cache_level_for_subselect_test;
  11. set optimizer_switch='mrr=on,mrr_sort_keys=on,index_condition_pushdown=on';
  12. select (select 2);
  13. (select 2)
  14. 2
  15. explain extended select (select 2);
  16. id select_type table type possible_keys key key_len ref rows filtered Extra
  17. 1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  18. Warnings:
  19. Note 1249 Select 2 was reduced during optimization
  20. Note 1003 select 2 AS `(select 2)`
  21. SELECT (SELECT 1) UNION SELECT (SELECT 2);
  22. (SELECT 1)
  23. 1
  24. 2
  25. explain extended SELECT (SELECT 1) UNION SELECT (SELECT 2);
  26. id select_type table type possible_keys key key_len ref rows filtered Extra
  27. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  28. 3 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  29. NULL UNION RESULT <union1,3> ALL NULL NULL NULL NULL NULL NULL
  30. Warnings:
  31. Note 1249 Select 2 was reduced during optimization
  32. Note 1249 Select 4 was reduced during optimization
  33. Note 1003 /* select#1 */ select 1 AS `(SELECT 1)` union /* select#3 */ select 2 AS `(SELECT 2)`
  34. SELECT (SELECT (SELECT 0 UNION SELECT 0));
  35. (SELECT (SELECT 0 UNION SELECT 0))
  36. 0
  37. explain extended SELECT (SELECT (SELECT 0 UNION SELECT 0));
  38. id select_type table type possible_keys key key_len ref rows filtered Extra
  39. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  40. 3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  41. 4 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  42. NULL UNION RESULT <union3,4> ALL NULL NULL NULL NULL NULL NULL
  43. Warnings:
  44. Note 1249 Select 2 was reduced during optimization
  45. Note 1003 /* select#1 */ select (/* select#3 */ select 0 union /* select#4 */ select 0) AS `(SELECT (SELECT 0 UNION SELECT 0))`
  46. SELECT (SELECT 1 FROM (SELECT 1) as b HAVING a=1) as a;
  47. ERROR 42S22: Reference 'a' not supported (forward reference in item list)
  48. 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;
  49. ERROR 42S22: Reference 'b' not supported (forward reference in item list)
  50. SELECT (SELECT 1),MAX(1) FROM (SELECT 1) as a;
  51. (SELECT 1) MAX(1)
  52. 1 1
  53. SELECT (SELECT a) as a;
  54. ERROR 42S22: Reference 'a' not supported (forward reference in item list)
  55. EXPLAIN EXTENDED SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1;
  56. id select_type table type possible_keys key key_len ref rows filtered Extra
  57. 1 PRIMARY <derived2> system NULL NULL NULL NULL 1 100.00
  58. 3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  59. 2 DERIVED NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  60. Warnings:
  61. Note 1276 Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1
  62. Note 1276 Field or reference 'b.a' of SELECT #3 was resolved in SELECT #1
  63. Note 1003 /* select#1 */ select 1 AS `1` from dual having (/* select#3 */ select 1) = 1
  64. SELECT 1 FROM (SELECT 1 as a) as b HAVING (SELECT a)=1;
  65. 1
  66. 1
  67. SELECT (SELECT 1), a;
  68. ERROR 42S22: Unknown column 'a' in 'field list'
  69. SELECT 1 as a FROM (SELECT 1) as b HAVING (SELECT a)=1;
  70. a
  71. 1
  72. SELECT 1 FROM (SELECT (SELECT a) b) c;
  73. ERROR 42S22: Unknown column 'a' in 'field list'
  74. SELECT * FROM (SELECT 1 as id) b WHERE id IN (SELECT * FROM (SELECT 1 as id) c ORDER BY id);
  75. id
  76. 1
  77. SELECT * FROM (SELECT 1) a WHERE 1 IN (SELECT 1,1);
  78. ERROR 21000: Operand should contain 1 column(s)
  79. SELECT 1 IN (SELECT 1);
  80. 1 IN (SELECT 1)
  81. 1
  82. SELECT 1 FROM (SELECT 1 as a) b WHERE 1 IN (SELECT (SELECT a));
  83. 1
  84. 1
  85. select (SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE(1));
  86. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'PROCEDURE ANALYSE(1))' at line 1
  87. SELECT 1 FROM (SELECT 1) a PROCEDURE ANALYSE((SELECT 1));
  88. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT 1))' at line 1
  89. SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NULL;
  90. ERROR 42S22: Unknown column 'a' in 'field list'
  91. SELECT (SELECT 1) as a FROM (SELECT 1) b WHERE (SELECT a) IS NOT NULL;
  92. ERROR 42S22: Unknown column 'a' in 'field list'
  93. SELECT (SELECT 1,2,3) = ROW(1,2,3);
  94. (SELECT 1,2,3) = ROW(1,2,3)
  95. 1
  96. SELECT (SELECT 1,2,3) = ROW(1,2,1);
  97. (SELECT 1,2,3) = ROW(1,2,1)
  98. 0
  99. SELECT (SELECT 1,2,3) < ROW(1,2,1);
  100. (SELECT 1,2,3) < ROW(1,2,1)
  101. 0
  102. SELECT (SELECT 1,2,3) > ROW(1,2,1);
  103. (SELECT 1,2,3) > ROW(1,2,1)
  104. 1
  105. SELECT (SELECT 1,2,3) = ROW(1,2,NULL);
  106. (SELECT 1,2,3) = ROW(1,2,NULL)
  107. NULL
  108. SELECT ROW(1,2,3) = (SELECT 1,2,3);
  109. ROW(1,2,3) = (SELECT 1,2,3)
  110. 1
  111. SELECT ROW(1,2,3) = (SELECT 1,2,1);
  112. ROW(1,2,3) = (SELECT 1,2,1)
  113. 0
  114. SELECT ROW(1,2,3) < (SELECT 1,2,1);
  115. ROW(1,2,3) < (SELECT 1,2,1)
  116. 0
  117. SELECT ROW(1,2,3) > (SELECT 1,2,1);
  118. ROW(1,2,3) > (SELECT 1,2,1)
  119. 1
  120. SELECT ROW(1,2,3) = (SELECT 1,2,NULL);
  121. ROW(1,2,3) = (SELECT 1,2,NULL)
  122. NULL
  123. SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'a');
  124. (SELECT 1.5,2,'a') = ROW(1.5,2,'a')
  125. 1
  126. SELECT (SELECT 1.5,2,'a') = ROW(1.5,2,'b');
  127. (SELECT 1.5,2,'a') = ROW(1.5,2,'b')
  128. 0
  129. SELECT (SELECT 1.5,2,'a') = ROW('1.5b',2,'b');
  130. (SELECT 1.5,2,'a') = ROW('1.5b',2,'b')
  131. 0
  132. Warnings:
  133. Warning 1292 Truncated incorrect DOUBLE value: '1.5b'
  134. SELECT (SELECT 'b',2,'a') = ROW(1.5,2,'a');
  135. (SELECT 'b',2,'a') = ROW(1.5,2,'a')
  136. 0
  137. Warnings:
  138. Warning 1292 Truncated incorrect DOUBLE value: 'b'
  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. Warnings:
  146. Warning 1292 Truncated incorrect DOUBLE value: 'c'
  147. SELECT (SELECT * FROM (SELECT 'test' a,'test' b) a);
  148. ERROR 21000: Operand should contain 1 column(s)
  149. SELECT 1 as a,(SELECT a+a) b,(SELECT b);
  150. a b (SELECT b)
  151. 1 2 2
  152. create table t1 (a int);
  153. create table t2 (a int, b int);
  154. create table t3 (a int);
  155. create table t4 (a int not null, b int not null);
  156. insert into t1 values (2);
  157. insert into t2 values (1,7),(2,7);
  158. insert into t4 values (4,8),(3,8),(5,9);
  159. select (select a from t1 where t1.a = a1) as a2, (select b from t2 where t2.b=a2) as a1;
  160. ERROR 42S22: Reference 'a1' not supported (forward reference in item list)
  161. select (select a from t1 where t1.a=t2.a), a from t2;
  162. (select a from t1 where t1.a=t2.a) a
  163. NULL 1
  164. 2 2
  165. select (select a from t1 where t1.a=t2.b), a from t2;
  166. (select a from t1 where t1.a=t2.b) a
  167. NULL 1
  168. NULL 2
  169. select (select a from t1), a, (select 1 union select 2 limit 1) from t2;
  170. (select a from t1) a (select 1 union select 2 limit 1)
  171. 2 1 1
  172. 2 2 1
  173. select (select a from t3), a from t2;
  174. (select a from t3) a
  175. NULL 1
  176. NULL 2
  177. select * from t2 where t2.a=(select a from t1);
  178. a b
  179. 2 7
  180. insert into t3 values (6),(7),(3);
  181. select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1);
  182. a b
  183. 1 7
  184. 2 7
  185. (select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1))
  186. union (select * from t4 order by a limit 2) order by a limit 3;
  187. a b
  188. 1 7
  189. 2 7
  190. 3 8
  191. (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);
  192. a b
  193. 1 7
  194. 2 7
  195. 4 8
  196. 3 8
  197. 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);
  198. id select_type table type possible_keys key key_len ref rows filtered Extra
  199. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
  200. 2 SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using filesort
  201. 3 UNION t4 ALL NULL NULL NULL NULL 3 100.00 Using where
  202. 4 SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00
  203. NULL UNION RESULT <union1,3> ALL NULL NULL NULL NULL NULL NULL
  204. Warnings:
  205. Note 1003 (/* select#1 */ select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t2` where `test`.`t2`.`b` = (/* select#2 */ select `test`.`t3`.`a` from `test`.`t3` order by 1 desc limit 1)) union (/* select#3 */ select `test`.`t4`.`a` AS `a`,`test`.`t4`.`b` AS `b` from `test`.`t4` where `test`.`t4`.`b` = (/* select#4 */ select max(`test`.`t2`.`a`) * 4 from `test`.`t2`))
  206. select (select a from t3 where a<t2.a*4 order by 1 desc limit 1), a from t2;
  207. (select a from t3 where a<t2.a*4 order by 1 desc limit 1) a
  208. 3 1
  209. 7 2
  210. select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from
  211. (select * from t2 where a>1) as tt;
  212. (select t3.a from t3 where a<8 order by 1 desc limit 1) a
  213. 7 2
  214. set @tmp_optimizer_switch=@@optimizer_switch;
  215. set optimizer_switch='derived_merge=off,derived_with_keys=off';
  216. explain extended select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from
  217. (select * from t2 where a>1) as tt;
  218. id select_type table type possible_keys key key_len ref rows filtered Extra
  219. 1 PRIMARY <derived3> ALL NULL NULL NULL NULL 2 100.00
  220. 3 DERIVED t2 ALL NULL NULL NULL NULL 2 100.00 Using where
  221. 2 SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using filesort
  222. Warnings:
  223. Note 1003 /* select#1 */ select (/* select#2 */ 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#3 */ select `test`.`t2`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t2` where `test`.`t2`.`a` > 1) `tt`
  224. set optimizer_switch=@tmp_optimizer_switch;
  225. 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);
  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. 2
  231. 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);
  232. a
  233. select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
  234. b (select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2)
  235. 8 7.5000
  236. 8 4.5000
  237. 9 7.5000
  238. explain extended select b,(select avg(t2.a+(select min(t3.a) from t3 where t3.a >= t4.a)) from t2) from t4;
  239. id select_type table type possible_keys key key_len ref rows filtered Extra
  240. 1 PRIMARY t4 ALL NULL NULL NULL NULL 3 100.00
  241. 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00
  242. 3 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
  243. Warnings:
  244. Note 1276 Field or reference 'test.t4.a' of SELECT #3 was resolved in SELECT #1
  245. Note 1003 /* select#1 */ select `test`.`t4`.`b` AS `b`,<expr_cache><`test`.`t4`.`a`>((/* select#2 */ select avg(`test`.`t2`.`a` + (/* select#3 */ 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`
  246. select * from t3 where exists (select * from t2 where t2.b=t3.a);
  247. a
  248. 7
  249. select * from t3 where not exists (select * from t2 where t2.b=t3.a);
  250. a
  251. 6
  252. 3
  253. select * from t3 where a in (select b from t2);
  254. a
  255. 7
  256. select * from t3 where a not in (select b from t2);
  257. a
  258. 6
  259. 3
  260. select * from t3 where a = some (select b from t2);
  261. a
  262. 7
  263. select * from t3 where a <> any (select b from t2);
  264. a
  265. 6
  266. 3
  267. select * from t3 where a = all (select b from t2);
  268. a
  269. 7
  270. select * from t3 where a <> all (select b from t2);
  271. a
  272. 6
  273. 3
  274. insert into t2 values (100, 5);
  275. select * from t3 where a < any (select b from t2);
  276. a
  277. 6
  278. 3
  279. select * from t3 where a < all (select b from t2);
  280. a
  281. 3
  282. select * from t3 where a >= any (select b from t2);
  283. a
  284. 6
  285. 7
  286. explain extended select * from t3 where a >= any (select b from t2);
  287. id select_type table type possible_keys key key_len ref rows filtered Extra
  288. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
  289. 2 SUBQUERY t2 ALL NULL NULL NULL NULL 3 100.00
  290. Warnings:
  291. Note 1003 /* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(`test`.`t3`.`a`,(/* select#2 */ select min(`test`.`t2`.`b`) from `test`.`t2`) <= <cache>(`test`.`t3`.`a`)))
  292. select * from t3 where a >= all (select b from t2);
  293. a
  294. 7
  295. delete from t2 where a=100;
  296. select * from t3 where a in (select a,b from t2);
  297. ERROR 21000: Operand should contain 1 column(s)
  298. select * from t3 where a in (select * from t2);
  299. ERROR 21000: Operand should contain 1 column(s)
  300. insert into t4 values (12,7),(1,7),(10,9),(9,6),(7,6),(3,9),(1,10);
  301. select b,max(a) as ma from t4 group by b having b < (select max(t2.a) from t2 where t2.b=t4.b);
  302. b ma
  303. insert into t2 values (2,10);
  304. select b,max(a) as ma from t4 group by b having ma < (select max(t2.a) from t2 where t2.b=t4.b);
  305. b ma
  306. 10 1
  307. delete from t2 where a=2 and b=10;
  308. select b,max(a) as ma from t4 group by b having b >= (select max(t2.a) from t2 where t2.b=t4.b);
  309. b ma
  310. 7 12
  311. create table t5 (a int);
  312. select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
  313. (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
  314. NULL 1
  315. 2 2
  316. insert into t5 values (5);
  317. select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
  318. (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
  319. NULL 1
  320. 2 2
  321. insert into t5 values (2);
  322. select (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a), a from t2;
  323. (select a from t1 where t1.a=t2.a union select a from t5 where t5.a=t2.a) a
  324. NULL 1
  325. 2 2
  326. 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;
  327. id select_type table type possible_keys key key_len ref rows filtered Extra
  328. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00
  329. 2 DEPENDENT SUBQUERY t1 system NULL NULL NULL NULL 1 100.00
  330. 3 DEPENDENT UNION t5 ALL NULL NULL NULL NULL 2 100.00 Using where
  331. NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
  332. Warnings:
  333. Note 1276 Field or reference 'test.t2.a' of SELECT #2 was resolved in SELECT #1
  334. Note 1276 Field or reference 'test.t2.a' of SELECT #3 was resolved in SELECT #1
  335. Note 1003 /* select#1 */ select <expr_cache><`test`.`t2`.`a`>((/* select#2 */ select 2 from dual where 2 = `test`.`t2`.`a` union /* select#3 */ 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`
  336. select (select a from t1 where t1.a=t2.a union all select a from t5 where t5.a=t2.a), a from t2;
  337. ERROR 21000: Subquery returns more than 1 row
  338. create table t6 (patient_uq int, clinic_uq int, index i1 (clinic_uq));
  339. create table t7( uq int primary key, name char(25));
  340. insert into t7 values(1,"Oblastnaia bolnitsa"),(2,"Bolnitsa Krasnogo Kresta");
  341. insert into t6 values (1,1),(1,2),(2,2),(1,3);
  342. select * from t6 where exists (select * from t7 where uq = clinic_uq);
  343. patient_uq clinic_uq
  344. 1 1
  345. 1 2
  346. 2 2
  347. explain extended select * from t6 where exists (select * from t7 where uq = clinic_uq);
  348. id select_type table type possible_keys key key_len ref rows filtered Extra
  349. 1 PRIMARY t6 ALL NULL NULL NULL NULL 4 100.00 Using where
  350. 2 MATERIALIZED t7 index PRIMARY PRIMARY 4 NULL 2 100.00 Using index
  351. Warnings:
  352. Note 1276 Field or reference 'test.t6.clinic_uq' of SELECT #2 was resolved in SELECT #1
  353. Note 1003 /* select#1 */ select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t6` where <expr_cache><`test`.`t6`.`clinic_uq`>(<in_optimizer>(`test`.`t6`.`clinic_uq`,`test`.`t6`.`clinic_uq` in ( <materialize> (/* select#2 */ select `test`.`t7`.`uq` from `test`.`t7` where 1 ), <primary_index_lookup>(`test`.`t6`.`clinic_uq` in <temporary table> on distinct_key where `test`.`t6`.`clinic_uq` = `<subquery2>`.`uq`))))
  354. select * from t1 where a= (select a from t2,t4 where t2.b=t4.b);
  355. ERROR 23000: Column 'a' in field list is ambiguous
  356. drop table t1,t2,t3;
  357. CREATE TABLE t3 (a varchar(20),b char(1) NOT NULL default '0');
  358. INSERT INTO t3 VALUES ('W','a'),('A','c'),('J','b');
  359. CREATE TABLE t2 (a varchar(20),b int NOT NULL default '0');
  360. INSERT INTO t2 VALUES ('W','1'),('A','3'),('J','2');
  361. CREATE TABLE t1 (a varchar(20),b date NOT NULL default '0000-00-00');
  362. INSERT INTO t1 VALUES ('W','1732-02-22'),('A','1735-10-30'),('J','1743-04-13');
  363. SELECT * FROM t1 WHERE b = (SELECT MIN(b) FROM t1);
  364. a b
  365. W 1732-02-22
  366. SELECT * FROM t2 WHERE b = (SELECT MIN(b) FROM t2);
  367. a b
  368. W 1
  369. SELECT * FROM t3 WHERE b = (SELECT MIN(b) FROM t3);
  370. a b
  371. W a
  372. CREATE TABLE `t8` (
  373. `pseudo` varchar(35) character set latin1 NOT NULL default '',
  374. `email` varchar(60) character set latin1 NOT NULL default '',
  375. PRIMARY KEY (`pseudo`),
  376. UNIQUE KEY `email` (`email`)
  377. ) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
  378. INSERT INTO t8 (pseudo,email) VALUES ('joce','test');
  379. INSERT INTO t8 (pseudo,email) VALUES ('joce1','test1');
  380. INSERT INTO t8 (pseudo,email) VALUES ('2joce1','2test1');
  381. 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');
  382. id select_type table type possible_keys key key_len ref rows filtered Extra
  383. 1 PRIMARY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using index
  384. 4 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using index
  385. 2 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00
  386. 3 SUBQUERY t8 const PRIMARY PRIMARY 37 const 1 100.00 Using index
  387. Warnings:
  388. Note 1003 /* select#1 */ select 'joce' AS `pseudo`,(/* select#2 */ select 'test' from `test`.`t8` where 1) AS `(SELECT email FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce'))` from `test`.`t8` where 1
  389. SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo,email FROM
  390. t8 WHERE pseudo='joce');
  391. ERROR HY000: Illegal parameter data types varchar and row for operation '='
  392. SELECT pseudo FROM t8 WHERE pseudo=(SELECT * FROM t8 WHERE
  393. pseudo='joce');
  394. ERROR HY000: Illegal parameter data types varchar and row for operation '='
  395. SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo='joce');
  396. pseudo
  397. joce
  398. SELECT pseudo FROM t8 WHERE pseudo=(SELECT pseudo FROM t8 WHERE pseudo LIKE '%joce%');
  399. ERROR 21000: Subquery returns more than 1 row
  400. drop table if exists t1,t2,t3,t4,t5,t6,t7,t8;
  401. CREATE TABLE `t1` (
  402. `topic` mediumint(8) unsigned NOT NULL default '0',
  403. `date` date NOT NULL default '0000-00-00',
  404. `pseudo` varchar(35) character set latin1 NOT NULL default '',
  405. PRIMARY KEY (`pseudo`,`date`,`topic`),
  406. KEY `topic` (`topic`)
  407. ) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
  408. INSERT INTO t1 (topic,date,pseudo) VALUES
  409. ('43506','2002-10-02','joce'),('40143','2002-08-03','joce');
  410. EXPLAIN EXTENDED SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
  411. id select_type table type possible_keys key key_len ref rows filtered Extra
  412. 1 SIMPLE t1 index NULL PRIMARY 43 NULL 2 100.00 Using where; Using index
  413. Warnings:
  414. Note 1003 select distinct `test`.`t1`.`date` AS `date` from `test`.`t1` where `test`.`t1`.`date` = DATE'2002-08-03'
  415. EXPLAIN EXTENDED SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
  416. id select_type table type possible_keys key key_len ref rows filtered Extra
  417. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  418. 2 SUBQUERY t1 index NULL PRIMARY 43 NULL 2 100.00 Using where; Using index
  419. Warnings:
  420. Note 1003 /* select#1 */ select (/* select#2 */ select distinct `test`.`t1`.`date` from `test`.`t1` where `test`.`t1`.`date` = DATE'2002-08-03') AS `(SELECT DISTINCT date FROM t1 WHERE date='2002-08-03')`
  421. SELECT DISTINCT date FROM t1 WHERE date='2002-08-03';
  422. date
  423. 2002-08-03
  424. SELECT (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03');
  425. (SELECT DISTINCT date FROM t1 WHERE date='2002-08-03')
  426. 2002-08-03
  427. SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1) UNION ALL SELECT 1;
  428. 1
  429. 1
  430. 1
  431. 1
  432. SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1) UNION SELECT 1;
  433. ERROR 21000: Subquery returns more than 1 row
  434. EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1=(SELECT 1 UNION SELECT 1);
  435. id select_type table type possible_keys key key_len ref rows filtered Extra
  436. 1 PRIMARY t1 index NULL topic 3 NULL 2 100.00 Using index
  437. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  438. 3 UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  439. NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
  440. Warnings:
  441. Note 1003 /* select#1 */ select 1 AS `1` from `test`.`t1` where 1
  442. drop table t1;
  443. CREATE TABLE `t1` (
  444. `numeropost` mediumint(8) unsigned NOT NULL auto_increment,
  445. `maxnumrep` int(10) unsigned NOT NULL default '0',
  446. PRIMARY KEY (`numeropost`),
  447. UNIQUE KEY `maxnumrep` (`maxnumrep`)
  448. ) ENGINE=MyISAM ROW_FORMAT=FIXED;
  449. INSERT INTO t1 (numeropost,maxnumrep) VALUES (40143,1),(43506,2);
  450. CREATE TABLE `t2` (
  451. `mot` varchar(30) NOT NULL default '',
  452. `topic` mediumint(8) unsigned NOT NULL default '0',
  453. `date` date NOT NULL default '0000-00-00',
  454. `pseudo` varchar(35) NOT NULL default '',
  455. PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`)
  456. ) ENGINE=MyISAM ROW_FORMAT=DYNAMIC;
  457. INSERT INTO t2 (mot,topic,date,pseudo) VALUES ('joce','40143','2002-10-22','joce'), ('joce','43506','2002-10-22','joce');
  458. select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
  459. a
  460. 40143
  461. 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;
  462. numeropost maxnumrep
  463. 43506 2
  464. 40143 1
  465. SELECT (SELECT 1) as a FROM (SELECT 1 FROM t1 HAVING a=1) b;
  466. ERROR 42S22: Unknown column 'a' in 'having clause'
  467. SELECT 1 IN (SELECT 1 FROM t2 HAVING a);
  468. ERROR 42S22: Unknown column 'a' in 'having clause'
  469. SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic);
  470. mot topic date pseudo
  471. joce 40143 2002-10-22 joce
  472. joce 43506 2002-10-22 joce
  473. SELECT * from t2 where topic IN (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
  474. mot topic date pseudo
  475. SELECT * from t2 where topic IN (SELECT SUM(topic) FROM t1);
  476. mot topic date pseudo
  477. SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic);
  478. mot topic date pseudo
  479. joce 40143 2002-10-22 joce
  480. joce 43506 2002-10-22 joce
  481. SELECT * from t2 where topic = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
  482. mot topic date pseudo
  483. SELECT * from t2 where topic = any (SELECT SUM(topic) FROM t1);
  484. mot topic date pseudo
  485. SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic);
  486. mot topic date pseudo
  487. SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100);
  488. mot topic date pseudo
  489. joce 40143 2002-10-22 joce
  490. joce 43506 2002-10-22 joce
  491. SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100) from t2;
  492. mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 4100)
  493. joce 40143 2002-10-22 joce 1
  494. joce 43506 2002-10-22 joce 1
  495. SELECT * from t2 where topic = all (SELECT SUM(topic) FROM t2);
  496. mot topic date pseudo
  497. SELECT * from t2 where topic <> any (SELECT SUM(topic) FROM t2);
  498. mot topic date pseudo
  499. joce 40143 2002-10-22 joce
  500. joce 43506 2002-10-22 joce
  501. SELECT * from t2 where topic IN (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 = any (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
  505. mot topic date pseudo
  506. joce 40143 2002-10-22 joce
  507. SELECT * from t2 where topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000);
  508. mot topic date pseudo
  509. joce 40143 2002-10-22 joce
  510. SELECT *, topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000) from t2;
  511. mot topic date pseudo topic = all (SELECT topic FROM t2 GROUP BY topic HAVING topic < 41000)
  512. joce 40143 2002-10-22 joce 1
  513. joce 43506 2002-10-22 joce 0
  514. drop table t1,t2;
  515. CREATE TABLE `t1` (
  516. `numeropost` mediumint(8) unsigned NOT NULL auto_increment,
  517. `maxnumrep` int(10) unsigned NOT NULL default '0',
  518. PRIMARY KEY (`numeropost`),
  519. UNIQUE KEY `maxnumrep` (`maxnumrep`)
  520. ) ENGINE=MyISAM ROW_FORMAT=FIXED;
  521. INSERT INTO t1 (numeropost,maxnumrep) VALUES (1,0),(2,1);
  522. select numeropost as a FROM t1 GROUP BY (SELECT 1 FROM t1 HAVING a=1);
  523. ERROR 21000: Subquery returns more than 1 row
  524. select numeropost as a FROM t1 ORDER BY (SELECT 1 FROM t1 HAVING a=1);
  525. ERROR 21000: Subquery returns more than 1 row
  526. show warnings;
  527. Level Code Message
  528. Error 1242 Subquery returns more than 1 row
  529. Error 1028 Sort aborted: Subquery returns more than 1 row
  530. drop table t1;
  531. create table t1 (a int);
  532. insert into t1 values (1),(2),(3);
  533. (select * from t1) union (select * from t1) order by (select a from t1 limit 1);
  534. a
  535. 1
  536. 2
  537. 3
  538. drop table t1;
  539. CREATE TABLE t1 (field char(1) NOT NULL DEFAULT 'b');
  540. INSERT INTO t1 VALUES ();
  541. SELECT field FROM t1 WHERE 1=(SELECT 1 UNION ALL SELECT 1 FROM (SELECT 1) a HAVING field='b');
  542. ERROR 21000: Subquery returns more than 1 row
  543. drop table t1;
  544. CREATE TABLE `t1` (
  545. `numeropost` mediumint(8) unsigned NOT NULL default '0',
  546. `numreponse` int(10) unsigned NOT NULL auto_increment,
  547. `pseudo` varchar(35) NOT NULL default '',
  548. PRIMARY KEY (`numeropost`,`numreponse`),
  549. UNIQUE KEY `numreponse` (`numreponse`),
  550. KEY `pseudo` (`pseudo`,`numeropost`)
  551. ) ENGINE=MyISAM;
  552. SELECT (SELECT numeropost FROM t1 HAVING numreponse=a),numreponse FROM (SELECT * FROM t1) as a;
  553. ERROR 42S22: Reference 'numreponse' not supported (forward reference in item list)
  554. SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=a) FROM (SELECT * FROM t1) as a;
  555. ERROR 42S22: Unknown column 'a' in 'having clause'
  556. SELECT numreponse, (SELECT numeropost FROM t1 HAVING numreponse=1) FROM (SELECT * FROM t1) as a;
  557. numreponse (SELECT numeropost FROM t1 HAVING numreponse=1)
  558. INSERT INTO t1 (numeropost,numreponse,pseudo) VALUES (1,1,'joce'),(1,2,'joce'),(1,3,'test');
  559. EXPLAIN EXTENDED SELECT numreponse FROM t1 WHERE numeropost='1' AND numreponse=(SELECT 1 FROM t1 WHERE numeropost='1');
  560. ERROR 21000: Subquery returns more than 1 row
  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 index
  571. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
  572. Warnings:
  573. Note 1003 /* select#1 */ select 3 AS `numreponse` from `test`.`t1` where 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 t2);
  591. ERROR 21000: Subquery returns more than 1 row
  592. update t1 set b= (select b from t2 where t1.a = t2.a);
  593. select * from t1;
  594. a b
  595. 0 NULL
  596. 1 21
  597. 2 22
  598. drop table t1, t2;
  599. create table t1 (a int NOT NULL, b int, primary key (a));
  600. create table t2 (a int NOT NULL, b int, primary key (a));
  601. insert into t1 values (0, 10),(1, 11),(2, 12);
  602. insert into t2 values (1, 21),(2, 12),(3, 23);
  603. select * from t1;
  604. a b
  605. 0 10
  606. 1 11
  607. 2 12
  608. select * from t1 where b = (select b from t2 where t1.a = t2.a);
  609. a b
  610. 2 12
  611. delete from t1 where b in (select b from t1);
  612. affected rows: 3
  613. insert into t1 values (0, 10),(1, 11),(2, 12);
  614. delete from t1 where b = (select b from t2);
  615. ERROR 21000: Subquery returns more than 1 row
  616. delete from t1 where b = (select b from t2 where t1.a = t2.a);
  617. select * from t1 order by b;
  618. a b
  619. 0 10
  620. 1 11
  621. drop table t1, t2;
  622. create table t11 (a int NOT NULL, b int, primary key (a));
  623. create table t12 (a int NOT NULL, b int, primary key (a));
  624. create table t2 (a int NOT NULL, b int, primary key (a));
  625. insert into t11 values (0, 10),(1, 11),(2, 12);
  626. insert into t12 values (33, 10),(22, 11),(2, 12);
  627. insert into t2 values (1, 21),(2, 12),(3, 23);
  628. select * from t11;
  629. a b
  630. 0 10
  631. 1 11
  632. 2 12
  633. select * from t12;
  634. a b
  635. 33 10
  636. 22 11
  637. 2 12
  638. delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t12 where t11.a = t12.a);
  639. ERROR HY000: Table 't12' is specified twice, both as a target for 'DELETE' and as a separate source for data
  640. delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2);
  641. ERROR 21000: Subquery returns more than 1 row
  642. delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b = (select b from t2 where t11.a = t2.a);
  643. select * from t11;
  644. a b
  645. 0 10
  646. 1 11
  647. select * from t12;
  648. a b
  649. 33 10
  650. 22 11
  651. drop table t11, t12, t2;
  652. CREATE TABLE t1 (x int) ENGINE=MyISAM;
  653. create table t2 (a int) ENGINE=MyISAM;
  654. create table t3 (b int);
  655. insert into t2 values (1);
  656. insert into t3 values (1),(2);
  657. INSERT INTO t1 (x) VALUES ((SELECT x FROM t1));
  658. ERROR HY000: Table 't1' is specified twice, both as a target for 'INSERT' and as a separate source for data
  659. INSERT INTO t1 (x) VALUES ((SELECT b FROM t3));
  660. ERROR 21000: Subquery returns more than 1 row
  661. INSERT INTO t1 (x) VALUES ((SELECT a FROM t2));
  662. select * from t1;
  663. x
  664. 1
  665. insert into t2 values (1);
  666. INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
  667. select * from t1;
  668. x
  669. 1
  670. 2
  671. INSERT INTO t1 (x) select (SELECT SUM(a)+1 FROM t2) FROM t2;
  672. select * from t1;
  673. x
  674. 1
  675. 2
  676. 3
  677. 3
  678. INSERT INTO t1 (x) select (SELECT SUM(x)+2 FROM t1) FROM t2;
  679. select * from t1;
  680. x
  681. 1
  682. 2
  683. 3
  684. 3
  685. 11
  686. 11
  687. INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(x) FROM t2));
  688. ERROR 42S22: Unknown column 'x' in 'field list'
  689. INSERT DELAYED INTO t1 (x) VALUES ((SELECT SUM(a) FROM t2));
  690. select * from t1;
  691. x
  692. 1
  693. 2
  694. 3
  695. 3
  696. 11
  697. 11
  698. 2
  699. drop table t1, t2, t3;
  700. CREATE TABLE t1 (x int not null, y int, primary key (x)) ENGINE=MyISAM;
  701. create table t2 (a int);
  702. create table t3 (a int);
  703. insert into t2 values (1);
  704. insert into t3 values (1),(2);
  705. select * from t1;
  706. x y
  707. replace into t1 (x, y) VALUES ((SELECT x FROM t1), (SELECT a+1 FROM t2));
  708. ERROR HY000: Table 't1' is specified twice, both as a target for 'INSERT' and as a separate source for data
  709. replace into t1 (x, y) VALUES ((SELECT a FROM t3), (SELECT a+1 FROM t2));
  710. ERROR 21000: Subquery returns more than 1 row
  711. replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+1 FROM t2));
  712. select * from t1;
  713. x y
  714. 1 2
  715. replace into t1 (x, y) VALUES ((SELECT a FROM t2), (SELECT a+2 FROM t2));
  716. select * from t1;
  717. x y
  718. 1 3
  719. replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a FROM t2));
  720. select * from t1;
  721. x y
  722. 1 3
  723. 4 1
  724. replace DELAYED into t1 (x, y) VALUES ((SELECT a+3 FROM t2), (SELECT a+1 FROM t2));
  725. select * from t1;
  726. x y
  727. 1 3
  728. 4 2
  729. replace LOW_PRIORITY into t1 (x, y) VALUES ((SELECT a+1 FROM t2), (SELECT a FROM t2));
  730. select * from t1;
  731. x y
  732. 1 3
  733. 4 2
  734. 2 1
  735. drop table t1, t2, t3;
  736. SELECT * FROM (SELECT 1) b WHERE 1 IN (SELECT *);
  737. ERROR HY000: No tables used
  738. CREATE TABLE t2 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;
  739. INSERT INTO t2 VALUES (1),(2);
  740. SELECT * FROM t2 WHERE id IN (SELECT 1);
  741. id
  742. 1
  743. EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1);
  744. id select_type table type possible_keys key key_len ref rows filtered Extra
  745. 1 SIMPLE t2 ref id id 5 const 1 100.00 Using index
  746. Warnings:
  747. Note 1249 Select 2 was reduced during optimization
  748. Note 1003 select `test`.`t2`.`id` AS `id` from `test`.`t2` where `test`.`t2`.`id` = 1
  749. SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);
  750. id
  751. 1
  752. SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));
  753. id
  754. 2
  755. EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1+(select 1));
  756. id select_type table type possible_keys key key_len ref rows filtered Extra
  757. 1 SIMPLE t2 ref id id 5 const 1 100.00 Using where; Using index
  758. Warnings:
  759. Note 1249 Select 3 was reduced during optimization
  760. Note 1249 Select 2 was reduced during optimization
  761. Note 1003 select `test`.`t2`.`id` AS `id` from `test`.`t2` where `test`.`t2`.`id` = <cache>(1 + 1)
  762. EXPLAIN EXTENDED SELECT * FROM t2 WHERE id IN (SELECT 1 UNION SELECT 3);
  763. id select_type table type possible_keys key key_len ref rows filtered Extra
  764. 1 PRIMARY t2 index NULL id 5 NULL 2 100.00 Using where; Using index
  765. 2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  766. 3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  767. NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
  768. Warnings:
  769. Note 1003 /* select#1 */ select `test`.`t2`.`id` AS `id` from `test`.`t2` where <expr_cache><`test`.`t2`.`id`>(<in_optimizer>(`test`.`t2`.`id`,<exists>(/* select#2 */ select 1 having <cache>(`test`.`t2`.`id`) = <ref_null_helper>(1) union /* select#3 */ select 3 having <cache>(`test`.`t2`.`id`) = <ref_null_helper>(3))))
  770. SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 3);
  771. id
  772. SELECT * FROM t2 WHERE id IN (SELECT 5 UNION SELECT 2);
  773. id
  774. 2
  775. INSERT INTO t2 VALUES ((SELECT * FROM t2));
  776. ERROR HY000: Table 't2' is specified twice, both as a target for 'INSERT' and as a separate source for data
  777. INSERT INTO t2 VALUES ((SELECT id FROM t2));
  778. ERROR HY000: Table 't2' is specified twice, both as a target for 'INSERT' and as a separate source for data
  779. SELECT * FROM t2;
  780. id
  781. 1
  782. 2
  783. CREATE TABLE t1 (id int(11) default NULL, KEY id (id)) ENGINE=MyISAM CHARSET=latin1;
  784. INSERT INTO t1 values (1),(1);
  785. UPDATE t2 SET id=(SELECT * FROM t1);
  786. ERROR 21000: Subquery returns more than 1 row
  787. drop table t2, t1;
  788. create table t1 (a int);
  789. insert into t1 values (1),(2),(3);
  790. select 1 IN (SELECT * from t1);
  791. 1 IN (SELECT * from t1)
  792. 1
  793. select 10 IN (SELECT * from t1);
  794. 10 IN (SELECT * from t1)
  795. 0
  796. select NULL IN (SELECT * from t1);
  797. NULL IN (SELECT * from t1)
  798. NULL
  799. update t1 set a=NULL where a=2;
  800. select 1 IN (SELECT * from t1);
  801. 1 IN (SELECT * from t1)
  802. 1
  803. select 3 IN (SELECT * from t1);
  804. 3 IN (SELECT * from t1)
  805. 1
  806. select 10 IN (SELECT * from t1);
  807. 10 IN (SELECT * from t1)
  808. NULL
  809. select 1 > ALL (SELECT * from t1);
  810. 1 > ALL (SELECT * from t1)
  811. 0
  812. select 10 > ALL (SELECT * from t1);
  813. 10 > ALL (SELECT * from t1)
  814. NULL
  815. select 1 > ANY (SELECT * from t1);
  816. 1 > ANY (SELECT * from t1)
  817. NULL
  818. select 10 > ANY (SELECT * from t1);
  819. 10 > ANY (SELECT * from t1)
  820. 1
  821. drop table t1;
  822. create table t1 (a varchar(20));
  823. insert into t1 values ('A'),('BC'),('DEF');
  824. select 'A' IN (SELECT * from t1);
  825. 'A' IN (SELECT * from t1)
  826. 1
  827. select 'XYZS' IN (SELECT * from t1);
  828. 'XYZS' IN (SELECT * from t1)
  829. 0
  830. select NULL IN (SELECT * from t1);
  831. NULL IN (SELECT * from t1)
  832. NULL
  833. update t1 set a=NULL where a='BC';
  834. select 'A' IN (SELECT * from t1);
  835. 'A' IN (SELECT * from t1)
  836. 1
  837. select 'DEF' IN (SELECT * from t1);
  838. 'DEF' IN (SELECT * from t1)
  839. 1
  840. select 'XYZS' IN (SELECT * from t1);
  841. 'XYZS' IN (SELECT * from t1)
  842. NULL
  843. select 'A' > ALL (SELECT * from t1);
  844. 'A' > ALL (SELECT * from t1)
  845. 0
  846. select 'XYZS' > ALL (SELECT * from t1);
  847. 'XYZS' > ALL (SELECT * from t1)
  848. NULL
  849. select 'A' > ANY (SELECT * from t1);
  850. 'A' > ANY (SELECT * from t1)
  851. NULL
  852. select 'XYZS' > ANY (SELECT * from t1);
  853. 'XYZS' > ANY (SELECT * from t1)
  854. 1
  855. drop table t1;
  856. create table t1 (a float);
  857. insert into t1 values (1.5),(2.5),(3.5);
  858. select 1.5 IN (SELECT * from t1);
  859. 1.5 IN (SELECT * from t1)
  860. 1
  861. select 10.5 IN (SELECT * from t1);
  862. 10.5 IN (SELECT * from t1)
  863. 0
  864. select NULL IN (SELECT * from t1);
  865. NULL IN (SELECT * from t1)
  866. NULL
  867. update t1 set a=NULL where a=2.5;
  868. select 1.5 IN (SELECT * from t1);
  869. 1.5 IN (SELECT * from t1)
  870. 1
  871. select 3.5 IN (SELECT * from t1);
  872. 3.5 IN (SELECT * from t1)
  873. 1
  874. select 10.5 IN (SELECT * from t1);
  875. 10.5 IN (SELECT * from t1)
  876. NULL
  877. select 1.5 > ALL (SELECT * from t1);
  878. 1.5 > ALL (SELECT * from t1)
  879. 0
  880. select 10.5 > ALL (SELECT * from t1);
  881. 10.5 > ALL (SELECT * from t1)
  882. NULL
  883. select 1.5 > ANY (SELECT * from t1);
  884. 1.5 > ANY (SELECT * from t1)
  885. NULL
  886. select 10.5 > ANY (SELECT * from t1);
  887. 10.5 > ANY (SELECT * from t1)
  888. 1
  889. explain extended select (select a+1) from t1;
  890. id select_type table type possible_keys key key_len ref rows filtered Extra
  891. 1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00
  892. Warnings:
  893. Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
  894. Note 1249 Select 2 was reduced during optimization
  895. Note 1003 select `test`.`t1`.`a` + 1 AS `(select a+1)` from `test`.`t1`
  896. select (select a+1) from t1;
  897. (select a+1)
  898. 2.5
  899. NULL
  900. 4.5
  901. drop table t1;
  902. CREATE TABLE t1 (a int(11) NOT NULL default '0', PRIMARY KEY (a));
  903. CREATE TABLE t2 (a int(11) default '0', INDEX (a));
  904. INSERT INTO t1 VALUES (1),(2),(3),(4);
  905. INSERT INTO t2 VALUES (1),(2),(3);
  906. SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
  907. a t1.a in (select t2.a from t2)
  908. 1 1
  909. 2 1
  910. 3 1
  911. 4 0
  912. explain extended SELECT t1.a, t1.a in (select t2.a from t2) FROM t1;
  913. id select_type table type possible_keys key key_len ref rows filtered Extra
  914. 1 PRIMARY t1 index NULL PRIMARY 4 NULL 4 100.00 Using index
  915. 2 MATERIALIZED t2 index a a 5 NULL 3 100.00 Using index
  916. Warnings:
  917. Note 1003 /* select#1 */ select `test`.`t1`.`a` AS `a`,<expr_cache><`test`.`t1`.`a`>(<in_optimizer>(`test`.`t1`.`a`,`test`.`t1`.`a` in ( <materialize> (/* select#2 */ select `test`.`t2`.`a` from `test`.`t2` ), <primary_index_lookup>(`test`.`t1`.`a` in <temporary table> on distinct_key where `test`.`t1`.`a` = `<subquery2>`.`a`)))) AS `t1.a in (select t2.a from t2)` from `test`.`t1`
  918. CREATE TABLE t3 (a int(11) default '0');
  919. INSERT INTO t3 VALUES (1),(2),(3);
  920. SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
  921. a t1.a in (select t2.a from t2,t3 where t3.a=t2.a)
  922. 1 1
  923. 2 1
  924. 3 1
  925. 4 0
  926. explain extended SELECT t1.a, t1.a in (select t2.a from t2,t3 where t3.a=t2.a) FROM t1;
  927. id select_type table type possible_keys key key_len ref rows filtered Extra
  928. 1 PRIMARY t1 index NULL PRIMARY 4 NULL 4 100.00 Using index
  929. 2 MATERIALIZED t2 index a a 5 NULL 3 100.00 Using index
  930. 2 MATERIALIZED t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join)
  931. Warnings:
  932. Note 1003 /* select#1 */ select `test`.`t1`.`a` AS `a`,<expr_cache><`test`.`t1`.`a`>(<in_optimizer>(`test`.`t1`.`a`,`test`.`t1`.`a` in ( <materialize> (/* select#2 */ select `test`.`t2`.`a` from `test`.`t2` join `test`.`t3` where `test`.`t3`.`a` = `test`.`t2`.`a` ), <primary_index_lookup>(`test`.`t1`.`a` in <temporary table> on distinct_key where `test`.`t1`.`a` = `<subquery2>`.`a`)))) AS `t1.a in (select t2.a from t2,t3 where t3.a=t2.a)` from `test`.`t1`
  933. drop table t1,t2,t3;
  934. # check correct NULL Processing for normal IN/ALL/ANY
  935. # and 2 ways of max/min optimization
  936. create table t1 (a int);
  937. insert into t1 values (1), (100), (NULL), (1000);
  938. create table t2 (a int not null);
  939. # subselect returns empty set (for NULL and non-NULL left part)
  940. select a, a in (select * from t2) from t1;
  941. a a in (select * from t2)
  942. 1 0
  943. 100 0
  944. NULL 0
  945. 1000 0
  946. select a, a > any (select * from t2) from t1;
  947. a a > any (select * from t2)
  948. 1 0
  949. 100 0
  950. NULL 0
  951. 1000 0
  952. select a, a > all (select * from t2) from t1;
  953. a a > all (select * from t2)
  954. 1 1
  955. 100 1
  956. NULL 1
  957. 1000 1
  958. select a from t1 where a in (select * from t2);
  959. a
  960. select a from t1 where a > any (select * from t2);
  961. a
  962. select a from t1 where a > all (select * from t2);
  963. a
  964. 1
  965. 100
  966. NULL
  967. 1000
  968. select a from t1 where a in (select * from t2 group by a);
  969. a
  970. select a from t1 where a > any (select * from t2 group by a);
  971. a
  972. select a from t1 where a > all (select * from t2 group by a);
  973. a
  974. 1
  975. 100
  976. NULL
  977. 1000
  978. insert into t2 values (1),(200);
  979. # sebselect returns non-empty set without NULLs
  980. select a, a in (select * from t2) from t1;
  981. a a in (select * from t2)
  982. 1 1
  983. 100 0
  984. NULL NULL
  985. 1000 0
  986. select a, a > any (select * from t2) from t1;
  987. a a > any (select * from t2)
  988. 1 0
  989. 100 1
  990. NULL NULL
  991. 1000 1
  992. select a, a > all (select * from t2) from t1;
  993. a a > all (select * from t2)
  994. 1 0
  995. 100 0
  996. NULL NULL
  997. 1000 1
  998. select a from t1 where a in (select * from t2);
  999. a
  1000. 1
  1001. select a from t1 where a > any (select * from t2);
  1002. a
  1003. 100
  1004. 1000
  1005. select a from t1 where a > all (select * from t2);
  1006. a
  1007. 1000
  1008. select a from t1 where a in (select * from t2 group by a);
  1009. a
  1010. 1
  1011. select a from t1 where a > any (select * from t2 group by a);
  1012. a
  1013. 100
  1014. 1000
  1015. select a from t1 where a > all (select * from t2 group by a);
  1016. a
  1017. 1000
  1018. drop table t2;
  1019. create table t2 (a int);
  1020. insert into t2 values (1),(NULL),(200);
  1021. # sebselect returns non-empty set with NULLs
  1022. select a, a in (select * from t2) from t1;
  1023. a a in (select * from t2)
  1024. 1 1
  1025. 100 NULL
  1026. NULL NULL
  1027. 1000 NULL
  1028. select a, a > any (select * from t2) from t1;
  1029. a a > any (select * from t2)
  1030. 1 NULL
  1031. 100 1
  1032. NULL NULL
  1033. 1000 1
  1034. select a, a > all (select * from t2) from t1;
  1035. a a > all (select * from t2)
  1036. 1 0
  1037. 100 0
  1038. NULL NULL
  1039. 1000 NULL
  1040. select a from t1 where a in (select * from t2);
  1041. a
  1042. 1
  1043. select a from t1 where a > any (select * from t2);
  1044. a
  1045. 100
  1046. 1000
  1047. select a from t1 where a > all (select * from t2);
  1048. a
  1049. select a from t1 where a in (select * from t2 group by a);
  1050. a
  1051. 1
  1052. select a from t1 where a > any (select * from t2 group by a);
  1053. a
  1054. 100
  1055. 1000
  1056. select a from t1 where a > all (select * from t2 group by a);
  1057. a
  1058. drop table t1, t2;
  1059. create table t1 (a float);
  1060. select 10.5 IN (SELECT * from t1 LIMIT 1);
  1061. ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
  1062. select 10.5 IN (SELECT * from t1 LIMIT 1 UNION SELECT 1.5);
  1063. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1.5)' at line 1
  1064. select 10.5 IN (SELECT * from t1 UNION SELECT 1.5 LIMIT 1);
  1065. ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
  1066. drop table t1;
  1067. create table t1 (a int, b int, c varchar(10));
  1068. create table t2 (a int);
  1069. insert into t1 values (1,2,'a'),(2,3,'b'),(3,4,'c');
  1070. insert into t2 values (1),(2),(NULL);
  1071. 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;
  1072. a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a') (select c from t1 where a=t2.a)
  1073. 1 1 a
  1074. 2 0 b
  1075. NULL NULL NULL
  1076. 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;
  1077. a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,3,'b') (select c from t1 where a=t2.a)
  1078. 1 0 a
  1079. 2 1 b
  1080. NULL NULL NULL
  1081. 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;
  1082. a (select a,b,c from t1 where t1.a=t2.a) = ROW(a,4,'c') (select c from t1 where a=t2.a)
  1083. 1 0 a
  1084. 2 0 b
  1085. NULL NULL NULL
  1086. drop table t1,t2;
  1087. create table t1 (a int, b real, c varchar(10));
  1088. insert into t1 values (1, 1, 'a'), (2,2,'b'), (NULL, 2, 'b');
  1089. select ROW(1, 1, 'a') IN (select a,b,c from t1);
  1090. ROW(1, 1, 'a') IN (select a,b,c from t1)
  1091. 1
  1092. select ROW(1, 2, 'a') IN (select a,b,c from t1);
  1093. ROW(1, 2, 'a') IN (select a,b,c from t1)
  1094. 0
  1095. select ROW(1, 1, 'a') IN (select b,a,c from t1);
  1096. ROW(1, 1, 'a') IN (select b,a,c from t1)
  1097. 1
  1098. select ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null);
  1099. ROW(1, 1, 'a') IN (select a,b,c from t1 where a is not null)
  1100. 1
  1101. select ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null);
  1102. ROW(1, 2, 'a') IN (select a,b,c from t1 where a is not null)
  1103. 0
  1104. select ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null);
  1105. ROW(1, 1, 'a') IN (select b,a,c from t1 where a is not null)
  1106. 1
  1107. select ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a');
  1108. ROW(1, 1, 'a') IN (select a,b,c from t1 where c='b' or c='a')
  1109. 1
  1110. select ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a');
  1111. ROW(1, 2, 'a') IN (select a,b,c from t1 where c='b' or c='a')
  1112. 0
  1113. select ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a');
  1114. ROW(1, 1, 'a') IN (select b,a,c from t1 where c='b' or c='a')
  1115. 1
  1116. select ROW(1, 1, 'a') IN (select b,a,c from t1 limit 2);
  1117. ERROR 42000: This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
  1118. drop table t1;
  1119. create table t1 (a int);
  1120. insert into t1 values (1);
  1121. do @a:=(SELECT a from t1);
  1122. select @a;
  1123. @a
  1124. 1
  1125. set @a:=2;
  1126. set @a:=(SELECT a from t1);
  1127. select @a;
  1128. @a
  1129. 1
  1130. drop table t1;
  1131. do (SELECT a from t1);
  1132. ERROR 42S02: Table 'test.t1' doesn't exist
  1133. set @a:=(SELECT a from t1);
  1134. ERROR 42S02: Table 'test.t1' doesn't exist
  1135. CREATE TABLE t1 (a int, KEY(a));
  1136. HANDLER t1 OPEN;
  1137. HANDLER t1 READ a=((SELECT 1));
  1138. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT 1))' at line 1
  1139. HANDLER t1 CLOSE;
  1140. drop table t1;
  1141. create table t1 (a int);
  1142. create table t2 (b int);
  1143. insert into t1 values (1),(2);
  1144. insert into t2 values (1);
  1145. select a from t1 where a in (select a from t1 where a in (select b from t2));
  1146. a
  1147. 1
  1148. drop table t1, t2;
  1149. create table t1 (a int, b int);
  1150. create table t2 like t1;
  1151. insert into t1 values (1,2),(1,3),(1,4),(1,5);
  1152. insert into t2 values (1,2),(1,3);
  1153. select * from t1 where row(a,b) in (select a,b from t2);
  1154. a b
  1155. 1 2
  1156. 1 3
  1157. drop table t1, t2;
  1158. CREATE TABLE `t1` (`i` int(11) NOT NULL default '0',PRIMARY KEY (`i`)) ENGINE=MyISAM CHARSET=latin1;
  1159. INSERT INTO t1 VALUES (1);
  1160. UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));
  1161. select * from t1;
  1162. i
  1163. 2
  1164. drop table t1;
  1165. CREATE TABLE t1 (a int(1));
  1166. EXPLAIN EXTENDED SELECT (SELECT RAND() FROM t1) FROM t1;
  1167. id select_type table type possible_keys key key_len ref rows filtered Extra
  1168. 1 PRIMARY t1 system NULL NULL NULL NULL 0 0.00 Const row not found
  1169. 2 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 0 0.00 Const row not found
  1170. Warnings:
  1171. Note 1003 /* select#1 */ select (/* select#2 */ select rand() from `test`.`t1`) AS `(SELECT RAND() FROM t1)` from `test`.`t1`
  1172. EXPLAIN EXTENDED SELECT (SELECT ENCRYPT('test') FROM t1) FROM t1;
  1173. id select_type table type possible_keys key key_len ref rows filtered Extra
  1174. 1 PRIMARY t1 system NULL NULL NULL NULL 0 0.00 Const row not found
  1175. 2 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 0 0.00 Const row not found
  1176. Warnings:
  1177. Note 1003 /* select#1 */ select (/* select#2 */ select encrypt('test') from `test`.`t1`) AS `(SELECT ENCRYPT('test') FROM t1)` from `test`.`t1`
  1178. EXPLAIN EXTENDED SELECT (SELECT BENCHMARK(1,1) FROM t1) FROM t1;
  1179. id select_type table type possible_keys key key_len ref rows filtered Extra
  1180. 1 PRIMARY t1 system NULL NULL NULL NULL 0 0.00 Const row not found
  1181. 2 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 0 0.00 Const row not found
  1182. Warnings:
  1183. Note 1003 /* select#1 */ select (/* select#2 */ select benchmark(1,1) from `test`.`t1`) AS `(SELECT BENCHMARK(1,1) FROM t1)` from `test`.`t1`
  1184. drop table t1;
  1185. CREATE TABLE `t1` (
  1186. `mot` varchar(30) character set latin1 NOT NULL default '',
  1187. `topic` mediumint(8) unsigned NOT NULL default '0',
  1188. `date` date NOT NULL default '0000-00-00',
  1189. `pseudo` varchar(35) character set latin1 NOT NULL default '',
  1190. PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`),
  1191. KEY `pseudo` (`pseudo`,`date`,`topic`),
  1192. KEY `topic` (`topic`)
  1193. ) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
  1194. CREATE TABLE `t2` (
  1195. `mot` varchar(30) character set latin1 NOT NULL default '',
  1196. `topic` mediumint(8) unsigned NOT NULL default '0',
  1197. `date` date NOT NULL default '0000-00-00',
  1198. `pseudo` varchar(35) character set latin1 NOT NULL default '',
  1199. PRIMARY KEY (`mot`,`pseudo`,`date`,`topic`),
  1200. KEY `pseudo` (`pseudo`,`date`,`topic`),
  1201. KEY `topic` (`topic`)
  1202. ) ENGINE=MyISAM CHARSET=latin1 ROW_FORMAT=DYNAMIC;
  1203. CREATE TABLE `t3` (
  1204. `numeropost` mediumint(8) unsigned NOT NULL auto_increment,
  1205. `maxnumrep` int(10) unsigned NOT NULL default '0',
  1206. PRIMARY KEY (`numeropost`),
  1207. UNIQUE KEY `maxnumrep` (`maxnumrep`)
  1208. ) ENGINE=MyISAM CHARSET=latin1;
  1209. INSERT IGNORE INTO t1 VALUES ('joce','1','','joce'),('test','2','','test');
  1210. Warnings:
  1211. Warning 1265 Data truncated for column 'date' at row 1
  1212. Warning 1265 Data truncated for column 'date' at row 2
  1213. INSERT IGNORE INTO t2 VALUES ('joce','1','','joce'),('test','2','','test');
  1214. Warnings:
  1215. Warning 1265 Data truncated for column 'date' at row 1
  1216. Warning 1265 Data truncated for column 'date' at row 2
  1217. INSERT INTO t3 VALUES (1,1);
  1218. SELECT DISTINCT topic FROM t2 WHERE NOT EXISTS(SELECT * FROM t3 WHERE
  1219. numeropost=topic);
  1220. topic
  1221. 2
  1222. select * from t1;
  1223. mot topic date pseudo
  1224. joce 1 0000-00-00 joce
  1225. test 2 0000-00-00 test
  1226. DELETE FROM t1 WHERE topic IN (SELECT DISTINCT topic FROM t2 WHERE NOT
  1227. EXISTS(SELECT * FROM t3 WHERE numeropost=topic));
  1228. select * from t1;
  1229. mot topic date pseudo
  1230. joce 1 0000-00-00 joce
  1231. drop table t1, t2, t3;
  1232. SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
  1233. a (SELECT a)
  1234. 1 1
  1235. CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT 1)) a;
  1236. SHOW CREATE TABLE t1;
  1237. Table Create Table
  1238. t1 CREATE TABLE `t1` (
  1239. `a` int(1) NOT NULL DEFAULT 0,
  1240. `(SELECT 1)` int(1) NOT NULL DEFAULT 0
  1241. ) ENGINE=MyISAM DEFAULT CHARSET=latin1
  1242. drop table t1;
  1243. CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a)) a;
  1244. SHOW CREATE TABLE t1;
  1245. Table Create Table
  1246. t1 CREATE TABLE `t1` (
  1247. `a` int(1) NOT NULL DEFAULT 0,
  1248. `(SELECT a)` int(1) NOT NULL DEFAULT 0
  1249. ) ENGINE=MyISAM DEFAULT CHARSET=latin1
  1250. drop table t1;
  1251. CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a;
  1252. SHOW CREATE TABLE t1;
  1253. Table Create Table
  1254. t1 CREATE TABLE `t1` (
  1255. `a` int(1) NOT NULL DEFAULT 0,
  1256. `(SELECT a+0)` int(3) NOT NULL DEFAULT 0
  1257. ) ENGINE=MyISAM DEFAULT CHARSET=latin1
  1258. drop table t1;
  1259. CREATE TABLE t1 SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a;
  1260. select * from t1;
  1261. a
  1262. 2
  1263. SHOW CREATE TABLE t1;
  1264. Table Create Table
  1265. t1 CREATE TABLE `t1` (
  1266. `a` int(3) NOT NULL
  1267. ) ENGINE=MyISAM DEFAULT CHARSET=latin1
  1268. drop table t1;
  1269. create table t1 (a int);
  1270. insert into t1 values (1), (2), (3);
  1271. explain extended select a,(select (select rand() from t1 limit 1) from t1 limit 1)
  1272. from t1;
  1273. id select_type table type possible_keys key key_len ref rows filtered Extra
  1274. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
  1275. 2 UNCACHEABLE SUBQUERY t1 ALL NULL NULL NULL NULL 3 100.00
  1276. 3 UNCACHEABLE SUBQUERY t1 ALL NULL NULL NULL NULL 3 100.00
  1277. Warnings:
  1278. Note 1003 /* select#1 */ select `test`.`t1`.`a` AS `a`,(/* select#2 */ select (/* select#3 */ 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`
  1279. drop table t1;
  1280. 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);
  1281. ERROR 42S02: Table 'test.t1' doesn't exist
  1282. CREATE TABLE t1 (
  1283. ID int(11) NOT NULL auto_increment,
  1284. name char(35) NOT NULL default '',
  1285. t2 char(3) NOT NULL default '',
  1286. District char(20) NOT NULL default '',
  1287. Population int(11) NOT NULL default '0',
  1288. PRIMARY KEY (ID)
  1289. ) ENGINE=MyISAM;
  1290. INSERT INTO t1 VALUES (130,'Sydney','AUS','New South Wales',3276207);
  1291. INSERT INTO t1 VALUES (131,'Melbourne','AUS','Victoria',2865329);
  1292. INSERT INTO t1 VALUES (132,'Brisbane','AUS','Queensland',1291117);
  1293. CREATE TABLE t2 (
  1294. Code char(3) NOT NULL default '',
  1295. Name char(52) NOT NULL default '',
  1296. Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
  1297. Region char(26) NOT NULL default '',
  1298. SurfaceArea float(10,2) NOT NULL default '0.00',
  1299. IndepYear smallint(6) default NULL,
  1300. Population int(11) NOT NULL default '0',
  1301. LifeExpectancy float(3,1) default NULL,
  1302. GNP float(10,2) default NULL,
  1303. GNPOld float(10,2) default NULL,
  1304. LocalName char(45) NOT NULL default '',
  1305. GovernmentForm char(45) NOT NULL default '',
  1306. HeadOfState char(60) default NULL,
  1307. Capital int(11) default NULL,
  1308. Code2 char(2) NOT NULL default '',
  1309. PRIMARY KEY (Code)
  1310. ) ENGINE=MyISAM;
  1311. 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');
  1312. 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');
  1313. 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);
  1314. Continent Name Population
  1315. Oceania Sydney 3276207
  1316. drop table t1, t2;
  1317. CREATE TABLE `t1` (
  1318. `id` mediumint(8) unsigned NOT NULL auto_increment,
  1319. `pseudo` varchar(35) character set latin1 NOT NULL default '',
  1320. PRIMARY KEY (`id`),
  1321. UNIQUE KEY `pseudo` (`pseudo`)
  1322. ) ENGINE=MyISAM PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
  1323. INSERT INTO t1 (pseudo) VALUES ('test');
  1324. SELECT 0 IN (SELECT 1 FROM t1 a);
  1325. 0 IN (SELECT 1 FROM t1 a)
  1326. 0
  1327. EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
  1328. id select_type table type possible_keys key key_len ref rows filtered Extra
  1329. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  1330. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  1331. Warnings:
  1332. Note 1003 /* select#1 */ select <in_optimizer>(0,<exists>(/* select#2 */ select 1 from dual where 0 = 1)) AS `0 IN (SELECT 1 FROM t1 a)`
  1333. INSERT INTO t1 (pseudo) VALUES ('test1');
  1334. SELECT 0 IN (SELECT 1 FROM t1 a);
  1335. 0 IN (SELECT 1 FROM t1 a)
  1336. 0
  1337. EXPLAIN EXTENDED SELECT 0 IN (SELECT 1 FROM t1 a);
  1338. id select_type table type possible_keys key key_len ref rows filtered Extra
  1339. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL No tables used
  1340. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  1341. Warnings:
  1342. Note 1003 /* select#1 */ select <in_optimizer>(0,<exists>(/* select#2 */ select 1 from `test`.`t1` `a` where 0 = 1)) AS `0 IN (SELECT 1 FROM t1 a)`
  1343. drop table t1;
  1344. CREATE TABLE `t1` (
  1345. `i` int(11) NOT NULL default '0',
  1346. PRIMARY KEY (`i`)
  1347. ) ENGINE=MyISAM CHARSET=latin1;
  1348. INSERT INTO t1 VALUES (1);
  1349. UPDATE t1 SET i=i+(SELECT MAX(i) FROM (SELECT 1) t) WHERE i=(SELECT MAX(i));
  1350. UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i));
  1351. UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t);
  1352. ERROR 42S22: Unknown column 't.i' in 'field list'
  1353. select * from t1;
  1354. i
  1355. 3
  1356. drop table t1;
  1357. CREATE TABLE t1 (
  1358. id int(11) default NULL
  1359. ) ENGINE=MyISAM CHARSET=latin1;
  1360. INSERT INTO t1 VALUES (1),(1),(2),(2),(1),(3);
  1361. CREATE TABLE t2 (
  1362. id int(11) default NULL,
  1363. name varchar(15) default NULL
  1364. ) ENGINE=MyISAM CHARSET=latin1;
  1365. INSERT INTO t2 VALUES (4,'vita'), (1,'vita'), (2,'vita'), (1,'vita');
  1366. update t1, t2 set t2.name='lenka' where t2.id in (select id from t1);
  1367. select * from t2;
  1368. id name
  1369. 4 vita
  1370. 1 lenka
  1371. 2 lenka
  1372. 1 lenka
  1373. drop table t1,t2;
  1374. create table t1 (a int, unique index indexa (a));
  1375. insert into t1 values (-1), (-4), (-2), (NULL);
  1376. select -10 IN (select a from t1 FORCE INDEX (indexa));
  1377. -10 IN (select a from t1 FORCE INDEX (indexa))
  1378. NULL
  1379. drop table t1;
  1380. create table t1 (id int not null auto_increment primary key, salary int, key(salary));
  1381. insert into t1 (salary) values (100),(1000),(10000),(10),(500),(5000),(50000);
  1382. explain extended SELECT id FROM t1 where salary = (SELECT MAX(salary) FROM t1);
  1383. id select_type table type possible_keys key key_len ref rows filtered Extra
  1384. 1 PRIMARY t1 ref salary salary 5 const 1 100.00 Using where
  1385. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
  1386. Warnings:
  1387. Note 1003 /* select#1 */ select `test`.`t1`.`id` AS `id` from `test`.`t1` where `test`.`t1`.`salary` = (/* select#2 */ select max(`test`.`t1`.`salary`) from `test`.`t1`)
  1388. drop table t1;
  1389. CREATE TABLE t1 (
  1390. ID int(10) unsigned NOT NULL auto_increment,
  1391. SUB_ID int(3) unsigned NOT NULL default '0',
  1392. REF_ID int(10) unsigned default NULL,
  1393. REF_SUB int(3) unsigned default '0',
  1394. PRIMARY KEY (ID,SUB_ID),
  1395. UNIQUE KEY t1_PK (ID,SUB_ID),
  1396. KEY t1_FK (REF_ID,REF_SUB),
  1397. KEY t1_REFID (REF_ID)
  1398. ) ENGINE=MyISAM CHARSET=cp1251;
  1399. INSERT INTO t1 VALUES (1,0,NULL,NULL),(2,0,NULL,NULL);
  1400. SELECT DISTINCT REF_ID FROM t1 WHERE ID= (SELECT DISTINCT REF_ID FROM t1 WHERE ID=2);
  1401. REF_ID
  1402. DROP TABLE t1;
  1403. create table t1 (a int, b int);
  1404. create table t2 (a int, b int);
  1405. insert into t1 values (1,0), (2,0), (3,0);
  1406. insert into t2 values (1,1), (2,1), (3,1), (2,2);
  1407. update ignore t1 set b=(select b from t2 where t1.a=t2.a);
  1408. Warnings:
  1409. Warning 1242 Subquery returns more than 1 row
  1410. select * from t1;
  1411. a b
  1412. 1 1
  1413. 2 NULL
  1414. 3 1
  1415. drop table t1, t2;
  1416. CREATE TABLE `t1` (
  1417. `id` mediumint(8) unsigned NOT NULL auto_increment,
  1418. `pseudo` varchar(35) NOT NULL default '',
  1419. `email` varchar(60) NOT NULL default '',
  1420. PRIMARY KEY (`id`),
  1421. UNIQUE KEY `email` (`email`),
  1422. UNIQUE KEY `pseudo` (`pseudo`)
  1423. ) ENGINE=MyISAM CHARSET=latin1 PACK_KEYS=1 ROW_FORMAT=DYNAMIC;
  1424. INSERT INTO t1 (id,pseudo,email) VALUES (1,'test','test'),(2,'test1','test1');
  1425. SELECT pseudo as a, pseudo as b FROM t1 GROUP BY (SELECT a) ORDER BY (SELECT id*1);
  1426. a b
  1427. test test
  1428. test1 test1
  1429. drop table if exists t1;
  1430. (SELECT 1 as a) UNION (SELECT 1) ORDER BY (SELECT a+0);
  1431. a
  1432. 1
  1433. create table t1 (a int not null, b int, primary key (a));
  1434. create table t2 (a int not null, primary key (a));
  1435. create table t3 (a int not null, b int, primary key (a));
  1436. insert into t1 values (1,10), (2,20), (3,30), (4,40);
  1437. insert into t2 values (2), (3), (4), (5);
  1438. insert into t3 values (10,3), (20,4), (30,5);
  1439. select * from t2 where t2.a in (select a from t1);
  1440. a
  1441. 2
  1442. 3
  1443. 4
  1444. explain extended select * from t2 where t2.a in (select a from t1);
  1445. id select_type table type possible_keys key key_len ref rows filtered Extra
  1446. 1 PRIMARY t2 index NULL PRIMARY 4 NULL 4 100.00 Using where; Using index
  1447. 2 MATERIALIZED t1 index PRIMARY PRIMARY 4 NULL 4 100.00 Using index
  1448. Warnings:
  1449. Note 1003 /* select#1 */ select `test`.`t2`.`a` AS `a` from `test`.`t2` where <expr_cache><`test`.`t2`.`a`>(<in_optimizer>(`test`.`t2`.`a`,`test`.`t2`.`a` in ( <materialize> (/* select#2 */ select `test`.`t1`.`a` from `test`.`t1` ), <primary_index_lookup>(`test`.`t2`.`a` in <temporary table> on distinct_key where `test`.`t2`.`a` = `<subquery2>`.`a`))))
  1450. select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
  1451. a
  1452. 2
  1453. 4
  1454. explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
  1455. id select_type table type possible_keys key key_len ref rows filtered Extra
  1456. 1 PRIMARY t2 index NULL PRIMARY 4 NULL 4 100.00 Using where; Using index
  1457. 2 MATERIALIZED t1 ALL PRIMARY NULL NULL NULL 4 100.00 Using where
  1458. Warnings:
  1459. Note 1003 /* select#1 */ select `test`.`t2`.`a` AS `a` from `test`.`t2` where <expr_cache><`test`.`t2`.`a`>(<in_optimizer>(`test`.`t2`.`a`,`test`.`t2`.`a` in ( <materialize> (/* select#2 */ select `test`.`t1`.`a` from `test`.`t1` where `test`.`t1`.`b` <> 30 ), <primary_index_lookup>(`test`.`t2`.`a` in <temporary table> on distinct_key where `test`.`t2`.`a` = `<subquery2>`.`a`))))
  1460. select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
  1461. a
  1462. 2
  1463. 3
  1464. explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
  1465. id select_type table type possible_keys key key_len ref rows filtered Extra
  1466. 1 PRIMARY t2 index NULL PRIMARY 4 NULL 4 100.00 Using where; Using index
  1467. 2 MATERIALIZED t3 index PRIMARY PRIMARY 4 NULL 3 100.00 Using index
  1468. 2 MATERIALIZED t1 ALL PRIMARY NULL NULL NULL 4 100.00 Using where; Using join buffer (flat, BNL join)
  1469. Warnings:
  1470. Note 1003 /* select#1 */ select `test`.`t2`.`a` AS `a` from `test`.`t2` where <expr_cache><`test`.`t2`.`a`>(<in_optimizer>(`test`.`t2`.`a`,`test`.`t2`.`a` in ( <materialize> (/* select#2 */ select `test`.`t1`.`a` from `test`.`t1` join `test`.`t3` where `test`.`t1`.`b` = `test`.`t3`.`a` ), <primary_index_lookup>(`test`.`t2`.`a` in <temporary table> on distinct_key where `test`.`t2`.`a` = `<subquery2>`.`a`))))
  1471. drop table t1, t2, t3;
  1472. create table t1 (a int, b int, index a (a,b));
  1473. create table t2 (a int, index a (a));
  1474. create table t3 (a int, b int, index a (a));
  1475. insert into t1 values (1,10), (2,20), (3,30), (4,40);
  1476. create table t0(a int);
  1477. insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
  1478. insert into t1
  1479. select rand()*100000+200,rand()*100000 from t0 A, t0 B, t0 C, t0 D;
  1480. insert into t2 values (2), (3), (4), (5);
  1481. insert into t3 values (10,3), (20,4), (30,5);
  1482. select * from t2 where t2.a in (select a from t1);
  1483. a
  1484. 2
  1485. 3
  1486. 4
  1487. explain extended select * from t2 where t2.a in (select a from t1);
  1488. id select_type table type possible_keys key key_len ref rows filtered Extra
  1489. 1 PRIMARY t2 index NULL a 5 NULL 4 100.00 Using where; Using index
  1490. 2 DEPENDENT SUBQUERY t1 index_subquery a a 5 func 1001 100.00 Using index
  1491. Warnings:
  1492. Note 1003 /* select#1 */ select `test`.`t2`.`a` AS `a` from `test`.`t2` where <expr_cache><`test`.`t2`.`a`>(<in_optimizer>(`test`.`t2`.`a`,<exists>(<index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on a))))
  1493. select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
  1494. a
  1495. 2
  1496. 4
  1497. explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
  1498. id select_type table type possible_keys key key_len ref rows filtered Extra
  1499. 1 PRIMARY t2 index NULL a 5 NULL 4 100.00 Using where; Using index
  1500. 2 DEPENDENT SUBQUERY t1 index_subquery a a 5 func 1001 100.00 Using index; Using where
  1501. Warnings:
  1502. Note 1003 /* select#1 */ select `test`.`t2`.`a` AS `a` from `test`.`t2` where <expr_cache><`test`.`t2`.`a`>(<in_optimizer>(`test`.`t2`.`a`,<exists>(<index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on a where `test`.`t1`.`b` <> 30 and <cache>(`test`.`t2`.`a`) = `test`.`t1`.`a`))))
  1503. select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
  1504. a
  1505. 2
  1506. 3
  1507. explain extended select * from t2 where t2.a in (select t1.a from t1,t3 where t1.b=t3.a);
  1508. id select_type table type possible_keys key key_len ref rows filtered Extra
  1509. 1 PRIMARY t2 index NULL a 5 NULL 4 100.00 Using where; Using index
  1510. 2 DEPENDENT SUBQUERY t3 index a a 5 NULL 3 100.00 Using where; Using index
  1511. 2 DEPENDENT SUBQUERY t1 ref a a 10 func,test.t3.a 1167 100.00 Using index
  1512. Warnings:
  1513. Note 1003 /* select#1 */ select `test`.`t2`.`a` AS `a` from `test`.`t2` where <expr_cache><`test`.`t2`.`a`>(<in_optimizer>(`test`.`t2`.`a`,<exists>(/* select#2 */ select `test`.`t1`.`a` from `test`.`t1` join `test`.`t3` where `test`.`t1`.`b` = `test`.`t3`.`a` and <cache>(`test`.`t2`.`a`) = `test`.`t1`.`a`)))
  1514. insert into t1 values (3,31);
  1515. select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
  1516. a
  1517. 2
  1518. 3
  1519. 4
  1520. select * from t2 where t2.a in (select a from t1 where t1.b <> 30 and t1.b <> 31);
  1521. a
  1522. 2
  1523. 4
  1524. explain extended select * from t2 where t2.a in (select a from t1 where t1.b <> 30);
  1525. id select_type table type possible_keys key key_len ref rows filtered Extra
  1526. 1 PRIMARY t2 index NULL a 5 NULL 4 100.00 Using where; Using index
  1527. 2 DEPENDENT SUBQUERY t1 index_subquery a a 5 func 1001 100.00 Using index; Using where
  1528. Warnings:
  1529. Note 1003 /* select#1 */ select `test`.`t2`.`a` AS `a` from `test`.`t2` where <expr_cache><`test`.`t2`.`a`>(<in_optimizer>(`test`.`t2`.`a`,<exists>(<index_lookup>(<cache>(`test`.`t2`.`a`) in t1 on a where `test`.`t1`.`b` <> 30 and <cache>(`test`.`t2`.`a`) = `test`.`t1`.`a`))))
  1530. drop table t0, t1, t2, t3;
  1531. create table t1 (a int, b int);
  1532. create table t2 (a int, b int);
  1533. create table t3 (a int, b int);
  1534. insert into t1 values (0,100),(1,2), (1,3), (2,2), (2,7), (2,-1), (3,10);
  1535. insert into t2 values (0,0), (1,1), (2,1), (3,1), (4,1);
  1536. insert into t3 values (3,3), (2,2), (1,1);
  1537. 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;
  1538. 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)
  1539. 3 1
  1540. 2 2
  1541. 1 2
  1542. drop table t1,t2,t3;
  1543. create table t1 (s1 int);
  1544. create table t2 (s1 int);
  1545. insert into t1 values (1);
  1546. insert into t2 values (1);
  1547. select * from t1 where exists (select s1 from t2 having max(t2.s1)=t1.s1);
  1548. s1
  1549. 1
  1550. drop table t1,t2;
  1551. create table t1 (s1 int);
  1552. create table t2 (s1 int);
  1553. insert into t1 values (1);
  1554. insert into t2 values (1);
  1555. update t1 set s1 = s1 + 1 where 1 = (select x.s1 as A from t2 WHERE t2.s1 > t1.s1 order by A);
  1556. ERROR 42S22: Unknown column 'x.s1' in 'field list'
  1557. DROP TABLE t1, t2;
  1558. CREATE TABLE t1 (s1 CHAR(5) COLLATE latin1_german1_ci,
  1559. s2 CHAR(5) COLLATE latin1_swedish_ci);
  1560. INSERT INTO t1 VALUES ('z','?');
  1561. select * from t1 where s1 > (select max(s2) from t1);
  1562. ERROR HY000: Illegal mix of collations (latin1_german1_ci,IMPLICIT) and (latin1_swedish_ci,IMPLICIT) for operation '>'
  1563. select * from t1 where s1 > any (select max(s2) from t1);
  1564. ERROR HY000: Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (latin1_german1_ci,IMPLICIT) for operation '<'
  1565. drop table t1;
  1566. create table t1(toid int,rd int);
  1567. create table t2(userid int,pmnew int,pmtotal int);
  1568. insert into t2 values(1,0,0),(2,0,0);
  1569. 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);
  1570. 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);
  1571. userid pmtotal pmnew calc_total calc_new
  1572. 1 0 0 9 3
  1573. 2 0 0 4 2
  1574. drop table t1, t2;
  1575. create table t1 (s1 char(5));
  1576. select (select 'a','b' from t1 union select 'a','b' from t1) from t1;
  1577. ERROR 21000: Operand should contain 1 column(s)
  1578. insert into t1 values ('tttt');
  1579. select * from t1 where ('a','b')=(select 'a','b' from t1 union select 'a','b' from t1);
  1580. s1
  1581. tttt
  1582. explain extended (select * from t1);
  1583. id select_type table type possible_keys key key_len ref rows filtered Extra
  1584. 1 SIMPLE t1 system NULL NULL NULL NULL 1 100.00
  1585. Warnings:
  1586. Note 1003 (select 'tttt' AS `s1` from dual)
  1587. (select * from t1);
  1588. s1
  1589. tttt
  1590. drop table t1;
  1591. create table t1 (s1 char(5), index s1(s1));
  1592. create table t2 (s1 char(5), index s1(s1));
  1593. insert into t1 values ('a1'),('a2'),('a3');
  1594. insert into t2 values ('a1'),('a2');
  1595. select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
  1596. s1 s1 NOT IN (SELECT s1 FROM t2)
  1597. a1 0
  1598. a2 0
  1599. a3 1
  1600. select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
  1601. s1 s1 = ANY (SELECT s1 FROM t2)
  1602. a1 1
  1603. a2 1
  1604. a3 0
  1605. select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
  1606. s1 s1 <> ALL (SELECT s1 FROM t2)
  1607. a1 0
  1608. a2 0
  1609. a3 1
  1610. select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
  1611. s1 s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2')
  1612. a1 0
  1613. a2 1
  1614. a3 1
  1615. explain extended select s1, s1 NOT IN (SELECT s1 FROM t2) from t1;
  1616. id select_type table type possible_keys key key_len ref rows filtered Extra
  1617. 1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index
  1618. 2 MATERIALIZED t2 index s1 s1 6 NULL 2 100.00 Using index
  1619. Warnings:
  1620. Note 1003 /* select#1 */ select `test`.`t1`.`s1` AS `s1`,!<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,`test`.`t1`.`s1` in ( <materialize> (/* select#2 */ select `test`.`t2`.`s1` from `test`.`t2` ), <primary_index_lookup>(`test`.`t1`.`s1` in <temporary table> on distinct_key where `test`.`t1`.`s1` = `<subquery2>`.`s1`)))) AS `s1 NOT IN (SELECT s1 FROM t2)` from `test`.`t1`
  1621. explain extended select s1, s1 = ANY (SELECT s1 FROM t2) from t1;
  1622. id select_type table type possible_keys key key_len ref rows filtered Extra
  1623. 1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index
  1624. 2 MATERIALIZED t2 index s1 s1 6 NULL 2 100.00 Using index
  1625. Warnings:
  1626. Note 1003 /* select#1 */ select `test`.`t1`.`s1` AS `s1`,<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,`test`.`t1`.`s1` in ( <materialize> (/* select#2 */ select `test`.`t2`.`s1` from `test`.`t2` ), <primary_index_lookup>(`test`.`t1`.`s1` in <temporary table> on distinct_key where `test`.`t1`.`s1` = `<subquery2>`.`s1`)))) AS `s1 = ANY (SELECT s1 FROM t2)` from `test`.`t1`
  1627. explain extended select s1, s1 <> ALL (SELECT s1 FROM t2) from t1;
  1628. id select_type table type possible_keys key key_len ref rows filtered Extra
  1629. 1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index
  1630. 2 MATERIALIZED t2 index s1 s1 6 NULL 2 100.00 Using index
  1631. Warnings:
  1632. Note 1003 /* select#1 */ select `test`.`t1`.`s1` AS `s1`,!<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,`test`.`t1`.`s1` in ( <materialize> (/* select#2 */ select `test`.`t2`.`s1` from `test`.`t2` ), <primary_index_lookup>(`test`.`t1`.`s1` in <temporary table> on distinct_key where `test`.`t1`.`s1` = `<subquery2>`.`s1`)))) AS `s1 <> ALL (SELECT s1 FROM t2)` from `test`.`t1`
  1633. explain extended select s1, s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2') from t1;
  1634. id select_type table type possible_keys key key_len ref rows filtered Extra
  1635. 1 PRIMARY t1 index NULL s1 6 NULL 3 100.00 Using index
  1636. 2 MATERIALIZED t2 index s1 s1 6 NULL 2 50.00 Using where; Using index
  1637. Warnings:
  1638. Note 1003 /* select#1 */ select `test`.`t1`.`s1` AS `s1`,!<expr_cache><`test`.`t1`.`s1`>(<in_optimizer>(`test`.`t1`.`s1`,`test`.`t1`.`s1` in ( <materialize> (/* select#2 */ select `test`.`t2`.`s1` from `test`.`t2` where `test`.`t2`.`s1` < 'a2' ), <primary_index_lookup>(`test`.`t1`.`s1` in <temporary table> on distinct_key where `test`.`t1`.`s1` = `<subquery2>`.`s1`)))) AS `s1 NOT IN (SELECT s1 FROM t2 WHERE s1 < 'a2')` from `test`.`t1`
  1639. drop table t1,t2;
  1640. create table t2 (a int, b int not null);
  1641. create table t3 (a int);
  1642. insert into t3 values (6),(7),(3);
  1643. select * from t3 where a >= all (select b from t2);
  1644. a
  1645. 6
  1646. 7
  1647. 3
  1648. explain extended select * from t3 where a >= all (select b from t2);
  1649. id select_type table type possible_keys key key_len ref rows filtered Extra
  1650. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
  1651. 2 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 Const row not found
  1652. Warnings:
  1653. Note 1003 /* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where <not>(<in_optimizer>(`test`.`t3`.`a`,(/* select#2 */ select max(NULL) from `test`.`t2`) > <cache>(`test`.`t3`.`a`)))
  1654. select * from t3 where a >= some (select b from t2);
  1655. a
  1656. explain extended select * from t3 where a >= some (select b from t2);
  1657. id select_type table type possible_keys key key_len ref rows filtered Extra
  1658. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
  1659. 2 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 Const row not found
  1660. Warnings:
  1661. Note 1003 /* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(`test`.`t3`.`a`,(/* select#2 */ select min(NULL) from `test`.`t2`) <= <cache>(`test`.`t3`.`a`)))
  1662. select * from t3 where a >= all (select b from t2 group by 1);
  1663. a
  1664. 6
  1665. 7
  1666. 3
  1667. explain extended select * from t3 where a >= all (select b from t2 group by 1);
  1668. id select_type table type possible_keys key key_len ref rows filtered Extra
  1669. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
  1670. 2 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 Const row not found
  1671. Warnings:
  1672. Note 1003 /* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where <not>(<in_optimizer>(`test`.`t3`.`a`,(/* select#2 */ select max(NULL) from `test`.`t2`) > <cache>(`test`.`t3`.`a`)))
  1673. select * from t3 where a >= some (select b from t2 group by 1);
  1674. a
  1675. explain extended select * from t3 where a >= some (select b from t2 group by 1);
  1676. id select_type table type possible_keys key key_len ref rows filtered Extra
  1677. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
  1678. 2 SUBQUERY t2 system NULL NULL NULL NULL 0 0.00 Const row not found
  1679. Warnings:
  1680. Note 1003 /* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where <nop>(<in_optimizer>(`test`.`t3`.`a`,(/* select#2 */ select min(NULL) from `test`.`t2`) <= <cache>(`test`.`t3`.`a`)))
  1681. select * from t3 where NULL >= any (select b from t2);
  1682. a
  1683. explain extended select * from t3 where NULL >= any (select b from t2);
  1684. id select_type table type possible_keys key key_len ref rows filtered Extra
  1685. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  1686. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  1687. Warnings:
  1688. Note 1003 /* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
  1689. select * from t3 where NULL >= any (select b from t2 group by 1);
  1690. a
  1691. explain extended select * from t3 where NULL >= any (select b from t2 group by 1);
  1692. id select_type table type possible_keys key key_len ref rows filtered Extra
  1693. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  1694. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  1695. Warnings:
  1696. Note 1003 /* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
  1697. select * from t3 where NULL >= some (select b from t2);
  1698. a
  1699. explain extended select * from t3 where NULL >= some (select b from t2);
  1700. id select_type table type possible_keys key key_len ref rows filtered Extra
  1701. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  1702. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  1703. Warnings:
  1704. Note 1003 /* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
  1705. select * from t3 where NULL >= some (select b from t2 group by 1);
  1706. a
  1707. explain extended select * from t3 where NULL >= some (select b from t2 group by 1);
  1708. id select_type table type possible_keys key key_len ref rows filtered Extra
  1709. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  1710. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  1711. Warnings:
  1712. Note 1003 /* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where 0
  1713. insert into t2 values (2,2), (2,1), (3,3), (3,1);
  1714. select * from t3 where a > all (select max(b) from t2 group by a);
  1715. a
  1716. 6
  1717. 7
  1718. explain extended select * from t3 where a > all (select max(b) from t2 group by a);
  1719. id select_type table type possible_keys key key_len ref rows filtered Extra
  1720. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 100.00 Using where
  1721. 2 SUBQUERY t2 ALL NULL NULL NULL NULL 4 100.00 Using temporary
  1722. Warnings:
  1723. Note 1003 /* select#1 */ select `test`.`t3`.`a` AS `a` from `test`.`t3` where <not>(<in_optimizer>(`test`.`t3`.`a`,<max>(/* select#2 */ select max(`test`.`t2`.`b`) from `test`.`t2` group by `test`.`t2`.`a`) >= <cache>(`test`.`t3`.`a`)))
  1724. drop table t2, t3;
  1725. 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 ;
  1726. 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());
  1727. 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;
  1728. 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);
  1729. 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 ;
  1730. 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);
  1731. CREATE TABLE `t4` (`task_id` smallint(6) NOT NULL default '0',`description` varchar(200) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
  1732. INSERT INTO `t4` (`task_id`, `description`) VALUES (1, 'Daily Check List'),(2, 'Weekly Status');
  1733. 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;
  1734. 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')
  1735. -1 Valid 1
  1736. -1 Valid 2 1
  1737. -1 Should Not Return 0
  1738. 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;
  1739. dbid name
  1740. -1 Valid
  1741. -1 Valid 2
  1742. drop table t1,t2,t3,t4;
  1743. CREATE TABLE t1 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1;
  1744. INSERT INTO t1 VALUES (1),(5);
  1745. CREATE TABLE t2 (id int(11) default NULL) ENGINE=MyISAM CHARSET=latin1;
  1746. INSERT INTO t2 VALUES (2),(6);
  1747. select * from t1 where (1,2,6) in (select * from t2);
  1748. ERROR 21000: Operand should contain 3 column(s)
  1749. DROP TABLE t1,t2;
  1750. create table t1 (s1 int);
  1751. insert into t1 values (1);
  1752. insert into t1 values (2);
  1753. set sort_buffer_size = (select s1 from t1);
  1754. ERROR 21000: Subquery returns more than 1 row
  1755. do (select * from t1);
  1756. Warnings:
  1757. Error 1242 Subquery returns more than 1 row
  1758. drop table t1;
  1759. create table t1 (s1 char);
  1760. insert into t1 values ('e');
  1761. select * from t1 where 'f' > any (select s1 from t1);
  1762. s1
  1763. e
  1764. select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
  1765. s1
  1766. e
  1767. explain extended select * from t1 where 'f' > any (select s1 from t1 union select s1 from t1);
  1768. id select_type table type possible_keys key key_len ref rows filtered Extra
  1769. 1 PRIMARY t1 system NULL NULL NULL NULL 1 100.00
  1770. 2 SUBQUERY t1 system NULL NULL NULL NULL 1 100.00
  1771. 3 UNION t1 system NULL NULL NULL NULL 1 100.00
  1772. NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
  1773. Warnings:
  1774. Note 1003 /* select#1 */ select 'e' AS `s1` from dual where 1
  1775. drop table t1;
  1776. CREATE TABLE t1 (number char(11) NOT NULL default '') ENGINE=MyISAM CHARSET=latin1;
  1777. INSERT INTO t1 VALUES ('69294728265'),('18621828126'),('89356874041'),('95895001874');
  1778. CREATE TABLE t2 (code char(5) NOT NULL default '',UNIQUE KEY code (code)) ENGINE=MyISAM CHARSET=latin1;
  1779. INSERT INTO t2 VALUES ('1'),('1226'),('1245'),('1862'),('18623'),('1874'),('1967'),('6');
  1780. 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;
  1781. phone code
  1782. 69294728265 6
  1783. 18621828126 1862
  1784. 89356874041 NULL
  1785. 95895001874 NULL
  1786. drop table t1, t2;
  1787. create table t1 (s1 int);
  1788. create table t2 (s1 int);
  1789. select * from t1 where (select count(*) from t2 where t1.s2) = 1;
  1790. ERROR 42S22: Unknown column 't1.s2' in 'where clause'
  1791. select * from t1 where (select count(*) from t2 group by t1.s2) = 1;
  1792. ERROR 42S22: Unknown column 't1.s2' in 'group statement'
  1793. select count(*) from t2 group by t1.s2;
  1794. ERROR 42S22: Unknown column 't1.s2' in 'group statement'
  1795. drop table t1, t2;
  1796. CREATE TABLE t1(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC VARCHAR(20) DEFAULT NULL,PRIMARY KEY (COLA, COLB));
  1797. CREATE TABLE t2(COLA FLOAT NOT NULL,COLB FLOAT NOT NULL,COLC CHAR(1) NOT NULL,PRIMARY KEY (COLA));
  1798. INSERT INTO t1 VALUES (1,1,'1A3240'), (1,2,'4W2365');
  1799. INSERT INTO t2 VALUES (100, 200, 'C');
  1800. SELECT DISTINCT COLC FROM t1 WHERE COLA = (SELECT COLA FROM t2 WHERE COLB = 200 AND COLC ='C' LIMIT 1);
  1801. COLC
  1802. DROP TABLE t1, t2;
  1803. CREATE TABLE t1 (a int(1));
  1804. INSERT INTO t1 VALUES (1),(1),(1),(1),(1),(2),(3),(4),(5);
  1805. SELECT DISTINCT (SELECT a) FROM t1 LIMIT 100;
  1806. (SELECT a)
  1807. 1
  1808. 2
  1809. 3
  1810. 4
  1811. 5
  1812. DROP TABLE t1;
  1813. create table t1 (a int, b decimal(13, 3));
  1814. insert into t1 values (1, 0.123);
  1815. select a, (select max(b) from t1) into outfile "../../tmp/subselect.out.file.1" from t1;
  1816. delete from t1;
  1817. load data infile "../../tmp/subselect.out.file.1" into table t1;
  1818. select * from t1;
  1819. a b
  1820. 1 0.123
  1821. drop table t1;
  1822. CREATE TABLE `t1` (
  1823. `id` int(11) NOT NULL auto_increment,
  1824. `id_cns` tinyint(3) unsigned NOT NULL default '0',
  1825. `tipo` enum('','UNO','DUE') NOT NULL default '',
  1826. `anno_dep` smallint(4) unsigned zerofill NOT NULL default '0000',
  1827. `particolare` mediumint(8) unsigned NOT NULL default '0',
  1828. `generale` mediumint(8) unsigned NOT NULL default '0',
  1829. `bis` tinyint(3) unsigned NOT NULL default '0',
  1830. PRIMARY KEY (`id`),
  1831. UNIQUE KEY `idx_cns_gen_anno` (`anno_dep`,`id_cns`,`generale`,`particolare`),
  1832. UNIQUE KEY `idx_cns_par_anno` (`id_cns`,`anno_dep`,`tipo`,`particolare`,`bis`)
  1833. );
  1834. 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);
  1835. CREATE TABLE `t2` (
  1836. `id` tinyint(3) unsigned NOT NULL auto_increment,
  1837. `max_anno_dep` smallint(6) unsigned NOT NULL default '0',
  1838. PRIMARY KEY (`id`)
  1839. );
  1840. INSERT INTO `t2` VALUES (16,1987),(50,1990),(51,1990);
  1841. 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;
  1842. id max_anno_dep PIPPO
  1843. 16 1987 1
  1844. 50 1990 0
  1845. 51 1990 NULL
  1846. DROP TABLE t1, t2;
  1847. create table t1 (a int);
  1848. insert into t1 values (1), (2), (3);
  1849. SET SQL_SELECT_LIMIT=1;
  1850. select sum(a) from (select * from t1) as a;
  1851. sum(a)
  1852. 6
  1853. select 2 in (select * from t1);
  1854. 2 in (select * from t1)
  1855. 1
  1856. SET SQL_SELECT_LIMIT=default;
  1857. drop table t1;
  1858. CREATE TABLE t1 (a int, b int, INDEX (a));
  1859. INSERT INTO t1 VALUES (1, 1), (1, 2), (1, 3);
  1860. SELECT * FROM t1 WHERE a = (SELECT MAX(a) FROM t1 WHERE a = 1) ORDER BY b;
  1861. a b
  1862. 1 1
  1863. 1 2
  1864. 1 3
  1865. DROP TABLE t1;
  1866. create table t1(val varchar(10));
  1867. insert into t1 values ('aaa'), ('bbb'),('eee'),('mmm'),('ppp');
  1868. 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%');
  1869. count(*)
  1870. 0
  1871. drop table t1;
  1872. create table t1 (id int not null, text varchar(20) not null default '', primary key (id));
  1873. 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');
  1874. select * from t1 where id not in (select id from t1 where id < 8);
  1875. id text
  1876. 8 text8
  1877. 9 text9
  1878. 10 text10
  1879. 11 text11
  1880. 12 text12
  1881. 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);
  1882. id text
  1883. 8 text8
  1884. 9 text9
  1885. 10 text10
  1886. 11 text11
  1887. 12 text12
  1888. explain extended select * from t1 where id not in (select id from t1 where id < 8);
  1889. id select_type table type possible_keys key key_len ref rows filtered Extra
  1890. 1 PRIMARY t1 ALL NULL NULL NULL NULL 12 100.00 Using where
  1891. 2 MATERIALIZED t1 range PRIMARY PRIMARY 4 NULL 7 100.00 Using where; Using index
  1892. Warnings:
  1893. Note 1003 /* select#1 */ select `test`.`t1`.`id` AS `id`,`test`.`t1`.`text` AS `text` from `test`.`t1` where !<expr_cache><`test`.`t1`.`id`>(<in_optimizer>(`test`.`t1`.`id`,`test`.`t1`.`id` in ( <materialize> (/* select#2 */ select `test`.`t1`.`id` from `test`.`t1` where `test`.`t1`.`id` < 8 ), <primary_index_lookup>(`test`.`t1`.`id` in <temporary table> on distinct_key where `test`.`t1`.`id` = `<subquery2>`.`id`))))
  1894. 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);
  1895. id select_type table type possible_keys key key_len ref rows filtered Extra
  1896. 1 PRIMARY tt ALL NULL NULL NULL NULL 12 100.00 Using where
  1897. 2 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.tt.id 1 100.00 Using where; Using index
  1898. Warnings:
  1899. Note 1276 Field or reference 'test.tt.id' of SELECT #2 was resolved in SELECT #1
  1900. Note 1003 /* select#1 */ select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where !<in_optimizer>(1,<expr_cache><`test`.`tt`.`id`>(exists(/* select#2 */ 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)))
  1901. insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001');
  1902. create table t2 (id int not null, text varchar(20) not null default '', primary key (id));
  1903. 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');
  1904. 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);
  1905. id text id text id text
  1906. 1 text1 1 text1 1 text1
  1907. 2 text2 2 text2 2 text2
  1908. 3 text3 3 text3 3 text3
  1909. 4 text4 4 text4 4 text4
  1910. 5 text5 5 text5 5 text5
  1911. 6 text6 6 text6 6 text6
  1912. 7 text7 7 text7 7 text7
  1913. 8 text8 8 text8 8 text8
  1914. 9 text9 9 text9 9 text9
  1915. 10 text10 10 text10 10 text10
  1916. 11 text11 11 text1 11 text11
  1917. 12 text12 12 text2 12 text12
  1918. 1000 text1000 NULL NULL 1000 text1000
  1919. 1001 text1001 NULL NULL 1000 text1000
  1920. 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);
  1921. id select_type table type possible_keys key key_len ref rows filtered Extra
  1922. 1 SIMPLE a ALL NULL NULL NULL NULL 14 100.00
  1923. 1 SIMPLE b eq_ref PRIMARY PRIMARY 4 test.a.id 2 100.00
  1924. 1 SIMPLE c eq_ref PRIMARY PRIMARY 4 func 1 100.00 Using index condition
  1925. Warnings:
  1926. 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 `test`.`b`.`id` is null) join `test`.`t1` `c` where if(`test`.`b`.`id` is null,1000,`test`.`b`.`id`) = `test`.`c`.`id`
  1927. drop table t1,t2;
  1928. create table t1 (a int);
  1929. insert into t1 values (1);
  1930. explain select benchmark(1000, (select a from t1 where a=sha(rand())));
  1931. id select_type table type possible_keys key key_len ref rows Extra
  1932. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  1933. 2 UNCACHEABLE SUBQUERY t1 system NULL NULL NULL NULL 1
  1934. drop table t1;
  1935. create table t1(id int);
  1936. create table t2(id int);
  1937. create table t3(flag int);
  1938. select (select * from t3 where id not null) from t1, t2;
  1939. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'null) from t1, t2' at line 1
  1940. drop table t1,t2,t3;
  1941. CREATE TABLE t1 (id INT);
  1942. CREATE TABLE t2 (id INT);
  1943. INSERT INTO t1 VALUES (1), (2);
  1944. INSERT INTO t2 VALUES (1);
  1945. 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);
  1946. id c
  1947. 1 1
  1948. 2 0
  1949. SELECT id, ( SELECT COUNT(t.id) FROM t2 AS t WHERE t.id = t1.id ) AS c FROM t1 LEFT JOIN t2 USING (id);
  1950. id c
  1951. 1 1
  1952. 2 0
  1953. 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;
  1954. id c
  1955. 1 1
  1956. 2 0
  1957. 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;
  1958. id c
  1959. 1 1
  1960. 2 0
  1961. DROP TABLE t1,t2;
  1962. CREATE TABLE t1 ( a int, b int );
  1963. INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
  1964. SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
  1965. a
  1966. 3
  1967. SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
  1968. a
  1969. 1
  1970. SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
  1971. a
  1972. 2
  1973. SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
  1974. a
  1975. 2
  1976. 3
  1977. SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
  1978. a
  1979. 1
  1980. 2
  1981. SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
  1982. a
  1983. 1
  1984. 3
  1985. SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
  1986. a
  1987. 3
  1988. SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
  1989. a
  1990. 1
  1991. SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
  1992. a
  1993. 2
  1994. SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
  1995. a
  1996. 2
  1997. 3
  1998. SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
  1999. a
  2000. 1
  2001. 2
  2002. SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
  2003. a
  2004. 1
  2005. 3
  2006. ALTER TABLE t1 ADD INDEX (a);
  2007. SELECT a FROM t1 WHERE a > ANY ( SELECT a FROM t1 WHERE b = 2 );
  2008. a
  2009. 3
  2010. SELECT a FROM t1 WHERE a < ANY ( SELECT a FROM t1 WHERE b = 2 );
  2011. a
  2012. 1
  2013. SELECT a FROM t1 WHERE a = ANY ( SELECT a FROM t1 WHERE b = 2 );
  2014. a
  2015. 2
  2016. SELECT a FROM t1 WHERE a >= ANY ( SELECT a FROM t1 WHERE b = 2 );
  2017. a
  2018. 2
  2019. 3
  2020. SELECT a FROM t1 WHERE a <= ANY ( SELECT a FROM t1 WHERE b = 2 );
  2021. a
  2022. 1
  2023. 2
  2024. SELECT a FROM t1 WHERE a <> ANY ( SELECT a FROM t1 WHERE b = 2 );
  2025. a
  2026. 1
  2027. 3
  2028. SELECT a FROM t1 WHERE a > ALL ( SELECT a FROM t1 WHERE b = 2 );
  2029. a
  2030. 3
  2031. SELECT a FROM t1 WHERE a < ALL ( SELECT a FROM t1 WHERE b = 2 );
  2032. a
  2033. 1
  2034. SELECT a FROM t1 WHERE a = ALL ( SELECT a FROM t1 WHERE b = 2 );
  2035. a
  2036. 2
  2037. SELECT a FROM t1 WHERE a >= ALL ( SELECT a FROM t1 WHERE b = 2 );
  2038. a
  2039. 2
  2040. 3
  2041. SELECT a FROM t1 WHERE a <= ALL ( SELECT a FROM t1 WHERE b = 2 );
  2042. a
  2043. 1
  2044. 2
  2045. SELECT a FROM t1 WHERE a <> ALL ( SELECT a FROM t1 WHERE b = 2 );
  2046. a
  2047. 1
  2048. 3
  2049. SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2);
  2050. a
  2051. 3
  2052. SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2);
  2053. a
  2054. 1
  2055. SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2);
  2056. a
  2057. 2
  2058. SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2);
  2059. a
  2060. 2
  2061. 3
  2062. SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2);
  2063. a
  2064. 1
  2065. 2
  2066. SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2);
  2067. a
  2068. 1
  2069. 3
  2070. SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2);
  2071. a
  2072. 3
  2073. SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2);
  2074. a
  2075. 1
  2076. SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2);
  2077. a
  2078. 2
  2079. SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2);
  2080. a
  2081. 2
  2082. 3
  2083. SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2);
  2084. a
  2085. 1
  2086. 2
  2087. SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2);
  2088. a
  2089. 1
  2090. 3
  2091. SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2092. a
  2093. 3
  2094. SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2095. a
  2096. 1
  2097. SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2098. a
  2099. 2
  2100. SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2101. a
  2102. 2
  2103. 3
  2104. SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2105. a
  2106. 1
  2107. 2
  2108. SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2109. a
  2110. 1
  2111. 3
  2112. SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2113. a
  2114. 3
  2115. SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2116. a
  2117. 1
  2118. SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2119. a
  2120. 2
  2121. SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2122. a
  2123. 2
  2124. 3
  2125. SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2126. a
  2127. 1
  2128. 2
  2129. SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 UNION SELECT a FROM t1 WHERE b = 2);
  2130. a
  2131. 1
  2132. 3
  2133. SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2134. a
  2135. 3
  2136. SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2137. a
  2138. 1
  2139. SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2140. a
  2141. 2
  2142. SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2143. a
  2144. 2
  2145. 3
  2146. SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2147. a
  2148. 1
  2149. 2
  2150. SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2151. a
  2152. 1
  2153. 3
  2154. SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2155. a
  2156. 3
  2157. SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2158. a
  2159. 1
  2160. SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2161. a
  2162. 2
  2163. SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2164. a
  2165. 2
  2166. 3
  2167. SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2168. a
  2169. 1
  2170. 2
  2171. SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 HAVING a = 2 UNION SELECT a FROM t1 HAVING a = 2);
  2172. a
  2173. 1
  2174. 3
  2175. SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a FROM t1 WHERE b = 2);
  2176. ERROR 21000: Operand should contain 1 column(s)
  2177. SELECT a FROM t1 WHERE a > ANY (SELECT a,2 FROM t1 WHERE b = 2);
  2178. ERROR 21000: Operand should contain 1 column(s)
  2179. SELECT a FROM t1 WHERE (1,2) > ANY (SELECT a,2 FROM t1 WHERE b = 2);
  2180. ERROR 21000: Operand should contain 1 column(s)
  2181. SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a FROM t1 WHERE b = 2);
  2182. ERROR 21000: Operand should contain 1 column(s)
  2183. SELECT a FROM t1 WHERE a > ALL (SELECT a,2 FROM t1 WHERE b = 2);
  2184. ERROR 21000: Operand should contain 1 column(s)
  2185. SELECT a FROM t1 WHERE (1,2) > ALL (SELECT a,2 FROM t1 WHERE b = 2);
  2186. ERROR 21000: Operand should contain 1 column(s)
  2187. SELECT a FROM t1 WHERE (1,2) = ALL (SELECT a,2 FROM t1 WHERE b = 2);
  2188. ERROR 21000: Operand should contain 1 column(s)
  2189. SELECT a FROM t1 WHERE (1,2) <> ANY (SELECT a,2 FROM t1 WHERE b = 2);
  2190. ERROR 21000: Operand should contain 1 column(s)
  2191. SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a FROM t1 WHERE b = 2);
  2192. ERROR 21000: Operand should contain 2 column(s)
  2193. SELECT a FROM t1 WHERE a = ANY (SELECT a,2 FROM t1 WHERE b = 2);
  2194. ERROR 21000: Operand should contain 1 column(s)
  2195. SELECT a FROM t1 WHERE (1,2) = ANY (SELECT a,2 FROM t1 WHERE b = 2);
  2196. a
  2197. SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a FROM t1 WHERE b = 2);
  2198. ERROR 21000: Operand should contain 2 column(s)
  2199. SELECT a FROM t1 WHERE a <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
  2200. ERROR 21000: Operand should contain 1 column(s)
  2201. SELECT a FROM t1 WHERE (1,2) <> ALL (SELECT a,2 FROM t1 WHERE b = 2);
  2202. a
  2203. 1
  2204. 2
  2205. 3
  2206. SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 WHERE b = 2);
  2207. a
  2208. 2
  2209. SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 WHERE b = 2);
  2210. a
  2211. 1
  2212. 3
  2213. SELECT a FROM t1 WHERE (a,1) = ANY (SELECT a,1 FROM t1 HAVING a = 2);
  2214. a
  2215. 2
  2216. SELECT a FROM t1 WHERE (a,1) <> ALL (SELECT a,1 FROM t1 HAVING a = 2);
  2217. a
  2218. 1
  2219. 3
  2220. 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);
  2221. a
  2222. 2
  2223. 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);
  2224. a
  2225. 1
  2226. 3
  2227. 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);
  2228. a
  2229. 2
  2230. 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);
  2231. a
  2232. 1
  2233. 3
  2234. SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2 group by a);
  2235. a
  2236. 3
  2237. SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2 group by a);
  2238. a
  2239. 1
  2240. SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2 group by a);
  2241. a
  2242. 2
  2243. SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2 group by a);
  2244. a
  2245. 2
  2246. 3
  2247. SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2 group by a);
  2248. a
  2249. 1
  2250. 2
  2251. SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2 group by a);
  2252. a
  2253. 1
  2254. 3
  2255. SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2 group by a);
  2256. a
  2257. 3
  2258. SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2 group by a);
  2259. a
  2260. 1
  2261. SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2 group by a);
  2262. a
  2263. 2
  2264. SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2 group by a);
  2265. a
  2266. 2
  2267. 3
  2268. SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2 group by a);
  2269. a
  2270. 1
  2271. 2
  2272. SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2 group by a);
  2273. a
  2274. 1
  2275. 3
  2276. SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 group by a HAVING a = 2);
  2277. a
  2278. 3
  2279. SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 group by a HAVING a = 2);
  2280. a
  2281. 1
  2282. SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 group by a HAVING a = 2);
  2283. a
  2284. 2
  2285. SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 group by a HAVING a = 2);
  2286. a
  2287. 2
  2288. 3
  2289. SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 group by a HAVING a = 2);
  2290. a
  2291. 1
  2292. 2
  2293. SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 group by a HAVING a = 2);
  2294. a
  2295. 1
  2296. 3
  2297. SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 group by a HAVING a = 2);
  2298. a
  2299. 3
  2300. SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 group by a HAVING a = 2);
  2301. a
  2302. 1
  2303. SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 group by a HAVING a = 2);
  2304. a
  2305. 2
  2306. SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 group by a HAVING a = 2);
  2307. a
  2308. 2
  2309. 3
  2310. SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 group by a HAVING a = 2);
  2311. a
  2312. 1
  2313. 2
  2314. SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 group by a HAVING a = 2);
  2315. a
  2316. 1
  2317. 3
  2318. SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-') from t1 a;
  2319. concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a > t1.a), '-')
  2320. 0-
  2321. 0-
  2322. 1-
  2323. SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-') from t1 a;
  2324. concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a < t1.a), '-')
  2325. 1-
  2326. 0-
  2327. 0-
  2328. SELECT concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-') from t1 a;
  2329. concat(EXISTS(SELECT a FROM t1 WHERE b = 2 and a.a = t1.a), '-')
  2330. 0-
  2331. 1-
  2332. 0-
  2333. DROP TABLE t1;
  2334. CREATE TABLE t1 ( a double, b double );
  2335. INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
  2336. SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = 2e0);
  2337. a
  2338. 3
  2339. SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = 2e0);
  2340. a
  2341. 1
  2342. SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = 2e0);
  2343. a
  2344. 2
  2345. SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = 2e0);
  2346. a
  2347. 2
  2348. 3
  2349. SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = 2e0);
  2350. a
  2351. 1
  2352. 2
  2353. SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = 2e0);
  2354. a
  2355. 1
  2356. 3
  2357. SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = 2e0);
  2358. a
  2359. 3
  2360. SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = 2e0);
  2361. a
  2362. 1
  2363. SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = 2e0);
  2364. a
  2365. 2
  2366. SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = 2e0);
  2367. a
  2368. 2
  2369. 3
  2370. SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = 2e0);
  2371. a
  2372. 1
  2373. 2
  2374. SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = 2e0);
  2375. a
  2376. 1
  2377. 3
  2378. DROP TABLE t1;
  2379. CREATE TABLE t1 ( a char(1), b char(1));
  2380. INSERT INTO t1 VALUES ('1','1'),('2','2'),('3','3');
  2381. SELECT a FROM t1 WHERE a > ANY (SELECT a FROM t1 WHERE b = '2');
  2382. a
  2383. 3
  2384. SELECT a FROM t1 WHERE a < ANY (SELECT a FROM t1 WHERE b = '2');
  2385. a
  2386. 1
  2387. SELECT a FROM t1 WHERE a = ANY (SELECT a FROM t1 WHERE b = '2');
  2388. a
  2389. 2
  2390. SELECT a FROM t1 WHERE a >= ANY (SELECT a FROM t1 WHERE b = '2');
  2391. a
  2392. 2
  2393. 3
  2394. SELECT a FROM t1 WHERE a <= ANY (SELECT a FROM t1 WHERE b = '2');
  2395. a
  2396. 1
  2397. 2
  2398. SELECT a FROM t1 WHERE a <> ANY (SELECT a FROM t1 WHERE b = '2');
  2399. a
  2400. 1
  2401. 3
  2402. SELECT a FROM t1 WHERE a > ALL (SELECT a FROM t1 WHERE b = '2');
  2403. a
  2404. 3
  2405. SELECT a FROM t1 WHERE a < ALL (SELECT a FROM t1 WHERE b = '2');
  2406. a
  2407. 1
  2408. SELECT a FROM t1 WHERE a = ALL (SELECT a FROM t1 WHERE b = '2');
  2409. a
  2410. 2
  2411. SELECT a FROM t1 WHERE a >= ALL (SELECT a FROM t1 WHERE b = '2');
  2412. a
  2413. 2
  2414. 3
  2415. SELECT a FROM t1 WHERE a <= ALL (SELECT a FROM t1 WHERE b = '2');
  2416. a
  2417. 1
  2418. 2
  2419. SELECT a FROM t1 WHERE a <> ALL (SELECT a FROM t1 WHERE b = '2');
  2420. a
  2421. 1
  2422. 3
  2423. DROP TABLE t1;
  2424. create table t1 (a int, b int);
  2425. insert into t1 values (1,2),(3,4);
  2426. select * from t1 up where exists (select * from t1 where t1.a=up.a);
  2427. a b
  2428. 1 2
  2429. 3 4
  2430. explain extended select * from t1 up where exists (select * from t1 where t1.a=up.a);
  2431. id select_type table type possible_keys key key_len ref rows filtered Extra
  2432. 1 PRIMARY up ALL NULL NULL NULL NULL 2 100.00 Using where
  2433. 2 MATERIALIZED t1 ALL NULL NULL NULL NULL 2 100.00
  2434. Warnings:
  2435. Note 1276 Field or reference 'test.up.a' of SELECT #2 was resolved in SELECT #1
  2436. Note 1003 /* select#1 */ select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` where <expr_cache><`test`.`up`.`a`>(<in_optimizer>(`test`.`up`.`a`,`test`.`up`.`a` in ( <materialize> (/* select#2 */ select `test`.`t1`.`a` from `test`.`t1` where 1 ), <primary_index_lookup>(`test`.`up`.`a` in <temporary table> on distinct_key where `test`.`up`.`a` = `<subquery2>`.`a`))))
  2437. drop table t1;
  2438. CREATE TABLE t1 (t1_a int);
  2439. INSERT INTO t1 VALUES (1);
  2440. CREATE TABLE t2 (t2_a int, t2_b int, PRIMARY KEY (t2_a, t2_b));
  2441. INSERT INTO t2 VALUES (1, 1), (1, 2);
  2442. SELECT * FROM t1, t2 table2 WHERE t1_a = 1 AND table2.t2_a = 1
  2443. HAVING table2.t2_b = (SELECT MAX(t2_b) FROM t2 WHERE t2_a = table2.t2_a);
  2444. t1_a t2_a t2_b
  2445. 1 1 2
  2446. DROP TABLE t1, t2;
  2447. CREATE TABLE t1 (id int(11) default NULL,name varchar(10) default NULL);
  2448. INSERT INTO t1 VALUES (1,'Tim'),(2,'Rebecca'),(3,NULL);
  2449. CREATE TABLE t2 (id int(11) default NULL, pet varchar(10) default NULL);
  2450. INSERT INTO t2 VALUES (1,'Fido'),(2,'Spot'),(3,'Felix');
  2451. SELECT a.*, b.* FROM (SELECT * FROM t1) AS a JOIN t2 as b on a.id=b.id;
  2452. id name id pet
  2453. 1 Tim 1 Fido
  2454. 2 Rebecca 2 Spot
  2455. 3 NULL 3 Felix
  2456. drop table t1,t2;
  2457. CREATE TABLE t1 ( a int, b int );
  2458. CREATE TABLE t2 ( c int, d int );
  2459. INSERT INTO t1 VALUES (1,2), (2,3), (3,4);
  2460. SELECT a AS abc, b FROM t1 outr WHERE b =
  2461. (SELECT MIN(b) FROM t1 WHERE a=outr.a);
  2462. abc b
  2463. 1 2
  2464. 2 3
  2465. 3 4
  2466. INSERT INTO t2 SELECT a AS abc, b FROM t1 outr WHERE b =
  2467. (SELECT MIN(b) FROM t1 WHERE a=outr.a);
  2468. select * from t2;
  2469. c d
  2470. 1 2
  2471. 2 3
  2472. 3 4
  2473. CREATE TABLE t3 SELECT a AS abc, b FROM t1 outr WHERE b =
  2474. (SELECT MIN(b) FROM t1 WHERE a=outr.a);
  2475. select * from t3;
  2476. abc b
  2477. 1 2
  2478. 2 3
  2479. 3 4
  2480. 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);";
  2481. execute stmt1;
  2482. deallocate prepare stmt1;
  2483. select * from t2;
  2484. c d
  2485. 1 2
  2486. 2 3
  2487. 3 4
  2488. 1 2
  2489. 2 3
  2490. 3 4
  2491. drop table t3;
  2492. 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);";
  2493. execute stmt1;
  2494. select * from t3;
  2495. abc b
  2496. 1 2
  2497. 2 3
  2498. 3 4
  2499. deallocate prepare stmt1;
  2500. DROP TABLE t1, t2, t3;
  2501. CREATE TABLE `t1` ( `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  2502. insert into t1 values (1);
  2503. CREATE TABLE `t2` ( `b` int(11) default NULL, `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  2504. insert into t2 values (1,2);
  2505. select t000.a, count(*) `C` FROM t1 t000 GROUP BY t000.a HAVING count(*) > ALL (SELECT count(*) FROM t2 t001 WHERE t001.a=1);
  2506. a C
  2507. 1 1
  2508. drop table t1,t2;
  2509. create table t1 (a int not null auto_increment primary key, b varchar(40), fulltext(b));
  2510. insert into t1 (b) values ('ball'),('ball games'), ('games'), ('foo'), ('foobar'), ('Serg'), ('Sergei'),('Georg'), ('Patrik'),('Hakan');
  2511. create table t2 (a int);
  2512. insert into t2 values (1),(3),(2),(7);
  2513. select a,b from t1 where match(b) against ('Ball') > 0;
  2514. a b
  2515. 1 ball
  2516. 2 ball games
  2517. select a from t2 where a in (select a from t1 where match(b) against ('Ball') > 0);
  2518. a
  2519. 1
  2520. 2
  2521. drop table t1,t2;
  2522. 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);
  2523. CREATE INDEX AK01IZAVORGANG ON t1(izaAnalyseart_id,Kuerzel);
  2524. INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000001','601','D0000000001','I0000000001');
  2525. INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000002','602','D0000000001','I0000000001');
  2526. INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000003','603','D0000000001','I0000000001');
  2527. INSERT INTO t1(`IZAVORGANG_ID`,`KUERZEL`,`IZAANALYSEART_ID`,`IZAPMKZ_ID`)VALUES('D0000000004','101','D0000000001','I0000000001');
  2528. SELECT `IZAVORGANG_ID` FROM t1 WHERE `KUERZEL` IN(SELECT MIN(`KUERZEL`)`Feld1` FROM t1 WHERE `KUERZEL` LIKE'601%'And`IZAANALYSEART_ID`='D0000000001');
  2529. IZAVORGANG_ID
  2530. D0000000001
  2531. drop table t1;
  2532. CREATE TABLE `t1` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY (`aid`,`bid`));
  2533. CREATE TABLE `t2` ( `aid` int(11) NOT NULL default '0', `bid` int(11) NOT NULL default '0', PRIMARY KEY (`aid`,`bid`));
  2534. insert into t1 values (1,1),(1,2),(2,1),(2,2);
  2535. insert into t2 values (1,2),(2,2);
  2536. select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
  2537. aid bid
  2538. 1 1
  2539. 2 1
  2540. alter table t2 drop primary key;
  2541. alter table t2 add key KEY1 (aid, bid);
  2542. select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
  2543. aid bid
  2544. 1 1
  2545. 2 1
  2546. alter table t2 drop key KEY1;
  2547. alter table t2 add primary key (bid, aid);
  2548. select * from t1 where t1.aid not in (select aid from t2 where bid=t1.bid);
  2549. aid bid
  2550. 1 1
  2551. 2 1
  2552. drop table t1,t2;
  2553. CREATE TABLE t1 (howmanyvalues bigint, avalue int);
  2554. INSERT INTO t1 VALUES (1, 1),(2, 1),(2, 2),(3, 1),(3, 2),(3, 3),(4, 1),(4, 2),(4, 3),(4, 4);
  2555. SELECT howmanyvalues, count(*) from t1 group by howmanyvalues;
  2556. howmanyvalues count(*)
  2557. 1 1
  2558. 2 2
  2559. 3 3
  2560. 4 4
  2561. SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues;
  2562. howmanyvalues mycount
  2563. 1 1
  2564. 2 2
  2565. 3 3
  2566. 4 4
  2567. CREATE INDEX t1_howmanyvalues_idx ON t1 (howmanyvalues);
  2568. SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues+1 = a.howmanyvalues+1) as mycount from t1 a group by a.howmanyvalues;
  2569. howmanyvalues mycount
  2570. 1 1
  2571. 2 2
  2572. 3 3
  2573. 4 4
  2574. SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.howmanyvalues) as mycount from t1 a group by a.howmanyvalues;
  2575. howmanyvalues mycount
  2576. 1 1
  2577. 2 2
  2578. 3 3
  2579. 4 4
  2580. SELECT a.howmanyvalues, (SELECT count(*) from t1 b where b.howmanyvalues = a.avalue) as mycount from t1 a group by a.howmanyvalues;
  2581. howmanyvalues mycount
  2582. 1 1
  2583. 2 1
  2584. 3 1
  2585. 4 1
  2586. drop table t1;
  2587. create table t1 (x int);
  2588. 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;
  2589. (select b.x from t1 as b where b.x=a.x)
  2590. drop table t1;
  2591. 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`));
  2592. 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);
  2593. 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`)) ;
  2594. 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');
  2595. 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;
  2596. ERROR 42S22: Unknown column 'b.sc' in 'field list'
  2597. 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;
  2598. ac
  2599. 700
  2600. NULL
  2601. drop tables t1,t2;
  2602. create table t1 (a int not null, b int not null, c int, primary key (a,b));
  2603. insert into t1 values (1,1,1), (2,2,2), (3,3,3);
  2604. set @b:= 0;
  2605. explain select sum(a) from t1 where b > @b;
  2606. id select_type table type possible_keys key key_len ref rows Extra
  2607. 1 SIMPLE t1 index NULL PRIMARY 8 NULL 3 Using where; Using index
  2608. set @a:= (select sum(a) from t1 where b > @b);
  2609. explain select a from t1 where c=2;
  2610. id select_type table type possible_keys key key_len ref rows Extra
  2611. 1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using where
  2612. do @a:= (select sum(a) from t1 where b > @b);
  2613. explain select a from t1 where c=2;
  2614. id select_type table type possible_keys key key_len ref rows Extra
  2615. 1 SIMPLE t1 ALL NULL NULL NULL NULL 3 Using where
  2616. drop table t1;
  2617. connect root,localhost,root,,test,$MASTER_MYPORT,$MASTER_MYSOCK;
  2618. connection root;
  2619. set @got_val= (SELECT 1 FROM (SELECT 'A' as my_col) as T1 ) ;
  2620. disconnect root;
  2621. connection default;
  2622. create table t1 (a int, b int);
  2623. create table t2 (a int, b int);
  2624. insert into t1 values (1,1),(1,2),(1,3),(2,4),(2,5);
  2625. insert into t2 values (1,3),(2,1);
  2626. select distinct a,b, (select max(b) from t2 where t1.b=t2.a) from t1 order by t1.b;
  2627. a b (select max(b) from t2 where t1.b=t2.a)
  2628. 1 1 3
  2629. 1 2 1
  2630. 1 3 NULL
  2631. 2 4 NULL
  2632. 2 5 NULL
  2633. drop table t1, t2;
  2634. create table t1 (id int);
  2635. create table t2 (id int, body text, fulltext (body));
  2636. insert into t1 values(1),(2),(3);
  2637. insert into t2 values (1,'test'), (2,'mysql'), (3,'test'), (4,'test');
  2638. select count(distinct id) from t1 where id in (select id from t2 where match(body) against ('mysql' in boolean mode));
  2639. count(distinct id)
  2640. 1
  2641. drop table t2,t1;
  2642. create table t1 (s1 int,s2 int);
  2643. insert into t1 values (20,15);
  2644. select * from t1 where (('a',null) <=> (select 'a',s2 from t1 where s1 = 0));
  2645. s1 s2
  2646. drop table t1;
  2647. create table t1 (s1 int);
  2648. insert into t1 values (1),(null);
  2649. select * from t1 where s1 < all (select s1 from t1);
  2650. s1
  2651. select s1, s1 < all (select s1 from t1) from t1;
  2652. s1 s1 < all (select s1 from t1)
  2653. 1 0
  2654. NULL NULL
  2655. drop table t1;
  2656. CREATE TABLE t1 (
  2657. Code char(3) NOT NULL default '',
  2658. Name char(52) NOT NULL default '',
  2659. Continent enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL default 'Asia',
  2660. Region char(26) NOT NULL default '',
  2661. SurfaceArea float(10,2) NOT NULL default '0.00',
  2662. IndepYear smallint(6) default NULL,
  2663. Population int(11) NOT NULL default '0',
  2664. LifeExpectancy float(3,1) default NULL,
  2665. GNP float(10,2) default NULL,
  2666. GNPOld float(10,2) default NULL,
  2667. LocalName char(45) NOT NULL default '',
  2668. GovernmentForm char(45) NOT NULL default '',
  2669. HeadOfState char(60) default NULL,
  2670. Capital int(11) default NULL,
  2671. Code2 char(2) NOT NULL default ''
  2672. ) ENGINE=MyISAM;
  2673. INSERT INTO t1 VALUES ('XXX','Xxxxx','Oceania','Xxxxxx',26.00,0,0,0,0,0,'Xxxxx','Xxxxx','Xxxxx',NULL,'XX');
  2674. 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');
  2675. 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');
  2676. 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');
  2677. /*!40000 ALTER TABLE t1 ENABLE KEYS */;
  2678. SELECT DISTINCT Continent AS c FROM t1 outr WHERE
  2679. Code <> SOME ( SELECT Code FROM t1 WHERE Continent = outr.Continent AND
  2680. Population < 200);
  2681. c
  2682. Oceania
  2683. drop table t1;
  2684. create table t1 (a1 int);
  2685. create table t2 (b1 int);
  2686. select * from t1 where a2 > any(select b1 from t2);
  2687. ERROR 42S22: Unknown column 'a2' in 'IN/ALL/ANY subquery'
  2688. select * from t1 where a1 > any(select b1 from t2);
  2689. a1
  2690. drop table t1,t2;
  2691. create table t1 (a integer, b integer);
  2692. select (select * from t1) = (select 1,2);
  2693. (select * from t1) = (select 1,2)
  2694. NULL
  2695. select (select 1,2) = (select * from t1);
  2696. (select 1,2) = (select * from t1)
  2697. NULL
  2698. select row(1,2) = ANY (select * from t1);
  2699. row(1,2) = ANY (select * from t1)
  2700. 0
  2701. select row(1,2) != ALL (select * from t1);
  2702. row(1,2) != ALL (select * from t1)
  2703. 1
  2704. drop table t1;
  2705. create table t1 (a integer, b integer);
  2706. select row(1,(2,2)) in (select * from t1 );
  2707. ERROR 21000: Operand should contain 2 column(s)
  2708. select row(1,(2,2)) = (select * from t1 );
  2709. ERROR 21000: Operand should contain 2 column(s)
  2710. select (select * from t1) = row(1,(2,2));
  2711. ERROR 21000: Operand should contain 1 column(s)
  2712. drop table t1;
  2713. create table t1 (a integer);
  2714. insert into t1 values (1);
  2715. select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx ;
  2716. ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
  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 as xx, 1 = ALL ( select 1 from t1 where 1 = xx );
  2720. xx 1 = ALL ( select 1 from t1 where 1 = xx )
  2721. 1 1
  2722. select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx;
  2723. ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
  2724. select 1 = ALL (select 1 from t1 where 1 = xx ), 1 as xx from DUAL;
  2725. ERROR 42S22: Reference 'xx' not supported (forward reference in item list)
  2726. drop table t1;
  2727. CREATE TABLE t1 (
  2728. categoryId int(11) NOT NULL,
  2729. courseId int(11) NOT NULL,
  2730. startDate datetime NOT NULL,
  2731. endDate datetime NOT NULL,
  2732. createDate datetime NOT NULL,
  2733. modifyDate timestamp NOT NULL,
  2734. attributes text NOT NULL
  2735. );
  2736. INSERT INTO t1 VALUES (1,41,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
  2737. (1,86,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
  2738. (1,87,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
  2739. (2,52,'2004-03-15','2004-10-01','2004-03-15','2004-09-17',''),
  2740. (2,53,'2004-03-16','2004-10-01','2004-03-16','2004-09-17',''),
  2741. (2,88,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
  2742. (2,89,'2004-08-16','2004-08-16','2004-08-16','2004-08-16',''),
  2743. (3,51,'2004-02-09','2010-01-01','2004-02-09','2004-02-09',''),
  2744. (5,12,'2004-02-18','2010-01-01','2004-02-18','2004-02-18','');
  2745. CREATE TABLE t2 (
  2746. userId int(11) NOT NULL,
  2747. courseId int(11) NOT NULL,
  2748. date datetime NOT NULL
  2749. );
  2750. INSERT INTO t2 VALUES (5141,71,'2003-11-18'),
  2751. (5141,72,'2003-11-25'),(5141,41,'2004-08-06'),
  2752. (5141,52,'2004-08-06'),(5141,53,'2004-08-06'),
  2753. (5141,12,'2004-08-06'),(5141,86,'2004-10-21'),
  2754. (5141,87,'2004-10-21'),(5141,88,'2004-10-21'),
  2755. (5141,89,'2004-10-22'),(5141,51,'2004-10-26');
  2756. CREATE TABLE t3 (
  2757. groupId int(11) NOT NULL,
  2758. parentId int(11) NOT NULL,
  2759. startDate datetime NOT NULL,
  2760. endDate datetime NOT NULL,
  2761. createDate datetime NOT NULL,
  2762. modifyDate timestamp NOT NULL,
  2763. ordering int(11)
  2764. );
  2765. INSERT INTO t3 VALUES (12,9,'1000-01-01','3999-12-31','2004-01-29','2004-01-29',NULL);
  2766. CREATE TABLE t4 (
  2767. id int(11) NOT NULL,
  2768. groupTypeId int(11) NOT NULL,
  2769. groupKey varchar(50) NOT NULL,
  2770. name text,
  2771. ordering int(11),
  2772. description text,
  2773. createDate datetime NOT NULL,
  2774. modifyDate timestamp NOT NULL
  2775. );
  2776. INSERT INTO t4 VALUES (9,5,'stationer','stationer',0,'Stationer','2004-01-29','2004-01-29'),
  2777. (12,5,'group2','group2',0,'group2','2004-01-29','2004-01-29');
  2778. CREATE TABLE t5 (
  2779. userId int(11) NOT NULL,
  2780. groupId int(11) NOT NULL,
  2781. createDate datetime NOT NULL,
  2782. modifyDate timestamp NOT NULL
  2783. );
  2784. INSERT INTO t5 VALUES (5141,12,'2004-08-06','2004-08-06');
  2785. select
  2786. count(distinct t2.userid) pass,
  2787. groupstuff.*,
  2788. count(t2.courseid) crse,
  2789. t1.categoryid,
  2790. t2.courseid,
  2791. date_format(date, '%b%y') as colhead
  2792. from t2
  2793. join t1 on t2.courseid=t1.courseid
  2794. join
  2795. (
  2796. select
  2797. t5.userid,
  2798. parentid,
  2799. parentgroup,
  2800. childid,
  2801. groupname,
  2802. grouptypeid
  2803. from t5
  2804. join
  2805. (
  2806. select t4.id as parentid,
  2807. t4.name as parentgroup,
  2808. t4.id as childid,
  2809. t4.name as groupname,
  2810. t4.grouptypeid
  2811. from t4
  2812. ) as gin on t5.groupid=gin.childid
  2813. ) as groupstuff on t2.userid = groupstuff.userid
  2814. group by
  2815. groupstuff.groupname, colhead , t2.courseid;
  2816. pass userid parentid parentgroup childid groupname grouptypeid crse categoryid courseid colhead
  2817. 1 5141 12 group2 12 group2 5 1 5 12 Aug04
  2818. 1 5141 12 group2 12 group2 5 1 1 41 Aug04
  2819. 1 5141 12 group2 12 group2 5 1 2 52 Aug04
  2820. 1 5141 12 group2 12 group2 5 1 2 53 Aug04
  2821. 1 5141 12 group2 12 group2 5 1 3 51 Oct04
  2822. 1 5141 12 group2 12 group2 5 1 1 86 Oct04
  2823. 1 5141 12 group2 12 group2 5 1 1 87 Oct04
  2824. 1 5141 12 group2 12 group2 5 1 2 88 Oct04
  2825. 1 5141 12 group2 12 group2 5 1 2 89 Oct04
  2826. drop table t1, t2, t3, t4, t5;
  2827. create table t1 (a int);
  2828. insert into t1 values (1), (2), (3);
  2829. SELECT 1 FROM t1 WHERE (SELECT 1) in (SELECT 1);
  2830. 1
  2831. 1
  2832. 1
  2833. 1
  2834. drop table t1;
  2835. create table t1 (a int);
  2836. create table t2 (a int);
  2837. insert into t1 values (1),(2);
  2838. insert into t2 values (0),(1),(2),(3);
  2839. select a from t2 where a in (select a from t1);
  2840. a
  2841. 1
  2842. 2
  2843. select a from t2 having a in (select a from t1);
  2844. a
  2845. 1
  2846. 2
  2847. prepare stmt1 from "select a from t2 where a in (select a from t1)";
  2848. execute stmt1;
  2849. a
  2850. 1
  2851. 2
  2852. execute stmt1;
  2853. a
  2854. 1
  2855. 2
  2856. deallocate prepare stmt1;
  2857. prepare stmt1 from "select a from t2 having a in (select a from t1)";
  2858. execute stmt1;
  2859. a
  2860. 1
  2861. 2
  2862. execute stmt1;
  2863. a
  2864. 1
  2865. 2
  2866. deallocate prepare stmt1;
  2867. drop table t1, t2;
  2868. create table t1 (a int, b int);
  2869. insert into t1 values (1,2);
  2870. select 1 = (select * from t1);
  2871. ERROR HY000: Illegal parameter data types int and row for operation '='
  2872. select (select * from t1) = 1;
  2873. ERROR HY000: Illegal parameter data types row and int for operation '='
  2874. select (1,2) = (select a from t1);
  2875. ERROR HY000: Illegal parameter data types row and int for operation '='
  2876. select (select a from t1) = (1,2);
  2877. ERROR HY000: Illegal parameter data types int and row for operation '='
  2878. select (1,2,3) = (select * from t1);
  2879. ERROR 21000: Operand should contain 3 column(s)
  2880. select (select * from t1) = (1,2,3);
  2881. ERROR 21000: Operand should contain 2 column(s)
  2882. drop table t1;
  2883. CREATE TABLE `t1` (
  2884. `itemid` bigint(20) unsigned NOT NULL auto_increment,
  2885. `sessionid` bigint(20) unsigned default NULL,
  2886. `time` int(10) unsigned NOT NULL default '0',
  2887. `type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT
  2888. NULL default '',
  2889. `data` text collate latin1_general_ci NOT NULL,
  2890. PRIMARY KEY (`itemid`)
  2891. ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
  2892. INSERT INTO `t1` VALUES (1, 1, 1, 'D', '');
  2893. CREATE TABLE `t2` (
  2894. `sessionid` bigint(20) unsigned NOT NULL auto_increment,
  2895. `pid` int(10) unsigned NOT NULL default '0',
  2896. `date` int(10) unsigned NOT NULL default '0',
  2897. `ip` varchar(15) collate latin1_general_ci NOT NULL default '',
  2898. PRIMARY KEY (`sessionid`)
  2899. ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
  2900. INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
  2901. 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;
  2902. ip count( e.itemid )
  2903. 10.10.10.1 1
  2904. drop tables t1,t2;
  2905. create table t1 (fld enum('0','1'));
  2906. insert into t1 values ('1');
  2907. select * from (select max(fld) from t1) as foo;
  2908. max(fld)
  2909. 1
  2910. drop table t1;
  2911. set @tmp11867_optimizer_switch=@@optimizer_switch;
  2912. set optimizer_switch='semijoin_with_cache=off';
  2913. CREATE TABLE t1 (one int, two int, flag char(1));
  2914. CREATE TABLE t2 (one int, two int, flag char(1));
  2915. INSERT INTO t1 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N');
  2916. INSERT INTO t2 VALUES(1,2,'Y'),(2,3,'Y'),(3,4,'Y'),(5,6,'N'),(7,8,'N');
  2917. SELECT * FROM t1
  2918. WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t2 WHERE flag = 'N');
  2919. one two flag
  2920. 5 6 N
  2921. 7 8 N
  2922. SELECT * FROM t1
  2923. WHERE ROW(one,two) IN (SELECT DISTINCT one,two FROM t1 WHERE flag = 'N');
  2924. one two flag
  2925. 5 6 N
  2926. 7 8 N
  2927. insert into t2 values (null,null,'N');
  2928. insert into t2 values (null,3,'0');
  2929. insert into t2 values (null,5,'0');
  2930. insert into t2 values (10,null,'0');
  2931. insert into t1 values (10,3,'0');
  2932. insert into t1 values (10,5,'0');
  2933. insert into t1 values (10,10,'0');
  2934. SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N') as 'test' from t1;
  2935. one two test
  2936. 1 2 NULL
  2937. 2 3 NULL
  2938. 3 4 NULL
  2939. 5 6 1
  2940. 7 8 1
  2941. 10 3 NULL
  2942. 10 5 NULL
  2943. 10 10 NULL
  2944. SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N');
  2945. one two
  2946. 5 6
  2947. 7 8
  2948. SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N' group by one,two) as 'test' from t1;
  2949. one two test
  2950. 1 2 NULL
  2951. 2 3 NULL
  2952. 3 4 NULL
  2953. 5 6 1
  2954. 7 8 1
  2955. 10 3 NULL
  2956. 10 5 NULL
  2957. 10 10 NULL
  2958. SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1;
  2959. one two test
  2960. 1 2 0
  2961. 2 3 NULL
  2962. 3 4 0
  2963. 5 6 0
  2964. 7 8 0
  2965. 10 3 NULL
  2966. 10 5 NULL
  2967. 10 10 NULL
  2968. SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0' group by one,two) as 'test' from t1;
  2969. one two test
  2970. 1 2 0
  2971. 2 3 NULL
  2972. 3 4 0
  2973. 5 6 0
  2974. 7 8 0
  2975. 10 3 NULL
  2976. 10 5 NULL
  2977. 10 10 NULL
  2978. explain extended SELECT one,two,ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = '0') as 'test' from t1;
  2979. id select_type table type possible_keys key key_len ref rows filtered Extra
  2980. 1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00
  2981. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 9 100.00 Using where
  2982. Warnings:
  2983. Note 1003 /* select#1 */ select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),(`test`.`t1`.`one`,`test`.`t1`.`two`) in ( <materialize> (/* select#2 */ select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where `test`.`t2`.`flag` = '0' ), <primary_index_lookup>(`test`.`t1`.`one` in <temporary table> on distinct_key where `test`.`t1`.`one` = `<subquery2>`.`one` and `test`.`t1`.`two` = `<subquery2>`.`two`)))) AS `test` from `test`.`t1`
  2984. explain extended SELECT one,two from t1 where ROW(one,two) IN (SELECT one,two FROM t2 WHERE flag = 'N');
  2985. id select_type table type possible_keys key key_len ref rows filtered Extra
  2986. 1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00 Using where
  2987. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 9 100.00 Using where
  2988. Warnings:
  2989. Note 1003 /* select#1 */ select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two` from `test`.`t1` where <expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),(`test`.`t1`.`one`,`test`.`t1`.`two`) in ( <materialize> (/* select#2 */ select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where `test`.`t2`.`flag` = 'N' ), <primary_index_lookup>(`test`.`t1`.`one` in <temporary table> on distinct_key where `test`.`t1`.`one` = `<subquery2>`.`one` and `test`.`t1`.`two` = `<subquery2>`.`two`))))
  2990. 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;
  2991. id select_type table type possible_keys key key_len ref rows filtered Extra
  2992. 1 PRIMARY t1 ALL NULL NULL NULL NULL 8 100.00
  2993. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 9 100.00 Using where
  2994. Warnings:
  2995. Note 1003 /* select#1 */ select `test`.`t1`.`one` AS `one`,`test`.`t1`.`two` AS `two`,<expr_cache><`test`.`t1`.`one`,`test`.`t1`.`two`>(<in_optimizer>((`test`.`t1`.`one`,`test`.`t1`.`two`),(`test`.`t1`.`one`,`test`.`t1`.`two`) in ( <materialize> (/* select#2 */ select `test`.`t2`.`one`,`test`.`t2`.`two` from `test`.`t2` where `test`.`t2`.`flag` = '0' ), <primary_index_lookup>(`test`.`t1`.`one` in <temporary table> on distinct_key where `test`.`t1`.`one` = `<subquery2>`.`one` and `test`.`t1`.`two` = `<subquery2>`.`two`)))) AS `test` from `test`.`t1`
  2996. DROP TABLE t1,t2;
  2997. set optimizer_switch=@tmp11867_optimizer_switch;
  2998. CREATE TABLE t1 (a char(5), b char(5));
  2999. INSERT INTO t1 VALUES (NULL,'aaa'), ('aaa','aaa');
  3000. SELECT * FROM t1 WHERE (a,b) IN (('aaa','aaa'), ('aaa','bbb'));
  3001. a b
  3002. aaa aaa
  3003. DROP TABLE t1;
  3004. CREATE TABLE t1 (a int);
  3005. CREATE TABLE t2 (a int, b int);
  3006. CREATE TABLE t3 (b int NOT NULL);
  3007. INSERT INTO t1 VALUES (1), (2), (3), (4);
  3008. INSERT INTO t2 VALUES (1,10), (3,30);
  3009. SELECT * FROM t2 LEFT JOIN t3 ON t2.b=t3.b
  3010. WHERE t3.b IS NOT NULL OR t2.a > 10;
  3011. a b b
  3012. SELECT * FROM t1
  3013. WHERE t1.a NOT IN (SELECT a FROM t2 LEFT JOIN t3 ON t2.b=t3.b
  3014. WHERE t3.b IS NOT NULL OR t2.a > 10);
  3015. a
  3016. 1
  3017. 2
  3018. 3
  3019. 4
  3020. DROP TABLE t1,t2,t3;
  3021. CREATE TABLE t1 (f1 INT);
  3022. CREATE TABLE t2 (f2 INT);
  3023. INSERT INTO t1 VALUES (1);
  3024. SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2);
  3025. f1
  3026. 1
  3027. SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE 1=0);
  3028. f1
  3029. 1
  3030. INSERT INTO t2 VALUES (1);
  3031. INSERT INTO t2 VALUES (2);
  3032. SELECT * FROM t1 WHERE f1 > ALL (SELECT f2 FROM t2 WHERE f2=0);
  3033. f1
  3034. 1
  3035. DROP TABLE t1, t2;
  3036. select 1 from dual where 1 < any (select 2);
  3037. 1
  3038. 1
  3039. select 1 from dual where 1 < all (select 2);
  3040. 1
  3041. 1
  3042. select 1 from dual where 2 > any (select 1);
  3043. 1
  3044. 1
  3045. select 1 from dual where 2 > all (select 1);
  3046. 1
  3047. 1
  3048. select 1 from dual where 1 < any (select 2 from dual);
  3049. 1
  3050. 1
  3051. select 1 from dual where 1 < all (select 2 from dual where 1!=1);
  3052. 1
  3053. 1
  3054. create table t1 (s1 char);
  3055. insert into t1 values (1),(2);
  3056. select * from t1 where (s1 < any (select s1 from t1));
  3057. s1
  3058. 1
  3059. select * from t1 where not (s1 < any (select s1 from t1));
  3060. s1
  3061. 2
  3062. select * from t1 where (s1 < ALL (select s1+1 from t1));
  3063. s1
  3064. 1
  3065. select * from t1 where not(s1 < ALL (select s1+1 from t1));
  3066. s1
  3067. 2
  3068. select * from t1 where (s1+1 = ANY (select s1 from t1));
  3069. s1
  3070. 1
  3071. select * from t1 where NOT(s1+1 = ANY (select s1 from t1));
  3072. s1
  3073. 2
  3074. select * from t1 where (s1 = ALL (select s1/s1 from t1));
  3075. s1
  3076. 1
  3077. select * from t1 where NOT(s1 = ALL (select s1/s1 from t1));
  3078. s1
  3079. 2
  3080. drop table t1;
  3081. create table t1 (
  3082. retailerID varchar(8) NOT NULL,
  3083. statusID int(10) unsigned NOT NULL,
  3084. changed datetime NOT NULL,
  3085. UNIQUE KEY retailerID (retailerID, statusID, changed)
  3086. );
  3087. INSERT INTO t1 VALUES("0026", "1", "2005-12-06 12:18:56");
  3088. INSERT INTO t1 VALUES("0026", "2", "2006-01-06 12:25:53");
  3089. INSERT INTO t1 VALUES("0037", "1", "2005-12-06 12:18:56");
  3090. INSERT INTO t1 VALUES("0037", "2", "2006-01-06 12:25:53");
  3091. INSERT INTO t1 VALUES("0048", "1", "2006-01-06 12:37:50");
  3092. INSERT INTO t1 VALUES("0059", "1", "2006-01-06 12:37:50");
  3093. select * from t1 r1
  3094. where (r1.retailerID,(r1.changed)) in
  3095. (SELECT r2.retailerId,(max(changed)) from t1 r2
  3096. group by r2.retailerId);
  3097. retailerID statusID changed
  3098. 0026 2 2006-01-06 12:25:53
  3099. 0037 2 2006-01-06 12:25:53
  3100. 0048 1 2006-01-06 12:37:50
  3101. 0059 1 2006-01-06 12:37:50
  3102. drop table t1;
  3103. create table t1(a int, primary key (a));
  3104. insert into t1 values (10);
  3105. create table t2 (a int primary key, b varchar(32), c int, unique key b(c, b));
  3106. insert into t2(a, c, b) values (1,10,'359'), (2,10,'35988'), (3,10,'35989');
  3107. insert into t2(a, c, b) values (4,10,'360'), (5,10,'35998'), (6,10,'35999');
  3108. analyze table t1;
  3109. Table Op Msg_type Msg_text
  3110. test.t1 analyze status Engine-independent statistics collected
  3111. test.t1 analyze status OK
  3112. explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
  3113. ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
  3114. ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
  3115. id select_type table type possible_keys key key_len ref rows Extra
  3116. 1 PRIMARY t1 system PRIMARY NULL NULL NULL 1
  3117. 1 PRIMARY r const PRIMARY PRIMARY 4 const 1
  3118. 2 SUBQUERY t2 range b b 40 NULL 3 Using where
  3119. SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
  3120. ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
  3121. ORDER BY t2.c DESC, t2.b DESC LIMIT 1) WHERE t1.a = 10;
  3122. a a b
  3123. 10 3 35989
  3124. explain SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
  3125. ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
  3126. ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
  3127. id select_type table type possible_keys key key_len ref rows Extra
  3128. 1 PRIMARY t1 system PRIMARY NULL NULL NULL 1
  3129. 1 PRIMARY r const PRIMARY PRIMARY 4 const 1
  3130. 2 SUBQUERY t2 range b b 40 NULL 3 Using index condition
  3131. SELECT sql_no_cache t1.a, r.a, r.b FROM t1 LEFT JOIN t2 r
  3132. ON r.a = (SELECT t2.a FROM t2 WHERE t2.c = t1.a AND t2.b <= '359899'
  3133. ORDER BY t2.c, t2.b LIMIT 1) WHERE t1.a = 10;
  3134. a a b
  3135. 10 1 359
  3136. drop table t1,t2;
  3137. CREATE TABLE t1 (
  3138. field1 int NOT NULL,
  3139. field2 int NOT NULL,
  3140. field3 int NOT NULL,
  3141. PRIMARY KEY (field1,field2,field3)
  3142. );
  3143. CREATE TABLE t2 (
  3144. fieldA int NOT NULL,
  3145. fieldB int NOT NULL,
  3146. PRIMARY KEY (fieldA,fieldB)
  3147. );
  3148. INSERT INTO t1 VALUES
  3149. (1,1,1), (1,1,2), (1,2,1), (1,2,2), (1,2,3), (1,3,1);
  3150. INSERT INTO t2 VALUES (1,1), (1,2), (1,3);
  3151. SELECT field1, field2, COUNT(*)
  3152. FROM t1 GROUP BY field1, field2;
  3153. field1 field2 COUNT(*)
  3154. 1 1 2
  3155. 1 2 3
  3156. 1 3 1
  3157. SELECT field1, field2
  3158. FROM t1
  3159. GROUP BY field1, field2
  3160. HAVING COUNT(*) >= ALL (SELECT fieldB
  3161. FROM t2 WHERE fieldA = field1);
  3162. field1 field2
  3163. 1 2
  3164. SELECT field1, field2
  3165. FROM t1
  3166. GROUP BY field1, field2
  3167. HAVING COUNT(*) < ANY (SELECT fieldB
  3168. FROM t2 WHERE fieldA = field1);
  3169. field1 field2
  3170. 1 1
  3171. 1 3
  3172. DROP TABLE t1, t2;
  3173. CREATE TABLE t1(a int, INDEX (a));
  3174. INSERT INTO t1 VALUES (1), (3), (5), (7);
  3175. INSERT INTO t1 VALUES (NULL);
  3176. CREATE TABLE t2(a int);
  3177. INSERT INTO t2 VALUES (1),(2),(3);
  3178. EXPLAIN SELECT a, a IN (SELECT a FROM t1) FROM t2;
  3179. id select_type table type possible_keys key key_len ref rows Extra
  3180. 1 PRIMARY t2 ALL NULL NULL NULL NULL 3
  3181. 2 MATERIALIZED t1 index a a 5 NULL 5 Using index
  3182. SELECT a, a IN (SELECT a FROM t1) FROM t2;
  3183. a a IN (SELECT a FROM t1)
  3184. 1 1
  3185. 2 NULL
  3186. 3 1
  3187. DROP TABLE t1,t2;
  3188. CREATE TABLE t1 (a DATETIME);
  3189. INSERT INTO t1 VALUES ('1998-09-23'), ('2003-03-25');
  3190. CREATE TABLE t2 AS SELECT
  3191. (SELECT a FROM t1 WHERE a < '2000-01-01') AS sub_a
  3192. FROM t1 WHERE a > '2000-01-01';
  3193. SHOW CREATE TABLE t2;
  3194. Table Create Table
  3195. t2 CREATE TABLE `t2` (
  3196. `sub_a` datetime DEFAULT NULL
  3197. ) ENGINE=MyISAM DEFAULT CHARSET=latin1
  3198. CREATE TABLE t3 AS (SELECT a FROM t1 WHERE a < '2000-01-01') UNION (SELECT a FROM t1 WHERE a > '2000-01-01');
  3199. SHOW CREATE TABLE t3;
  3200. Table Create Table
  3201. t3 CREATE TABLE `t3` (
  3202. `a` datetime DEFAULT NULL
  3203. ) ENGINE=MyISAM DEFAULT CHARSET=latin1
  3204. DROP TABLE t1,t2,t3;
  3205. CREATE TABLE t1 (a int);
  3206. INSERT INTO t1 VALUES (1), (2);
  3207. SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) > 0;
  3208. a
  3209. SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL;
  3210. a
  3211. 1
  3212. 2
  3213. EXPLAIN SELECT a FROM t1 WHERE (SELECT 1 FROM DUAL WHERE 1=0) IS NULL;
  3214. id select_type table type possible_keys key key_len ref rows Extra
  3215. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2
  3216. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  3217. DROP TABLE t1;
  3218. CREATE TABLE t1 (a int);
  3219. INSERT INTO t1 VALUES (2), (4), (1), (3);
  3220. CREATE TABLE t2 (b int, c int);
  3221. INSERT INTO t2 VALUES
  3222. (2,1), (1,3), (2,1), (4,4), (2,2), (1,4);
  3223. SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2 );
  3224. a
  3225. 2
  3226. 4
  3227. 1
  3228. 3
  3229. SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1);
  3230. ERROR 21000: Subquery returns more than 1 row
  3231. SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 2), a;
  3232. a
  3233. 1
  3234. 2
  3235. 3
  3236. 4
  3237. SELECT a FROM t1 ORDER BY (SELECT c FROM t2 WHERE b > 1), a;
  3238. ERROR 21000: Subquery returns more than 1 row
  3239. SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 2);
  3240. b MAX(c)
  3241. 1 4
  3242. 2 2
  3243. 4 4
  3244. SELECT b, MAX(c) FROM t2 GROUP BY b, (SELECT c FROM t2 WHERE b > 1);
  3245. ERROR 21000: Subquery returns more than 1 row
  3246. SELECT a FROM t1 GROUP BY a
  3247. HAVING IFNULL((SELECT b FROM t2 WHERE b > 2),
  3248. (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
  3249. a
  3250. 1
  3251. 2
  3252. 3
  3253. 4
  3254. SELECT a FROM t1 GROUP BY a
  3255. HAVING IFNULL((SELECT b FROM t2 WHERE b > 1),
  3256. (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
  3257. ERROR 21000: Subquery returns more than 1 row
  3258. SELECT a FROM t1 GROUP BY a
  3259. HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
  3260. (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b)) > 3;
  3261. a
  3262. 4
  3263. SELECT a FROM t1 GROUP BY a
  3264. HAVING IFNULL((SELECT b FROM t2 WHERE b > 4),
  3265. (SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b)) > 3;
  3266. ERROR 21000: Subquery returns more than 1 row
  3267. SELECT a FROM t1
  3268. ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 2),
  3269. (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b));
  3270. a
  3271. 1
  3272. 2
  3273. 3
  3274. 4
  3275. SELECT a FROM t1
  3276. ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 1),
  3277. (SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b));
  3278. ERROR 21000: Subquery returns more than 1 row
  3279. SELECT a FROM t1
  3280. ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
  3281. (SELECT c FROM t2 WHERE c=a AND b > 2 ORDER BY b));
  3282. a
  3283. 1
  3284. 2
  3285. 3
  3286. 4
  3287. SELECT a FROM t1
  3288. ORDER BY IFNULL((SELECT b FROM t2 WHERE b > 4),
  3289. (SELECT c FROM t2 WHERE c=a AND b > 1 ORDER BY b));
  3290. ERROR 21000: Subquery returns more than 1 row
  3291. DROP TABLE t1,t2;
  3292. create table t1 (df decimal(5,1));
  3293. insert into t1 values(1.1);
  3294. insert into t1 values(2.2);
  3295. select * from t1 where df <= all (select avg(df) from t1 group by df);
  3296. df
  3297. 1.1
  3298. select * from t1 where df >= all (select avg(df) from t1 group by df);
  3299. df
  3300. 2.2
  3301. drop table t1;
  3302. create table t1 (df decimal(5,1));
  3303. insert into t1 values(1.1);
  3304. select 1.1 * exists(select * from t1);
  3305. 1.1 * exists(select * from t1)
  3306. 1.1
  3307. drop table t1;
  3308. CREATE TABLE t1 (
  3309. grp int(11) default NULL,
  3310. a decimal(10,2) default NULL);
  3311. insert into t1 values (1, 1), (2, 2), (2, 3), (3, 4), (3, 5), (3, 6), (NULL, NULL);
  3312. select * from t1;
  3313. grp a
  3314. 1 1.00
  3315. 2 2.00
  3316. 2 3.00
  3317. 3 4.00
  3318. 3 5.00
  3319. 3 6.00
  3320. NULL NULL
  3321. select min(a) from t1 group by grp;
  3322. min(a)
  3323. NULL
  3324. 1.00
  3325. 2.00
  3326. 4.00
  3327. drop table t1;
  3328. CREATE table t1 ( c1 integer );
  3329. INSERT INTO t1 VALUES ( 1 );
  3330. INSERT INTO t1 VALUES ( 2 );
  3331. INSERT INTO t1 VALUES ( 3 );
  3332. CREATE TABLE t2 ( c2 integer );
  3333. INSERT INTO t2 VALUES ( 1 );
  3334. INSERT INTO t2 VALUES ( 4 );
  3335. INSERT INTO t2 VALUES ( 5 );
  3336. SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2 WHERE c2 IN (1);
  3337. c1 c2
  3338. 1 1
  3339. SELECT * FROM t1 LEFT JOIN t2 ON c1 = c2
  3340. WHERE c2 IN ( SELECT c2 FROM t2 WHERE c2 IN ( 1 ) );
  3341. c1 c2
  3342. 1 1
  3343. DROP TABLE t1,t2;
  3344. CREATE TABLE t1 ( c1 integer );
  3345. INSERT INTO t1 VALUES ( 1 );
  3346. INSERT INTO t1 VALUES ( 2 );
  3347. INSERT INTO t1 VALUES ( 3 );
  3348. INSERT INTO t1 VALUES ( 6 );
  3349. CREATE TABLE t2 ( c2 integer );
  3350. INSERT INTO t2 VALUES ( 1 );
  3351. INSERT INTO t2 VALUES ( 4 );
  3352. INSERT INTO t2 VALUES ( 5 );
  3353. INSERT INTO t2 VALUES ( 6 );
  3354. CREATE TABLE t3 ( c3 integer );
  3355. INSERT INTO t3 VALUES ( 7 );
  3356. INSERT INTO t3 VALUES ( 8 );
  3357. SELECT c1,c2 FROM t1 LEFT JOIN t2 ON c1 = c2
  3358. WHERE EXISTS (SELECT c3 FROM t3 WHERE c2 IS NULL );
  3359. c1 c2
  3360. 2 NULL
  3361. 3 NULL
  3362. DROP TABLE t1,t2,t3;
  3363. CREATE TABLE `t1` (
  3364. `itemid` bigint(20) unsigned NOT NULL auto_increment,
  3365. `sessionid` bigint(20) unsigned default NULL,
  3366. `time` int(10) unsigned NOT NULL default '0',
  3367. `type` set('A','D','E','F','G','I','L','N','U') collate latin1_general_ci NOT
  3368. NULL default '',
  3369. `data` text collate latin1_general_ci NOT NULL,
  3370. PRIMARY KEY (`itemid`)
  3371. ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
  3372. INSERT INTO `t1` VALUES (1, 1, 1, 'D', '');
  3373. CREATE TABLE `t2` (
  3374. `sessionid` bigint(20) unsigned NOT NULL auto_increment,
  3375. `pid` int(10) unsigned NOT NULL default '0',
  3376. `date` int(10) unsigned NOT NULL default '0',
  3377. `ip` varchar(15) collate latin1_general_ci NOT NULL default '',
  3378. PRIMARY KEY (`sessionid`)
  3379. ) DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
  3380. INSERT INTO `t2` VALUES (1, 1, 1, '10.10.10.1');
  3381. 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;
  3382. ip count( e.itemid )
  3383. 10.10.10.1 1
  3384. drop tables t1,t2;
  3385. CREATE TABLE t1 (EMPNUM CHAR(3));
  3386. CREATE TABLE t2 (EMPNUM CHAR(3) );
  3387. INSERT INTO t1 VALUES ('E1'),('E2');
  3388. INSERT INTO t2 VALUES ('E1');
  3389. DELETE FROM t1
  3390. WHERE t1.EMPNUM NOT IN
  3391. (SELECT t2.EMPNUM
  3392. FROM t2
  3393. WHERE t1.EMPNUM = t2.EMPNUM);
  3394. select * from t1;
  3395. EMPNUM
  3396. E1
  3397. DROP TABLE t1,t2;
  3398. CREATE TABLE t1(select_id BIGINT, values_id BIGINT);
  3399. INSERT INTO t1 VALUES (1, 1);
  3400. CREATE TABLE t2 (select_id BIGINT, values_id BIGINT,
  3401. PRIMARY KEY(select_id,values_id));
  3402. INSERT INTO t2 VALUES (0, 1), (0, 2), (0, 3), (1, 5);
  3403. SELECT values_id FROM t1
  3404. WHERE values_id IN (SELECT values_id FROM t2
  3405. WHERE select_id IN (1, 0));
  3406. values_id
  3407. 1
  3408. SELECT values_id FROM t1
  3409. WHERE values_id IN (SELECT values_id FROM t2
  3410. WHERE select_id BETWEEN 0 AND 1);
  3411. values_id
  3412. 1
  3413. SELECT values_id FROM t1
  3414. WHERE values_id IN (SELECT values_id FROM t2
  3415. WHERE select_id = 0 OR select_id = 1);
  3416. values_id
  3417. 1
  3418. DROP TABLE t1, t2;
  3419. create table t1 (fld enum('0','1'));
  3420. insert into t1 values ('1');
  3421. select * from (select max(fld) from t1) as foo;
  3422. max(fld)
  3423. 1
  3424. drop table t1;
  3425. CREATE TABLE t1 (a int, b int);
  3426. CREATE TABLE t2 (c int, d int);
  3427. CREATE TABLE t3 (e int);
  3428. INSERT INTO t1 VALUES
  3429. (1,10), (2,10), (1,20), (2,20), (3,20), (2,30), (4,40);
  3430. INSERT INTO t2 VALUES
  3431. (2,10), (2,20), (4,10), (5,10), (3,20), (2,40);
  3432. INSERT INTO t3 VALUES (10), (30), (10), (20) ;
  3433. SELECT a, MAX(b), MIN(b) FROM t1 GROUP BY a;
  3434. a MAX(b) MIN(b)
  3435. 1 20 10
  3436. 2 30 10
  3437. 3 20 20
  3438. 4 40 40
  3439. SELECT * FROM t2;
  3440. c d
  3441. 2 10
  3442. 2 20
  3443. 4 10
  3444. 5 10
  3445. 3 20
  3446. 2 40
  3447. SELECT * FROM t3;
  3448. e
  3449. 10
  3450. 30
  3451. 10
  3452. 20
  3453. SELECT a FROM t1 GROUP BY a
  3454. HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>20);
  3455. a
  3456. 2
  3457. 4
  3458. SELECT a FROM t1 GROUP BY a
  3459. HAVING a IN (SELECT c FROM t2 WHERE MAX(b)<d);
  3460. a
  3461. 2
  3462. SELECT a FROM t1 GROUP BY a
  3463. HAVING a IN (SELECT c FROM t2 WHERE MAX(b)>d);
  3464. a
  3465. 2
  3466. 4
  3467. SELECT a FROM t1 GROUP BY a
  3468. HAVING a IN (SELECT c FROM t2
  3469. WHERE d >= SOME(SELECT e FROM t3 WHERE MAX(b)=e));
  3470. a
  3471. 2
  3472. 3
  3473. SELECT a FROM t1 GROUP BY a
  3474. HAVING a IN (SELECT c FROM t2
  3475. WHERE EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
  3476. a
  3477. 2
  3478. 3
  3479. SELECT a FROM t1 GROUP BY a
  3480. HAVING a IN (SELECT c FROM t2
  3481. WHERE d > SOME(SELECT e FROM t3 WHERE MAX(b)=e));
  3482. a
  3483. 2
  3484. SELECT a FROM t1 GROUP BY a
  3485. HAVING a IN (SELECT c FROM t2
  3486. WHERE EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e < d));
  3487. a
  3488. 2
  3489. SELECT a FROM t1 GROUP BY a
  3490. HAVING a IN (SELECT c FROM t2
  3491. WHERE MIN(b) < d AND
  3492. EXISTS(SELECT e FROM t3 WHERE MAX(b)=e AND e <= d));
  3493. a
  3494. 2
  3495. SELECT a, SUM(a) FROM t1 GROUP BY a;
  3496. a SUM(a)
  3497. 1 2
  3498. 2 6
  3499. 3 3
  3500. 4 4
  3501. SELECT a FROM t1
  3502. WHERE EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c) GROUP BY a;
  3503. a
  3504. 3
  3505. 4
  3506. SELECT a FROM t1 GROUP BY a
  3507. HAVING EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) = c);
  3508. a
  3509. 1
  3510. 3
  3511. 4
  3512. SELECT a FROM t1
  3513. WHERE a < 3 AND
  3514. EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c) GROUP BY a;
  3515. a
  3516. 1
  3517. 2
  3518. SELECT a FROM t1
  3519. WHERE a < 3 AND
  3520. EXISTS(SELECT c FROM t2 GROUP BY c HAVING SUM(a) != c);
  3521. a
  3522. 1
  3523. 2
  3524. 1
  3525. 2
  3526. 2
  3527. SELECT t1.a FROM t1 GROUP BY t1.a
  3528. HAVING t1.a < ALL(SELECT t2.c FROM t2 GROUP BY t2.c
  3529. HAVING EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
  3530. HAVING SUM(t1.a+t2.c) < t3.e/4));
  3531. a
  3532. 1
  3533. 2
  3534. SELECT t1.a FROM t1 GROUP BY t1.a
  3535. HAVING t1.a > ALL(SELECT t2.c FROM t2
  3536. WHERE EXISTS(SELECT t3.e FROM t3 GROUP BY t3.e
  3537. HAVING SUM(t1.a+t2.c) < t3.e/4));
  3538. a
  3539. 4
  3540. SELECT t1.a FROM t1 GROUP BY t1.a
  3541. HAVING t1.a > ALL(SELECT t2.c FROM t2
  3542. WHERE EXISTS(SELECT t3.e FROM t3
  3543. WHERE SUM(t1.a+t2.c) < t3.e/4));
  3544. ERROR HY000: Invalid use of group function
  3545. SELECT t1.a from t1 GROUP BY t1.a HAVING AVG(SUM(t1.b)) > 20;
  3546. ERROR HY000: Invalid use of group function
  3547. SELECT t1.a FROM t1 GROUP BY t1.a
  3548. HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
  3549. HAVING AVG(t2.c+SUM(t1.b)) > 20);
  3550. a
  3551. 2
  3552. 3
  3553. 4
  3554. SELECT t1.a FROM t1 GROUP BY t1.a
  3555. HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
  3556. HAVING AVG(SUM(t1.b)) > 20);
  3557. a
  3558. 2
  3559. 4
  3560. SELECT t1.a, SUM(b) AS sum FROM t1 GROUP BY t1.a
  3561. HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
  3562. HAVING t2.c+sum > 20);
  3563. a sum
  3564. 2 60
  3565. 3 20
  3566. 4 40
  3567. DROP TABLE t1,t2,t3;
  3568. CREATE TABLE t1 (a varchar(5), b varchar(10));
  3569. INSERT INTO t1 VALUES
  3570. ('AAA', 5), ('BBB', 4), ('BBB', 1), ('CCC', 2),
  3571. ('CCC', 7), ('AAA', 2), ('AAA', 4), ('BBB', 3), ('AAA', 8);
  3572. SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
  3573. a b
  3574. BBB 4
  3575. CCC 7
  3576. AAA 8
  3577. EXPLAIN
  3578. SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
  3579. id select_type table type possible_keys key key_len ref rows Extra
  3580. 1 PRIMARY t1 ALL NULL NULL NULL NULL 9 Using where
  3581. 2 MATERIALIZED t1 ALL NULL NULL NULL NULL 9 Using temporary
  3582. ALTER TABLE t1 ADD INDEX(a);
  3583. SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
  3584. a b
  3585. BBB 4
  3586. CCC 7
  3587. AAA 8
  3588. EXPLAIN
  3589. SELECT * FROM t1 WHERE (a,b) = ANY (SELECT a, max(b) FROM t1 GROUP BY a);
  3590. id select_type table type possible_keys key key_len ref rows Extra
  3591. 1 PRIMARY t1 ALL NULL NULL NULL NULL 9 Using where
  3592. 2 MATERIALIZED t1 ALL NULL NULL NULL NULL 9 Using temporary
  3593. DROP TABLE t1;
  3594. create table t1( f1 int,f2 int);
  3595. insert into t1 values (1,1),(2,2);
  3596. 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';
  3597. t
  3598. crash1
  3599. crash1
  3600. drop table t1;
  3601. create table t1 (c int, key(c));
  3602. insert into t1 values (1142477582), (1142455969);
  3603. create table t2 (a int, b int);
  3604. insert into t2 values (2, 1), (1, 0);
  3605. delete from t1 where c <= 1140006215 and (select b from t2 where a = 2) = 1;
  3606. drop table t1, t2;
  3607. CREATE TABLE t1 (a INT);
  3608. CREATE VIEW v1 AS SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1);
  3609. ERROR 42S22: Unknown column 'no_such_column' in 'IN/ALL/ANY subquery'
  3610. CREATE VIEW v2 AS SELECT * FROM t1 WHERE no_such_column = (SELECT 1);
  3611. ERROR 42S22: Unknown column 'no_such_column' in 'where clause'
  3612. SELECT * FROM t1 WHERE no_such_column = ANY (SELECT 1);
  3613. ERROR 42S22: Unknown column 'no_such_column' in 'IN/ALL/ANY subquery'
  3614. DROP TABLE t1;
  3615. create table t1 (i int, j bigint);
  3616. insert into t1 values (1, 2), (2, 2), (3, 2);
  3617. select * from (select min(i) from t1 where j=(select * from (select min(j) from t1) t2)) t3;
  3618. min(i)
  3619. 1
  3620. drop table t1;
  3621. CREATE TABLE t1 (i BIGINT UNSIGNED);
  3622. INSERT INTO t1 VALUES (10000000000000000000);
  3623. INSERT INTO t1 VALUES (1);
  3624. CREATE TABLE t2 (i BIGINT UNSIGNED);
  3625. INSERT INTO t2 VALUES (10000000000000000000);
  3626. INSERT INTO t2 VALUES (1);
  3627. /* simple test */
  3628. SELECT t1.i FROM t1 JOIN t2 ON t1.i = t2.i;
  3629. i
  3630. 10000000000000000000
  3631. 1
  3632. /* subquery test */
  3633. SELECT t1.i FROM t1 WHERE t1.i = (SELECT MAX(i) FROM t2);
  3634. i
  3635. 10000000000000000000
  3636. /* subquery test with cast*/
  3637. SELECT t1.i FROM t1 WHERE t1.i = CAST((SELECT MAX(i) FROM t2) AS UNSIGNED);
  3638. i
  3639. 10000000000000000000
  3640. DROP TABLE t1;
  3641. DROP TABLE t2;
  3642. CREATE TABLE t1 (
  3643. id bigint(20) unsigned NOT NULL auto_increment,
  3644. name varchar(255) NOT NULL,
  3645. PRIMARY KEY (id)
  3646. );
  3647. INSERT INTO t1 VALUES
  3648. (1, 'Balazs'), (2, 'Joe'), (3, 'Frank');
  3649. CREATE TABLE t2 (
  3650. id bigint(20) unsigned NOT NULL auto_increment,
  3651. mid bigint(20) unsigned NOT NULL,
  3652. date date NOT NULL,
  3653. PRIMARY KEY (id)
  3654. );
  3655. INSERT INTO t2 VALUES
  3656. (1, 1, '2006-03-30'), (2, 2, '2006-04-06'), (3, 3, '2006-04-13'),
  3657. (4, 2, '2006-04-20'), (5, 1, '2006-05-01');
  3658. SELECT *,
  3659. (SELECT date FROM t2 WHERE mid = t1.id
  3660. ORDER BY date DESC LIMIT 0, 1) AS date_last,
  3661. (SELECT date FROM t2 WHERE mid = t1.id
  3662. ORDER BY date DESC LIMIT 3, 1) AS date_next_to_last
  3663. FROM t1;
  3664. id name date_last date_next_to_last
  3665. 1 Balazs 2006-05-01 NULL
  3666. 2 Joe 2006-04-20 NULL
  3667. 3 Frank 2006-04-13 NULL
  3668. SELECT *,
  3669. (SELECT COUNT(*) FROM t2 WHERE mid = t1.id
  3670. ORDER BY date DESC LIMIT 1, 1) AS date_count
  3671. FROM t1;
  3672. id name date_count
  3673. 1 Balazs NULL
  3674. 2 Joe NULL
  3675. 3 Frank NULL
  3676. SELECT *,
  3677. (SELECT date FROM t2 WHERE mid = t1.id
  3678. ORDER BY date DESC LIMIT 0, 1) AS date_last,
  3679. (SELECT date FROM t2 WHERE mid = t1.id
  3680. ORDER BY date DESC LIMIT 1, 1) AS date_next_to_last
  3681. FROM t1;
  3682. id name date_last date_next_to_last
  3683. 1 Balazs 2006-05-01 2006-03-30
  3684. 2 Joe 2006-04-20 2006-04-06
  3685. 3 Frank 2006-04-13 NULL
  3686. DROP TABLE t1,t2;
  3687. CREATE TABLE t1 (
  3688. i1 int(11) NOT NULL default '0',
  3689. i2 int(11) NOT NULL default '0',
  3690. t datetime NOT NULL default '0000-00-00 00:00:00',
  3691. PRIMARY KEY (i1,i2,t)
  3692. );
  3693. INSERT INTO t1 VALUES
  3694. (24,1,'2005-03-03 16:31:31'),(24,1,'2005-05-27 12:40:07'),
  3695. (24,1,'2005-05-27 12:40:08'),(24,1,'2005-05-27 12:40:10'),
  3696. (24,1,'2005-05-27 12:40:25'),(24,1,'2005-05-27 12:40:30'),
  3697. (24,2,'2005-03-03 13:43:05'),(24,2,'2005-03-03 16:23:31'),
  3698. (24,2,'2005-03-03 16:31:30'),(24,2,'2005-05-27 12:37:02'),
  3699. (24,2,'2005-05-27 12:40:06');
  3700. CREATE TABLE t2 (
  3701. i1 int(11) NOT NULL default '0',
  3702. i2 int(11) NOT NULL default '0',
  3703. t datetime default NULL,
  3704. PRIMARY KEY (i1)
  3705. );
  3706. INSERT INTO t2 VALUES (24,1,'2006-06-20 12:29:40');
  3707. EXPLAIN
  3708. SELECT * FROM t1,t2
  3709. WHERE t1.t = (SELECT t1.t FROM t1
  3710. WHERE t1.t < t2.t AND t1.i2=1 AND t2.i1=t1.i1
  3711. ORDER BY t1.t DESC LIMIT 1);
  3712. id select_type table type possible_keys key key_len ref rows Extra
  3713. 1 PRIMARY t2 system NULL NULL NULL NULL 1
  3714. 1 PRIMARY t1 index NULL PRIMARY 13 NULL 11 Using where; Using index
  3715. 2 SUBQUERY t1 range PRIMARY PRIMARY 13 NULL 6 Using where; Using index
  3716. SELECT * FROM t1,t2
  3717. WHERE t1.t = (SELECT t1.t FROM t1
  3718. WHERE t1.t < t2.t AND t1.i2=1 AND t2.i1=t1.i1
  3719. ORDER BY t1.t DESC LIMIT 1);
  3720. i1 i2 t i1 i2 t
  3721. 24 1 2005-05-27 12:40:30 24 1 2006-06-20 12:29:40
  3722. DROP TABLE t1, t2;
  3723. CREATE TABLE t1 (i INT);
  3724. (SELECT i FROM t1) UNION (SELECT i FROM t1);
  3725. i
  3726. SELECT sql_no_cache * FROM t1 WHERE NOT EXISTS
  3727. (
  3728. (SELECT i FROM t1) UNION
  3729. (SELECT i FROM t1)
  3730. );
  3731. i
  3732. SELECT * FROM t1
  3733. WHERE NOT EXISTS (((SELECT i FROM t1) UNION (SELECT i FROM t1)));
  3734. i
  3735. explain select ((select t11.i from t1 t11) union (select t12.i from t1 t12))
  3736. from t1;
  3737. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'union (select t12.i from t1 t12))
  3738. from t1' at line 1
  3739. explain select * from t1 where not exists
  3740. ((select t11.i from t1 t11) union (select t12.i from t1 t12));
  3741. id select_type table type possible_keys key key_len ref rows Extra
  3742. 1 PRIMARY t1 system NULL NULL NULL NULL 0 Const row not found
  3743. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  3744. 3 UNION NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  3745. NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
  3746. DROP TABLE t1;
  3747. CREATE TABLE t1 (a VARCHAR(250), b INT auto_increment, PRIMARY KEY (b));
  3748. insert into t1 (a) values (FLOOR(rand() * 100));
  3749. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3750. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3751. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3752. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3753. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3754. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3755. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3756. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3757. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3758. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3759. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3760. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3761. insert into t1 (a) select FLOOR(rand() * 100) from t1;
  3762. SELECT a,
  3763. (SELECT REPEAT(' ',250) FROM t1 i1
  3764. WHERE i1.b=t1.a ORDER BY RAND() LIMIT 1) AS a
  3765. FROM t1 ORDER BY a LIMIT 5;
  3766. a a
  3767. 0 NULL
  3768. 0 NULL
  3769. 0 NULL
  3770. 0 NULL
  3771. 0 NULL
  3772. DROP TABLE t1;
  3773. CREATE TABLE t1 (a INT, b INT);
  3774. CREATE TABLE t2 (a INT);
  3775. INSERT INTO t2 values (1);
  3776. INSERT INTO t1 VALUES (1,1),(1,2),(2,3),(3,4);
  3777. SELECT (SELECT COUNT(DISTINCT t1.b) from t2) FROM t1 GROUP BY t1.a;
  3778. (SELECT COUNT(DISTINCT t1.b) from t2)
  3779. 2
  3780. 1
  3781. 1
  3782. SELECT (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
  3783. FROM t1 GROUP BY t1.a;
  3784. (SELECT COUNT(DISTINCT t1.b) from t2 union select 1 from t2 where 12 < 3)
  3785. 2
  3786. 1
  3787. 1
  3788. SELECT COUNT(DISTINCT t1.b), (SELECT COUNT(DISTINCT t1.b)) FROM t1 GROUP BY t1.a;
  3789. COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b))
  3790. 2 2
  3791. 1 1
  3792. 1 1
  3793. SELECT COUNT(DISTINCT t1.b),
  3794. (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
  3795. FROM t1 GROUP BY t1.a;
  3796. COUNT(DISTINCT t1.b) (SELECT COUNT(DISTINCT t1.b) union select 1 from DUAL where 12 < 3)
  3797. 2 2
  3798. 1 1
  3799. 1 1
  3800. SELECT (
  3801. SELECT (
  3802. SELECT COUNT(DISTINCT t1.b)
  3803. )
  3804. )
  3805. FROM t1 GROUP BY t1.a;
  3806. (
  3807. SELECT (
  3808. SELECT COUNT(DISTINCT t1.b)
  3809. )
  3810. )
  3811. 2
  3812. 1
  3813. 1
  3814. SELECT (
  3815. SELECT (
  3816. SELECT (
  3817. SELECT COUNT(DISTINCT t1.b)
  3818. )
  3819. )
  3820. FROM t1 GROUP BY t1.a LIMIT 1)
  3821. FROM t1 t2
  3822. GROUP BY t2.a;
  3823. (
  3824. SELECT (
  3825. SELECT (
  3826. SELECT COUNT(DISTINCT t1.b)
  3827. )
  3828. )
  3829. FROM t1 GROUP BY t1.a LIMIT 1)
  3830. 2
  3831. 2
  3832. 2
  3833. DROP TABLE t1,t2;
  3834. CREATE TABLE t1 (a int, b int, PRIMARY KEY (b));
  3835. CREATE TABLE t2 (x int auto_increment, y int, z int,
  3836. PRIMARY KEY (x), FOREIGN KEY (y) REFERENCES t1 (b));
  3837. create table t3 (a int);
  3838. insert into t3 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
  3839. insert into t1 select RAND()*1000, A.a + 10*(B.a+10*(C.a+10*D.a))
  3840. from t3 A, t3 B, t3 C, t3 D where D.a<3;
  3841. insert into t2(y,z) select t1.b, RAND()*1000 from t1, t3;
  3842. SET SESSION sort_buffer_size = 32 * 1024;
  3843. SELECT SQL_NO_CACHE COUNT(*)
  3844. FROM (SELECT a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c
  3845. FROM t1) t;
  3846. COUNT(*)
  3847. 3000
  3848. SET SESSION sort_buffer_size = 8 * 1024 * 1024;
  3849. SELECT SQL_NO_CACHE COUNT(*)
  3850. FROM (SELECT a, b, (SELECT x FROM t2 WHERE y=b ORDER BY z DESC LIMIT 1) c
  3851. FROM t1) t;
  3852. COUNT(*)
  3853. 3000
  3854. DROP TABLE t1,t2,t3;
  3855. CREATE TABLE t1 (id char(4) PRIMARY KEY, c int);
  3856. CREATE TABLE t2 (c int);
  3857. INSERT INTO t1 VALUES ('aa', 1);
  3858. INSERT INTO t2 VALUES (1);
  3859. SELECT * FROM t1
  3860. WHERE EXISTS (SELECT c FROM t2 WHERE c=1
  3861. UNION
  3862. SELECT c from t2 WHERE c=t1.c);
  3863. id c
  3864. aa 1
  3865. INSERT INTO t1 VALUES ('bb', 2), ('cc', 3), ('dd',1);
  3866. SELECT * FROM t1
  3867. WHERE EXISTS (SELECT c FROM t2 WHERE c=1
  3868. UNION
  3869. SELECT c from t2 WHERE c=t1.c);
  3870. id c
  3871. aa 1
  3872. bb 2
  3873. cc 3
  3874. dd 1
  3875. INSERT INTO t2 VALUES (2);
  3876. CREATE TABLE t3 (c int);
  3877. INSERT INTO t3 VALUES (1);
  3878. SELECT * FROM t1
  3879. WHERE EXISTS (SELECT t2.c FROM t2 JOIN t3 ON t2.c=t3.c WHERE t2.c=1
  3880. UNION
  3881. SELECT c from t2 WHERE c=t1.c);
  3882. id c
  3883. aa 1
  3884. bb 2
  3885. cc 3
  3886. dd 1
  3887. DROP TABLE t1,t2,t3;
  3888. CREATE TABLE t1(f1 int);
  3889. CREATE TABLE t2(f2 int, f21 int, f3 timestamp);
  3890. INSERT INTO t1 VALUES (1),(1),(2),(2);
  3891. INSERT INTO t2 VALUES (1,1,"2004-02-29 11:11:11"), (2,2,"2004-02-29 11:11:11");
  3892. SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1;
  3893. sq
  3894. 2
  3895. 4
  3896. SELECT (SELECT SUM(1) FROM t2 ttt GROUP BY t2.f3 LIMIT 1) AS tt FROM t2;
  3897. tt
  3898. 2
  3899. 2
  3900. PREPARE stmt1 FROM 'SELECT ((SELECT f2 FROM t2 WHERE f21=f1 LIMIT 1) * COUNT(f1)) AS sq FROM t1 GROUP BY f1';
  3901. EXECUTE stmt1;
  3902. sq
  3903. 2
  3904. 4
  3905. EXECUTE stmt1;
  3906. sq
  3907. 2
  3908. 4
  3909. DEALLOCATE PREPARE stmt1;
  3910. SELECT f2, AVG(f21),
  3911. (SELECT t.f3 FROM t2 AS t WHERE t2.f2=t.f2 AND t.f3=MAX(t2.f3)) AS test
  3912. FROM t2 GROUP BY f2;
  3913. f2 AVG(f21) test
  3914. 1 1.0000 2004-02-29 11:11:11
  3915. 2 2.0000 2004-02-29 11:11:11
  3916. DROP TABLE t1,t2;
  3917. CREATE TABLE t1 (a int, b INT, c CHAR(10) NOT NULL);
  3918. INSERT INTO t1 VALUES
  3919. (1,1,'a'), (1,2,'b'), (1,3,'c'), (1,4,'d'), (1,5,'e'),
  3920. (2,1,'f'), (2,2,'g'), (2,3,'h'), (3,4,'i'), (3,3,'j'),
  3921. (3,2,'k'), (3,1,'l'), (1,9,'m');
  3922. SELECT a, MAX(b),
  3923. (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b)) AS test
  3924. FROM t1 GROUP BY a;
  3925. a MAX(b) test
  3926. 1 9 m
  3927. 2 3 h
  3928. 3 4 i
  3929. DROP TABLE t1;
  3930. DROP TABLE IF EXISTS t1;
  3931. DROP TABLE IF EXISTS t2;
  3932. DROP TABLE IF EXISTS t1xt2;
  3933. CREATE TABLE t1 (
  3934. id_1 int(5) NOT NULL,
  3935. t varchar(4) DEFAULT NULL
  3936. );
  3937. CREATE TABLE t2 (
  3938. id_2 int(5) NOT NULL,
  3939. t varchar(4) DEFAULT NULL
  3940. );
  3941. CREATE TABLE t1xt2 (
  3942. id_1 int(5) NOT NULL,
  3943. id_2 int(5) NOT NULL
  3944. );
  3945. INSERT INTO t1 VALUES (1, 'a'), (2, 'b'), (3, 'c'), (4, 'd');
  3946. INSERT INTO t2 VALUES (2, 'bb'), (3, 'cc'), (4, 'dd'), (12, 'aa');
  3947. INSERT INTO t1xt2 VALUES (2, 2), (3, 3), (4, 4);
  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. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3952. (12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
  3953. id_1
  3954. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3955. (12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
  3956. id_1
  3957. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3958. (12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
  3959. id_1
  3960. 1
  3961. 2
  3962. 3
  3963. 4
  3964. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3965. (12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1)));
  3966. id_1
  3967. 1
  3968. 2
  3969. 3
  3970. 4
  3971. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3972. (12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 where t1.id_1 = t1xt2.id_1))));
  3973. id_1
  3974. 1
  3975. 2
  3976. 3
  3977. 4
  3978. insert INTO t1xt2 VALUES (1, 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. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3984. (12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
  3985. id_1
  3986. 1
  3987. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3988. (12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
  3989. id_1
  3990. 1
  3991. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3992. (12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
  3993. id_1
  3994. 2
  3995. 3
  3996. 4
  3997. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  3998. (12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
  3999. id_1
  4000. 2
  4001. 3
  4002. 4
  4003. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  4004. (12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
  4005. id_1
  4006. 2
  4007. 3
  4008. 4
  4009. insert INTO t1xt2 VALUES (2, 12);
  4010. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  4011. (12 IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
  4012. id_1
  4013. 1
  4014. 2
  4015. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  4016. (12 IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
  4017. id_1
  4018. 1
  4019. 2
  4020. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  4021. (12 IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
  4022. id_1
  4023. 1
  4024. 2
  4025. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  4026. (12 NOT IN (SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1));
  4027. id_1
  4028. 3
  4029. 4
  4030. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  4031. (12 NOT IN ((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1)));
  4032. id_1
  4033. 3
  4034. 4
  4035. SELECT DISTINCT t1.id_1 FROM t1 WHERE
  4036. (12 NOT IN (((SELECT t1xt2.id_2 FROM t1xt2 WHERE t1.id_1 = t1xt2.id_1))));
  4037. id_1
  4038. 3
  4039. 4
  4040. DROP TABLE t1;
  4041. DROP TABLE t2;
  4042. DROP TABLE t1xt2;
  4043. CREATE TABLE t1 (a int);
  4044. INSERT INTO t1 VALUES (3), (1), (2);
  4045. SELECT 'this is ' 'a test.' AS col1, a AS col2 FROM t1;
  4046. col1 col2
  4047. this is a test. 3
  4048. this is a test. 1
  4049. this is a test. 2
  4050. SELECT * FROM (SELECT 'this is ' 'a test.' AS col1, a AS t2 FROM t1) t;
  4051. col1 t2
  4052. this is a test. 3
  4053. this is a test. 1
  4054. this is a test. 2
  4055. DROP table t1;
  4056. CREATE TABLE t1 (a int, b int);
  4057. CREATE TABLE t2 (m int, n int);
  4058. INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
  4059. INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
  4060. SELECT COUNT(*), a,
  4061. (SELECT m FROM t2 WHERE m = count(*) LIMIT 1)
  4062. FROM t1 GROUP BY a;
  4063. COUNT(*) a (SELECT m FROM t2 WHERE m = count(*) LIMIT 1)
  4064. 2 2 2
  4065. 3 3 3
  4066. 1 4 1
  4067. SELECT COUNT(*), a,
  4068. (SELECT MIN(m) FROM t2 WHERE m = count(*))
  4069. FROM t1 GROUP BY a;
  4070. COUNT(*) a (SELECT MIN(m) FROM t2 WHERE m = count(*))
  4071. 2 2 2
  4072. 3 3 3
  4073. 1 4 1
  4074. SELECT COUNT(*), a
  4075. FROM t1 GROUP BY a
  4076. HAVING (SELECT MIN(m) FROM t2 WHERE m = count(*)) > 1;
  4077. COUNT(*) a
  4078. 2 2
  4079. 3 3
  4080. DROP TABLE t1,t2;
  4081. CREATE TABLE t1 (a int, b int);
  4082. CREATE TABLE t2 (m int, n int);
  4083. INSERT INTO t1 VALUES (2,2), (2,2), (3,3), (3,3), (3,3), (4,4);
  4084. INSERT INTO t2 VALUES (1,11), (2,22), (3,32), (4,44), (4,44);
  4085. SELECT COUNT(*) c, a,
  4086. (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a)
  4087. FROM t1 GROUP BY a;
  4088. c a (SELECT GROUP_CONCAT(COUNT(a)) FROM t2 WHERE m = a)
  4089. 2 2 2
  4090. 3 3 3
  4091. 1 4 1,1
  4092. SELECT COUNT(*) c, a,
  4093. (SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a)
  4094. FROM t1 GROUP BY a;
  4095. c a (SELECT GROUP_CONCAT(COUNT(a)+1) FROM t2 WHERE m = a)
  4096. 2 2 3
  4097. 3 3 4
  4098. 1 4 2,2
  4099. DROP table t1,t2;
  4100. CREATE TABLE t1 (a int, b INT, d INT, c CHAR(10) NOT NULL, PRIMARY KEY (a, b));
  4101. INSERT INTO t1 VALUES (1,1,0,'a'), (1,2,0,'b'), (1,3,0,'c'), (1,4,0,'d'),
  4102. (1,5,0,'e'), (2,1,0,'f'), (2,2,0,'g'), (2,3,0,'h'), (3,4,0,'i'), (3,3,0,'j'),
  4103. (3,2,0,'k'), (3,1,0,'l'), (1,9,0,'m'), (1,0,10,'n'), (2,0,5,'o'), (3,0,7,'p');
  4104. SELECT a, MAX(b),
  4105. (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=MAX(t1.b + 0)) as test
  4106. FROM t1 GROUP BY a;
  4107. a MAX(b) test
  4108. 1 9 m
  4109. 2 3 h
  4110. 3 4 i
  4111. SELECT a x, MAX(b),
  4112. (SELECT t.c FROM t1 AS t WHERE x=t.a AND t.b=MAX(t1.b + 0)) as test
  4113. FROM t1 GROUP BY a;
  4114. x MAX(b) test
  4115. 1 9 m
  4116. 2 3 h
  4117. 3 4 i
  4118. SELECT a, AVG(b),
  4119. (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.b=AVG(t1.b)) AS test
  4120. FROM t1 WHERE t1.d=0 GROUP BY a;
  4121. a AVG(b) test
  4122. 1 4.0000 d
  4123. 2 2.0000 g
  4124. 3 2.5000 NULL
  4125. SELECT tt.a,
  4126. (SELECT (SELECT c FROM t1 as t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
  4127. LIMIT 1) FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test
  4128. FROM t1 as tt;
  4129. a test
  4130. 1 n
  4131. 1 n
  4132. 1 n
  4133. 1 n
  4134. 1 n
  4135. 1 n
  4136. 1 n
  4137. 2 o
  4138. 2 o
  4139. 2 o
  4140. 2 o
  4141. 3 p
  4142. 3 p
  4143. 3 p
  4144. 3 p
  4145. 3 p
  4146. SELECT tt.a,
  4147. (SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
  4148. LIMIT 1)
  4149. FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1) as test
  4150. FROM t1 as tt GROUP BY tt.a;
  4151. a test
  4152. 1 n
  4153. 2 o
  4154. 3 p
  4155. SELECT tt.a, MAX(
  4156. (SELECT (SELECT t.c FROM t1 AS t WHERE t1.a=t.a AND t.d=MAX(t1.b + tt.a)
  4157. LIMIT 1)
  4158. FROM t1 WHERE t1.a=tt.a GROUP BY a LIMIT 1)) as test
  4159. FROM t1 as tt GROUP BY tt.a;
  4160. a test
  4161. 1 n
  4162. 2 o
  4163. 3 p
  4164. DROP TABLE t1;
  4165. CREATE TABLE t1 (a int, b int);
  4166. INSERT INTO t1 VALUES (2,22),(1,11),(2,22);
  4167. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
  4168. a
  4169. 1
  4170. 2
  4171. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
  4172. a
  4173. SELECT a FROM t1 t0
  4174. WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
  4175. a
  4176. 1
  4177. 2
  4178. SET @@sql_mode='ansi';
  4179. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
  4180. ERROR HY000: Invalid use of group function
  4181. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
  4182. ERROR HY000: Invalid use of group function
  4183. SELECT a FROM t1 t0
  4184. WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
  4185. ERROR HY000: Invalid use of group function
  4186. SET @@sql_mode=default;
  4187. DROP TABLE t1;
  4188. CREATE TABLE t1 (a INT);
  4189. INSERT INTO t1 values (1),(1),(1),(1);
  4190. CREATE TABLE t2 (x INT);
  4191. INSERT INTO t1 values (1000),(1001),(1002);
  4192. SELECT SUM( (SELECT COUNT(a) FROM t2) ) FROM t1;
  4193. ERROR HY000: Invalid use of group function
  4194. SELECT SUM( (SELECT SUM(COUNT(a)) FROM t2) ) FROM t1;
  4195. ERROR HY000: Invalid use of group function
  4196. SELECT COUNT(1) FROM DUAL;
  4197. COUNT(1)
  4198. 1
  4199. SELECT SUM( (SELECT AVG( (SELECT t1.a FROM t2) ) FROM DUAL) ) FROM t1;
  4200. ERROR HY000: Invalid use of group function
  4201. SELECT
  4202. SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING t1.a < 12) ) FROM t2) )
  4203. FROM t1;
  4204. ERROR HY000: Invalid use of group function
  4205. SELECT t1.a as XXA,
  4206. SUM( (SELECT AVG( (SELECT COUNT(*) FROM t1 t HAVING XXA < 12) ) FROM t2) )
  4207. FROM t1;
  4208. ERROR HY000: Invalid use of group function
  4209. DROP TABLE t1,t2;
  4210. CREATE TABLE t1 (a int, b int, KEY (a));
  4211. INSERT INTO t1 VALUES (1,1),(2,1);
  4212. EXPLAIN SELECT 1 FROM t1 WHERE a = (SELECT COUNT(*) FROM t1 GROUP BY b);
  4213. id select_type table type possible_keys key key_len ref rows Extra
  4214. 1 PRIMARY t1 ref a a 5 const 1 Using where; Using index
  4215. 2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using temporary; Using filesort
  4216. DROP TABLE t1;
  4217. CREATE TABLE t1 (id int NOT NULL, st CHAR(2), INDEX idx(id));
  4218. INSERT INTO t1 VALUES
  4219. (3,'FL'), (2,'GA'), (4,'FL'), (1,'GA'), (5,'NY'), (7,'FL'), (6,'NY');
  4220. CREATE TABLE t2 (id int NOT NULL, INDEX idx(id));
  4221. INSERT INTO t2 VALUES (7), (5), (1), (3);
  4222. SELECT id, st FROM t1
  4223. WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id);
  4224. id st
  4225. 3 FL
  4226. 1 GA
  4227. 7 FL
  4228. SELECT id, st FROM t1
  4229. WHERE st IN ('GA','FL') AND EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id)
  4230. GROUP BY id;
  4231. id st
  4232. 1 GA
  4233. 3 FL
  4234. 7 FL
  4235. SELECT id, st FROM t1
  4236. WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id);
  4237. id st
  4238. 2 GA
  4239. 4 FL
  4240. SELECT id, st FROM t1
  4241. WHERE st IN ('GA','FL') AND NOT EXISTS(SELECT 1 FROM t2 WHERE t2.id=t1.id)
  4242. GROUP BY id;
  4243. id st
  4244. 2 GA
  4245. 4 FL
  4246. DROP TABLE t1,t2;
  4247. CREATE TABLE t1 (a int);
  4248. INSERT INTO t1 VALUES (1), (2);
  4249. EXPLAIN EXTENDED
  4250. SELECT * FROM (SELECT count(*) FROM t1 GROUP BY a) as res;
  4251. id select_type table type possible_keys key key_len ref rows filtered Extra
  4252. 1 PRIMARY <derived2> ALL NULL NULL NULL NULL 2 100.00
  4253. 2 DERIVED t1 ALL NULL NULL NULL NULL 2 100.00 Using temporary; Using filesort
  4254. Warnings:
  4255. Note 1003 /* select#1 */ select `res`.`count(*)` AS `count(*)` from (/* select#2 */ select count(0) AS `count(*)` from `test`.`t1` group by `test`.`t1`.`a`) `res`
  4256. DROP TABLE t1;
  4257. CREATE TABLE t1 (
  4258. a varchar(255) default NULL,
  4259. b timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  4260. INDEX idx(a,b)
  4261. );
  4262. CREATE TABLE t2 (
  4263. a varchar(255) default NULL
  4264. );
  4265. INSERT INTO t1 VALUES ('abcdefghijk','2007-05-07 06:00:24');
  4266. INSERT INTO t1 SELECT * FROM t1;
  4267. INSERT INTO t1 SELECT * FROM t1;
  4268. INSERT INTO t1 SELECT * FROM t1;
  4269. INSERT INTO t1 SELECT * FROM t1;
  4270. INSERT INTO t1 SELECT * FROM t1;
  4271. INSERT INTO t1 SELECT * FROM t1;
  4272. INSERT INTO t1 SELECT * FROM t1;
  4273. INSERT INTO t1 SELECT * FROM t1;
  4274. INSERT INTO `t1` VALUES ('asdf','2007-02-08 01:11:26');
  4275. INSERT INTO `t2` VALUES ('abcdefghijk');
  4276. INSERT INTO `t2` VALUES ('asdf');
  4277. SET session sort_buffer_size=8192;
  4278. SELECT (SELECT 1 FROM t1 WHERE t1.a=t2.a ORDER BY t1.b LIMIT 1) AS d1 FROM t2;
  4279. d1
  4280. 1
  4281. 1
  4282. DROP TABLE t1,t2;
  4283. CREATE TABLE t1 (a INTEGER, b INTEGER);
  4284. CREATE TABLE t2 (x INTEGER);
  4285. INSERT INTO t1 VALUES (1,11), (2,22), (2,22);
  4286. INSERT INTO t2 VALUES (1), (2);
  4287. SELECT a, COUNT(b), (SELECT COUNT(b) FROM t2) FROM t1 GROUP BY a;
  4288. ERROR 21000: Subquery returns more than 1 row
  4289. SELECT a, COUNT(b), (SELECT COUNT(b)+0 FROM t2) FROM t1 GROUP BY a;
  4290. ERROR 21000: Subquery returns more than 1 row
  4291. SELECT (SELECT SUM(t1.a)/AVG(t2.x) FROM t2) FROM t1;
  4292. (SELECT SUM(t1.a)/AVG(t2.x) FROM t2)
  4293. 3.3333
  4294. DROP TABLE t1,t2;
  4295. CREATE TABLE t1 (a INT, b INT);
  4296. INSERT INTO t1 VALUES (1, 2), (1,3), (1,4), (2,1), (2,2);
  4297. SELECT a1.a, COUNT(*) FROM t1 a1 WHERE a1.a = 1
  4298. AND EXISTS( SELECT a2.a FROM t1 a2 WHERE a2.a = a1.a)
  4299. GROUP BY a1.a;
  4300. a COUNT(*)
  4301. 1 3
  4302. DROP TABLE t1;
  4303. CREATE TABLE t1 (a INT);
  4304. CREATE TABLE t2 (a INT);
  4305. INSERT INTO t1 VALUES (1),(2);
  4306. INSERT INTO t2 VALUES (1),(2);
  4307. SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=0) FROM t1;
  4308. (SELECT SUM(t1.a) FROM t2 WHERE a=0)
  4309. NULL
  4310. SELECT (SELECT SUM(t1.a) FROM t2 WHERE a!=0) FROM t1;
  4311. ERROR 21000: Subquery returns more than 1 row
  4312. SELECT (SELECT SUM(t1.a) FROM t2 WHERE a=1) FROM t1;
  4313. (SELECT SUM(t1.a) FROM t2 WHERE a=1)
  4314. 3
  4315. DROP TABLE t1,t2;
  4316. CREATE TABLE t1 (a1 INT, a2 INT);
  4317. CREATE TABLE t2 (b1 INT, b2 INT);
  4318. INSERT INTO t1 VALUES (100, 200);
  4319. INSERT INTO t1 VALUES (101, 201);
  4320. INSERT INTO t2 VALUES (101, 201);
  4321. INSERT INTO t2 VALUES (103, 203);
  4322. SELECT ((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL FROM t1;
  4323. ((a1,a2) IN (SELECT * FROM t2 WHERE b2 > 0)) IS NULL
  4324. 0
  4325. 0
  4326. DROP TABLE t1, t2;
  4327. CREATE TABLE t1 (s1 BINARY(5), s2 VARBINARY(5));
  4328. INSERT INTO t1 VALUES (0x41,0x41), (0x42,0x42), (0x43,0x43);
  4329. SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
  4330. s1 s2
  4331. SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
  4332. s1 s2
  4333. CREATE INDEX I1 ON t1 (s1);
  4334. CREATE INDEX I2 ON t1 (s2);
  4335. SELECT s1, s2 FROM t1 WHERE s2 IN (SELECT s1 FROM t1);
  4336. s1 s2
  4337. SELECT s1, s2 FROM t1 WHERE (s2, 10) IN (SELECT s1, 10 FROM t1);
  4338. s1 s2
  4339. TRUNCATE t1;
  4340. INSERT INTO t1 VALUES (0x41,0x41);
  4341. SELECT * FROM t1 WHERE s1 = (SELECT s2 FROM t1);
  4342. s1 s2
  4343. DROP TABLE t1;
  4344. CREATE TABLE t1 (a1 VARBINARY(2) NOT NULL DEFAULT '0', PRIMARY KEY (a1));
  4345. CREATE TABLE t2 (a2 BINARY(2) default '0', INDEX (a2));
  4346. CREATE TABLE t3 (a3 BINARY(2) default '0');
  4347. INSERT INTO t1 VALUES (1),(2),(3),(4);
  4348. INSERT INTO t2 VALUES (1),(2),(3);
  4349. INSERT INTO t3 VALUES (1),(2),(3);
  4350. SELECT LEFT(t2.a2, 1) FROM t2,t3 WHERE t3.a3=t2.a2;
  4351. LEFT(t2.a2, 1)
  4352. 1
  4353. 2
  4354. 3
  4355. SELECT t1.a1, t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2) FROM t1;
  4356. a1 t1.a1 in (SELECT t2.a2 FROM t2,t3 WHERE t3.a3=t2.a2)
  4357. 1 0
  4358. 2 0
  4359. 3 0
  4360. 4 0
  4361. DROP TABLE t1,t2,t3;
  4362. CREATE TABLE t1 (a1 BINARY(3) PRIMARY KEY, b1 VARBINARY(3));
  4363. CREATE TABLE t2 (a2 VARBINARY(3) PRIMARY KEY);
  4364. CREATE TABLE t3 (a3 VARBINARY(3) PRIMARY KEY);
  4365. INSERT INTO t1 VALUES (1,10), (2,20), (3,30), (4,40);
  4366. INSERT INTO t2 VALUES (2), (3), (4), (5);
  4367. INSERT INTO t3 VALUES (10), (20), (30);
  4368. SELECT LEFT(t1.a1,1) FROM t1,t3 WHERE t1.b1=t3.a3;
  4369. LEFT(t1.a1,1)
  4370. 1
  4371. 2
  4372. 3
  4373. SELECT a2 FROM t2 WHERE t2.a2 IN (SELECT t1.a1 FROM t1,t3 WHERE t1.b1=t3.a3);
  4374. a2
  4375. DROP TABLE t1, t2, t3;
  4376. SET @save_optimizer_switch=@@optimizer_switch;
  4377. SET optimizer_switch='semijoin_with_cache=off';
  4378. SET optimizer_switch='materialization=off';
  4379. CREATE TABLE t1 (a CHAR(1), b VARCHAR(10));
  4380. INSERT INTO t1 VALUES ('a', 'aa');
  4381. INSERT INTO t1 VALUES ('a', 'aaa');
  4382. SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
  4383. a b
  4384. CREATE INDEX I1 ON t1 (a);
  4385. CREATE INDEX I2 ON t1 (b);
  4386. EXPLAIN SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
  4387. id select_type table type possible_keys key key_len ref rows Extra
  4388. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
  4389. 2 DEPENDENT SUBQUERY t1 index_subquery I1 I1 2 func 2 Using index; Using where
  4390. SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1);
  4391. a b
  4392. CREATE TABLE t2 (a VARCHAR(1), b VARCHAR(10));
  4393. INSERT INTO t2 SELECT * FROM t1;
  4394. CREATE INDEX I1 ON t2 (a);
  4395. CREATE INDEX I2 ON t2 (b);
  4396. EXPLAIN SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
  4397. id select_type table type possible_keys key key_len ref rows Extra
  4398. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where
  4399. 2 DEPENDENT SUBQUERY t2 index_subquery I1 I1 4 func 2 Using index; Using where
  4400. SELECT a,b FROM t2 WHERE b IN (SELECT a FROM t2);
  4401. a b
  4402. EXPLAIN
  4403. SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
  4404. id select_type table type possible_keys key key_len ref rows Extra
  4405. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
  4406. 2 DEPENDENT SUBQUERY t1 index_subquery I1 I1 2 func 2 Using index; Using where
  4407. SELECT a,b FROM t1 WHERE b IN (SELECT a FROM t1 WHERE LENGTH(a)<500);
  4408. a b
  4409. DROP TABLE t1,t2;
  4410. SET optimizer_switch= @save_optimizer_switch;
  4411. CREATE TABLE t1(a INT, b INT);
  4412. INSERT INTO t1 VALUES (1,1), (1,2), (2,3), (2,4);
  4413. EXPLAIN
  4414. SELECT a AS out_a, MIN(b) FROM t1
  4415. WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
  4416. GROUP BY a;
  4417. ERROR 42S22: Unknown column 'out_a' in 'where clause'
  4418. SELECT a AS out_a, MIN(b) FROM t1
  4419. WHERE b > (SELECT MIN(b) FROM t1 WHERE a = out_a)
  4420. GROUP BY a;
  4421. ERROR 42S22: Unknown column 'out_a' in 'where clause'
  4422. EXPLAIN
  4423. SELECT a AS out_a, MIN(b) FROM t1 t1_outer
  4424. WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
  4425. GROUP BY a;
  4426. id select_type table type possible_keys key key_len ref rows Extra
  4427. 1 PRIMARY t1_outer ALL NULL NULL NULL NULL 4 Using where; Using temporary; Using filesort
  4428. 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 4 Using where
  4429. SELECT a AS out_a, MIN(b) FROM t1 t1_outer
  4430. WHERE b > (SELECT MIN(b) FROM t1 WHERE a = t1_outer.a)
  4431. GROUP BY a;
  4432. out_a MIN(b)
  4433. 1 2
  4434. 2 4
  4435. DROP TABLE t1;
  4436. CREATE TABLE t1 (a INT);
  4437. CREATE TABLE t2 (a INT);
  4438. INSERT INTO t1 VALUES (1),(2);
  4439. INSERT INTO t2 VALUES (1),(2);
  4440. SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
  4441. 2
  4442. 2
  4443. 2
  4444. EXPLAIN EXTENDED
  4445. SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a));
  4446. id select_type table type possible_keys key key_len ref rows filtered Extra
  4447. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 Using where
  4448. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 100.00
  4449. Warnings:
  4450. Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
  4451. Note 1003 /* select#1 */ select 2 AS `2` from `test`.`t1` where <expr_cache><`test`.`t1`.`a`>(<in_optimizer>(`test`.`t1`.`a`,`test`.`t1`.`a` in ( <materialize> (/* select#2 */ select `test`.`t2`.`a` from `test`.`t2` where 1 ), <primary_index_lookup>(`test`.`t1`.`a` in <temporary table> on distinct_key where `test`.`t1`.`a` = `<subquery2>`.`a`))))
  4452. EXPLAIN EXTENDED
  4453. SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION
  4454. (SELECT 1 FROM t2 WHERE t1.a = t2.a));
  4455. id select_type table type possible_keys key key_len ref rows filtered Extra
  4456. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 Using where
  4457. 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
  4458. 3 DEPENDENT UNION t2 ALL NULL NULL NULL NULL 2 100.00 Using where
  4459. NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
  4460. Warnings:
  4461. Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
  4462. Note 1276 Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #1
  4463. Note 1003 /* select#1 */ select 2 AS `2` from `test`.`t1` where <expr_cache><`test`.`t1`.`a`>(exists((/* select#2 */ select 1 from `test`.`t2` where `test`.`t1`.`a` = `test`.`t2`.`a`) union (/* select#3 */ select 1 from `test`.`t2` where `test`.`t1`.`a` = `test`.`t2`.`a`)))
  4464. DROP TABLE t1,t2;
  4465. create table t0(a int);
  4466. insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
  4467. create table t1(f11 int, f12 int);
  4468. create table t2(f21 int unsigned not null, f22 int, f23 varchar(10));
  4469. insert into t1 values(1,1),(2,2), (3, 3);
  4470. insert ignore into t2
  4471. select -1 , (@a:=(A.a + 10 * (B.a + 10 * (C.a+10*D.a))))/5000 + 1, @a
  4472. from t0 A, t0 B, t0 C, t0 D;
  4473. set session sort_buffer_size= 33*1024;
  4474. select count(*) from t1 where f12 =
  4475. (select f22 from t2 where f22 = f12 order by f21 desc, f22, f23 limit 1);
  4476. count(*)
  4477. 3
  4478. drop table t0,t1,t2;
  4479. CREATE TABLE t4 (
  4480. f7 varchar(32) collate utf8_bin NOT NULL default '',
  4481. f10 varchar(32) collate utf8_bin default NULL,
  4482. PRIMARY KEY (f7)
  4483. );
  4484. INSERT INTO t4 VALUES(1,1), (2,null);
  4485. CREATE TABLE t2 (
  4486. f4 varchar(32) collate utf8_bin NOT NULL default '',
  4487. f2 varchar(50) collate utf8_bin default NULL,
  4488. f3 varchar(10) collate utf8_bin default NULL,
  4489. PRIMARY KEY (f4),
  4490. UNIQUE KEY uk1 (f2)
  4491. );
  4492. INSERT INTO t2 VALUES(1,1,null), (2,2,null);
  4493. CREATE TABLE t1 (
  4494. f8 varchar(32) collate utf8_bin NOT NULL default '',
  4495. f1 varchar(10) collate utf8_bin default NULL,
  4496. f9 varchar(32) collate utf8_bin default NULL,
  4497. PRIMARY KEY (f8)
  4498. );
  4499. INSERT INTO t1 VALUES (1,'P',1), (2,'P',1), (3,'R',2);
  4500. CREATE TABLE t3 (
  4501. f6 varchar(32) collate utf8_bin NOT NULL default '',
  4502. f5 varchar(50) collate utf8_bin default NULL,
  4503. PRIMARY KEY (f6)
  4504. );
  4505. INSERT INTO t3 VALUES (1,null), (2,null);
  4506. SELECT
  4507. IF(t1.f1 = 'R', a1.f2, t2.f2) AS a4,
  4508. IF(t1.f1 = 'R', a1.f3, t2.f3) AS f3,
  4509. SUM(
  4510. IF(
  4511. (SELECT VPC.f2
  4512. FROM t2 VPC, t4 a2, t2 a3
  4513. WHERE
  4514. VPC.f4 = a2.f10 AND a3.f2 = a4
  4515. LIMIT 1) IS NULL,
  4516. 0,
  4517. t3.f5
  4518. )
  4519. ) AS a6
  4520. FROM
  4521. t2, t3, t1 JOIN t2 a1 ON t1.f9 = a1.f4
  4522. GROUP BY a4;
  4523. a4 f3 a6
  4524. 1 NULL NULL
  4525. 2 NULL NULL
  4526. DROP TABLE t1, t2, t3, t4;
  4527. create table t1 (a float(5,4) zerofill);
  4528. create table t2 (a float(5,4),b float(2,0));
  4529. select t1.a from t1 where
  4530. t1.a= (select b from t2 limit 1) and not
  4531. t1.a= (select a from t2 limit 1) ;
  4532. a
  4533. drop table t1, t2;
  4534. CREATE TABLE t1 (a INT);
  4535. INSERT INTO t1 VALUES (1),(2);
  4536. SET @save_join_cache_level=@@join_cache_level;
  4537. SET join_cache_level=0;
  4538. EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT min(a) FROM t1 GROUP BY a);
  4539. id select_type table type possible_keys key key_len ref rows filtered Extra
  4540. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  4541. 2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using temporary
  4542. Warnings:
  4543. Note 1003 /* select#1 */ select 1 AS `1` from `test`.`t1` where 1
  4544. EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE 1 IN (SELECT min(a) FROM t1 WHERE a > 3 GROUP BY a);
  4545. id select_type table type possible_keys key key_len ref rows filtered Extra
  4546. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  4547. 2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using where; Using temporary
  4548. Warnings:
  4549. Note 1003 /* select#1 */ select 1 AS `1` from `test`.`t1` where 0
  4550. SET join_cache_level=@save_join_cache_level;
  4551. DROP TABLE t1;
  4552. #
  4553. # Bug#45061: Incorrectly market field caused wrong result.
  4554. #
  4555. CREATE TABLE `C` (
  4556. `int_nokey` int(11) NOT NULL,
  4557. `int_key` int(11) NOT NULL,
  4558. KEY `int_key` (`int_key`)
  4559. );
  4560. INSERT INTO `C` VALUES (9,9), (0,0), (8,6), (3,6), (7,6), (0,4),
  4561. (1,7), (9,4), (0,8), (9,4), (0,7), (5,5), (0,0), (8,5), (8,7),
  4562. (5,2), (1,8), (7,0), (0,9), (9,5);
  4563. SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`);
  4564. int_nokey int_key
  4565. 9 9
  4566. 0 0
  4567. 5 5
  4568. 0 0
  4569. EXPLAIN EXTENDED SELECT * FROM C WHERE `int_key` IN (SELECT `int_nokey`);
  4570. id select_type table type possible_keys key key_len ref rows filtered Extra
  4571. 1 SIMPLE C ALL NULL NULL NULL NULL 20 100.00 Using where
  4572. DROP TABLE C;
  4573. # End of test for bug#45061.
  4574. #
  4575. # Bug #46749: Segfault in add_key_fields() with outer subquery level
  4576. # field references
  4577. #
  4578. CREATE TABLE t1 (
  4579. a int,
  4580. b int,
  4581. UNIQUE (a), KEY (b)
  4582. );
  4583. INSERT INTO t1 VALUES (1,1), (2,1);
  4584. CREATE TABLE st1 like t1;
  4585. INSERT INTO st1 VALUES (1,1), (2,1);
  4586. CREATE TABLE st2 like t1;
  4587. INSERT INTO st2 VALUES (1,1), (2,1);
  4588. EXPLAIN
  4589. SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
  4590. FROM t1
  4591. WHERE a = 230;
  4592. id select_type table type possible_keys key key_len ref rows Extra
  4593. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  4594. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  4595. SELECT MAX(b), (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
  4596. FROM t1
  4597. WHERE a = 230;
  4598. MAX(b) (SELECT COUNT(*) FROM st1,st2 WHERE st2.b <= t1.b)
  4599. NULL NULL
  4600. DROP TABLE t1, st1, st2;
  4601. #
  4602. # Bug #48709: Assertion failed in sql_select.cc:11782:
  4603. # int join_read_key(JOIN_TAB*)
  4604. #
  4605. CREATE TABLE t1 (pk int PRIMARY KEY, int_key int);
  4606. INSERT INTO t1 VALUES (10,1), (14,1);
  4607. CREATE TABLE t2 (pk int PRIMARY KEY, int_key int);
  4608. INSERT INTO t2 VALUES (3,3), (5,NULL), (7,3);
  4609. # should have eq_ref for t1
  4610. EXPLAIN
  4611. SELECT * FROM t2 outr
  4612. WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2)
  4613. ORDER BY outr.pk;
  4614. id select_type table type possible_keys key key_len ref rows Extra
  4615. x x outr ALL x x x x x x
  4616. x x t1 index x x x x x x
  4617. x x t2 index x x x x x x
  4618. # should not crash on debug binaries
  4619. SELECT * FROM t2 outr
  4620. WHERE outr.int_key NOT IN (SELECT t1.pk FROM t1, t2)
  4621. ORDER BY outr.pk;
  4622. pk int_key
  4623. 3 3
  4624. 7 3
  4625. DROP TABLE t1,t2;
  4626. #
  4627. # Bug#12329653
  4628. # EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY
  4629. #
  4630. CREATE TABLE t1(a1 int);
  4631. INSERT INTO t1 VALUES (1),(2);
  4632. SELECT @@session.sql_mode INTO @old_sql_mode;
  4633. SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
  4634. EXPLAIN EXTENDED
  4635. SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
  4636. id select_type table type possible_keys key key_len ref rows filtered Extra
  4637. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  4638. 2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00
  4639. Warnings:
  4640. Note 1003 /* select#1 */ select 1 AS `1` from `test`.`t1` where 1
  4641. SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
  4642. 1
  4643. 1
  4644. 1
  4645. PREPARE stmt FROM
  4646. 'SELECT 1 UNION ALL
  4647. SELECT 1 FROM t1
  4648. ORDER BY
  4649. (SELECT 1 FROM t1 AS t1_0
  4650. WHERE 1 < SOME (SELECT a1 FROM t1)
  4651. )' ;
  4652. EXECUTE stmt ;
  4653. ERROR 21000: Subquery returns more than 1 row
  4654. EXECUTE stmt ;
  4655. ERROR 21000: Subquery returns more than 1 row
  4656. SET SESSION sql_mode=@old_sql_mode;
  4657. DEALLOCATE PREPARE stmt;
  4658. DROP TABLE t1;
  4659. #
  4660. # Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
  4661. #
  4662. CREATE TABLE t1(a1 int);
  4663. INSERT INTO t1 VALUES (1),(2);
  4664. CREATE TABLE t2(a1 int);
  4665. INSERT INTO t2 VALUES (3);
  4666. SELECT @@session.sql_mode INTO @old_sql_mode;
  4667. SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
  4668. SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2 FROM t2);
  4669. 1
  4670. 1
  4671. 1
  4672. SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2.0 FROM t2);
  4673. 1
  4674. 1
  4675. 1
  4676. SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 'a' FROM t2);
  4677. 1
  4678. Warnings:
  4679. Warning 1292 Truncated incorrect DOUBLE value: 'a'
  4680. SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t2);
  4681. 1
  4682. 1
  4683. 1
  4684. SET SESSION sql_mode=@old_sql_mode;
  4685. DROP TABLE t1, t2;
  4686. #
  4687. # Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
  4688. #
  4689. create table t2(i int);
  4690. insert into t2 values(0);
  4691. SELECT @@session.sql_mode INTO @old_sql_mode;
  4692. SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
  4693. CREATE VIEW v1 AS
  4694. SELECT 'f' FROM t2 UNION SELECT 'x' FROM t2
  4695. ;
  4696. CREATE TABLE t1 (
  4697. pk int NOT NULL,
  4698. col_varchar_key varchar(1) DEFAULT NULL,
  4699. PRIMARY KEY (pk),
  4700. KEY col_varchar_key (col_varchar_key)
  4701. );
  4702. SELECT t1.pk
  4703. FROM t1
  4704. WHERE t1.col_varchar_key < ALL ( SELECT * FROM v1 )
  4705. ;
  4706. pk
  4707. SET SESSION sql_mode=@old_sql_mode;
  4708. drop table t2, t1;
  4709. drop view v1;
  4710. # End of 5.0 tests.
  4711. create table t_out (subcase char(3),
  4712. a1 char(2), b1 char(2), c1 char(2));
  4713. create table t_in (a2 char(2), b2 char(2), c2 char(2));
  4714. insert into t_out values ('A.1','2a', NULL, '2a');
  4715. insert into t_out values ('A.3', '2a', NULL, '2a');
  4716. insert into t_out values ('A.4', '2a', NULL, 'xx');
  4717. insert into t_out values ('B.1', '2a', '2a', '2a');
  4718. insert into t_out values ('B.2', '2a', '2a', '2a');
  4719. insert into t_out values ('B.3', '3a', 'xx', '3a');
  4720. insert into t_out values ('B.4', 'xx', '3a', '3a');
  4721. insert into t_in values ('1a', '1a', '1a');
  4722. insert into t_in values ('2a', '2a', '2a');
  4723. insert into t_in values (NULL, '2a', '2a');
  4724. insert into t_in values ('3a', NULL, '3a');
  4725. Test general IN semantics (not top-level)
  4726. case A.1
  4727. select subcase,
  4728. (a1, b1, c1) IN (select * from t_in where a2 = 'no_match') pred_in,
  4729. (a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in
  4730. from t_out where subcase = 'A.1';
  4731. subcase pred_in pred_not_in
  4732. A.1 0 1
  4733. case A.2 - impossible
  4734. case A.3
  4735. select subcase,
  4736. (a1, b1, c1) IN (select * from t_in) pred_in,
  4737. (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
  4738. from t_out where subcase = 'A.3';
  4739. subcase pred_in pred_not_in
  4740. A.3 NULL NULL
  4741. case A.4
  4742. select subcase,
  4743. (a1, b1, c1) IN (select * from t_in) pred_in,
  4744. (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
  4745. from t_out where subcase = 'A.4';
  4746. subcase pred_in pred_not_in
  4747. A.4 0 1
  4748. case B.1
  4749. select subcase,
  4750. (a1, b1, c1) IN (select * from t_in where a2 = 'no_match') pred_in,
  4751. (a1, b1, c1) NOT IN (select * from t_in where a2 = 'no_match') pred_not_in
  4752. from t_out where subcase = 'B.1';
  4753. subcase pred_in pred_not_in
  4754. B.1 0 1
  4755. case B.2
  4756. select subcase,
  4757. (a1, b1, c1) IN (select * from t_in) pred_in,
  4758. (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
  4759. from t_out where subcase = 'B.2';
  4760. subcase pred_in pred_not_in
  4761. B.2 1 0
  4762. case B.3
  4763. select subcase,
  4764. (a1, b1, c1) IN (select * from t_in) pred_in,
  4765. (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
  4766. from t_out where subcase = 'B.3';
  4767. subcase pred_in pred_not_in
  4768. B.3 NULL NULL
  4769. case B.4
  4770. select subcase,
  4771. (a1, b1, c1) IN (select * from t_in) pred_in,
  4772. (a1, b1, c1) NOT IN (select * from t_in) pred_not_in
  4773. from t_out where subcase = 'B.4';
  4774. subcase pred_in pred_not_in
  4775. B.4 0 1
  4776. Test IN as top-level predicate, and
  4777. as non-top level for cases A.3, B.3 (the only cases with NULL result).
  4778. case A.1
  4779. select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
  4780. where subcase = 'A.1' and
  4781. (a1, b1, c1) IN (select * from t_in where a1 = 'no_match');
  4782. pred_in
  4783. F
  4784. select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
  4785. where subcase = 'A.1' and
  4786. (a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match');
  4787. pred_not_in
  4788. T
  4789. select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
  4790. where subcase = 'A.1' and
  4791. NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match'));
  4792. not_pred_in
  4793. T
  4794. case A.3
  4795. select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
  4796. where subcase = 'A.3' and
  4797. (a1, b1, c1) IN (select * from t_in);
  4798. pred_in
  4799. F
  4800. select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
  4801. where subcase = 'A.3' and
  4802. (a1, b1, c1) NOT IN (select * from t_in);
  4803. pred_not_in
  4804. F
  4805. select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
  4806. where subcase = 'A.3' and
  4807. NOT((a1, b1, c1) IN (select * from t_in));
  4808. not_pred_in
  4809. F
  4810. select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out
  4811. where subcase = 'A.3' and
  4812. ((a1, b1, c1) IN (select * from t_in)) is NULL and
  4813. ((a1, b1, c1) NOT IN (select * from t_in)) is NULL;
  4814. pred_in
  4815. N
  4816. case A.4
  4817. select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
  4818. where subcase = 'A.4' and
  4819. (a1, b1, c1) IN (select * from t_in);
  4820. pred_in
  4821. F
  4822. select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
  4823. where subcase = 'A.4' and
  4824. (a1, b1, c1) NOT IN (select * from t_in);
  4825. pred_not_in
  4826. T
  4827. select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
  4828. where subcase = 'A.4' and
  4829. NOT((a1, b1, c1) IN (select * from t_in));
  4830. not_pred_in
  4831. T
  4832. case B.1
  4833. select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
  4834. where subcase = 'B.1' and
  4835. (a1, b1, c1) IN (select * from t_in where a1 = 'no_match');
  4836. pred_in
  4837. F
  4838. select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
  4839. where subcase = 'B.1' and
  4840. (a1, b1, c1) NOT IN (select * from t_in where a1 = 'no_match');
  4841. pred_not_in
  4842. T
  4843. select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
  4844. where subcase = 'B.1' and
  4845. NOT((a1, b1, c1) IN (select * from t_in where a1 = 'no_match'));
  4846. not_pred_in
  4847. T
  4848. case B.2
  4849. select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
  4850. where subcase = 'B.2' and
  4851. (a1, b1, c1) IN (select * from t_in);
  4852. pred_in
  4853. T
  4854. select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
  4855. where subcase = 'B.2' and
  4856. (a1, b1, c1) NOT IN (select * from t_in);
  4857. pred_not_in
  4858. F
  4859. select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
  4860. where subcase = 'B.2' and
  4861. NOT((a1, b1, c1) IN (select * from t_in));
  4862. not_pred_in
  4863. F
  4864. case B.3
  4865. select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
  4866. where subcase = 'B.3' and
  4867. (a1, b1, c1) IN (select * from t_in);
  4868. pred_in
  4869. F
  4870. select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
  4871. where subcase = 'B.3' and
  4872. (a1, b1, c1) NOT IN (select * from t_in);
  4873. pred_not_in
  4874. F
  4875. select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
  4876. where subcase = 'B.3' and
  4877. NOT((a1, b1, c1) IN (select * from t_in));
  4878. not_pred_in
  4879. F
  4880. select case when count(*) > 0 then 'N' else 'wrong result' end as pred_in from t_out
  4881. where subcase = 'B.3' and
  4882. ((a1, b1, c1) IN (select * from t_in)) is NULL and
  4883. ((a1, b1, c1) NOT IN (select * from t_in)) is NULL;
  4884. pred_in
  4885. N
  4886. case B.4
  4887. select case when count(*) > 0 then 'T' else 'F' end as pred_in from t_out
  4888. where subcase = 'B.4' and
  4889. (a1, b1, c1) IN (select * from t_in);
  4890. pred_in
  4891. F
  4892. select case when count(*) > 0 then 'T' else 'F' end as pred_not_in from t_out
  4893. where subcase = 'B.4' and
  4894. (a1, b1, c1) NOT IN (select * from t_in);
  4895. pred_not_in
  4896. T
  4897. select case when count(*) > 0 then 'T' else 'F' end as not_pred_in from t_out
  4898. where subcase = 'B.4' and
  4899. NOT((a1, b1, c1) IN (select * from t_in));
  4900. not_pred_in
  4901. T
  4902. drop table t_out;
  4903. drop table t_in;
  4904. CREATE TABLE t1 (a INT, b INT);
  4905. INSERT INTO t1 VALUES (2,22),(1,11),(2,22);
  4906. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
  4907. a
  4908. 1
  4909. 2
  4910. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
  4911. a
  4912. SELECT a FROM t1 t0
  4913. WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
  4914. a
  4915. 1
  4916. 2
  4917. SET @@sql_mode='ansi';
  4918. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 0 GROUP BY a;
  4919. ERROR HY000: Invalid use of group function
  4920. SELECT a FROM t1 WHERE (SELECT COUNT(b) FROM DUAL) > 1 GROUP BY a;
  4921. ERROR HY000: Invalid use of group function
  4922. SELECT a FROM t1 t0
  4923. WHERE (SELECT COUNT(t0.b) FROM t1 t WHERE t.b>20) GROUP BY a;
  4924. ERROR HY000: Invalid use of group function
  4925. SET @@sql_mode=default;
  4926. DROP TABLE t1;
  4927. CREATE TABLE t1 (s1 CHAR(1));
  4928. INSERT INTO t1 VALUES ('a');
  4929. SELECT * FROM t1 WHERE _utf8'a' = ANY (SELECT s1 FROM t1);
  4930. s1
  4931. a
  4932. DROP TABLE t1;
  4933. CREATE TABLE t1(c INT, KEY(c));
  4934. CREATE TABLE t2(a INT, b INT);
  4935. INSERT INTO t2 VALUES (1, 10), (2, NULL);
  4936. INSERT INTO t1 VALUES (1), (3);
  4937. SELECT * FROM t2 WHERE b NOT IN (SELECT max(t.c) FROM t1, t1 t WHERE t.c>10);
  4938. a b
  4939. DROP TABLE t1,t2;
  4940. CREATE TABLE t1(pk INT PRIMARY KEY, a INT, INDEX idx(a));
  4941. INSERT INTO t1 VALUES (1, 10), (3, 30), (2, 20);
  4942. CREATE TABLE t2(pk INT PRIMARY KEY, a INT, b INT, INDEX idxa(a));
  4943. INSERT INTO t2 VALUES (2, 20, 700), (1, 10, 200), (4, 10, 100);
  4944. SELECT * FROM t1
  4945. WHERE EXISTS (SELECT DISTINCT a FROM t2 WHERE t1.a < t2.a ORDER BY b);
  4946. pk a
  4947. 1 10
  4948. DROP TABLE t1,t2;
  4949. CREATE TABLE t1 (a INT, b INT, PRIMARY KEY (a), KEY b (b));
  4950. INSERT INTO t1 VALUES (1,NULL), (9,NULL);
  4951. CREATE TABLE t2 (
  4952. a INT,
  4953. b INT,
  4954. c INT,
  4955. d INT,
  4956. PRIMARY KEY (a),
  4957. UNIQUE KEY b (b,c,d),
  4958. KEY b_2 (b),
  4959. KEY c (c),
  4960. KEY d (d)
  4961. );
  4962. INSERT INTO t2 VALUES
  4963. (43, 2, 11 ,30),
  4964. (44, 2, 12 ,30),
  4965. (45, 1, 1 ,10000),
  4966. (46, 1, 2 ,10000),
  4967. (556,1, 32 ,10000);
  4968. CREATE TABLE t3 (
  4969. a INT,
  4970. b INT,
  4971. c INT,
  4972. PRIMARY KEY (a),
  4973. UNIQUE KEY b (b,c),
  4974. KEY c (c),
  4975. KEY b_2 (b)
  4976. );
  4977. INSERT INTO t3 VALUES (1,1,1), (2,32,1), (3,33,1), (4,34,2);
  4978. explain
  4979. 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;
  4980. id select_type table type possible_keys key key_len ref rows Extra
  4981. 1 PRIMARY t1 index PRIMARY PRIMARY 4 NULL 2 Using index
  4982. 1 PRIMARY t3 ref b,b_2 b 5 test.t1.a 1 Using index
  4983. 2 DEPENDENT SUBQUERY t2 ref b,b_2,c b 10 test.t3.c,test.t1.a 1 Using where; Using index
  4984. 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;
  4985. a incorrect
  4986. 1 1
  4987. DROP TABLE t1,t2,t3;
  4988. CREATE TABLE t1 (id int);
  4989. CREATE TABLE t2 (id int, c int);
  4990. INSERT INTO t1 (id) VALUES (1);
  4991. INSERT INTO t2 (id) VALUES (1);
  4992. INSERT INTO t1 (id) VALUES (1);
  4993. INSERT INTO t2 (id) VALUES (1);
  4994. CREATE VIEW v1 AS
  4995. SELECT t2.c AS c FROM t1, t2
  4996. WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
  4997. UPDATE v1 SET c=1;
  4998. CREATE VIEW v2 (a,b) AS
  4999. SELECT t2.id, t2.c AS c FROM t1, t2
  5000. WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
  5001. INSERT INTO v2(a,b) VALUES (2,2);
  5002. ERROR 44000: CHECK OPTION failed `test`.`v2`
  5003. SELECT * FROM v1;
  5004. c
  5005. 1
  5006. 1
  5007. 1
  5008. 1
  5009. CREATE VIEW v3 AS
  5010. SELECT t2.c AS c FROM t2
  5011. WHERE 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
  5012. DELETE FROM v3;
  5013. DROP VIEW v1,v2,v3;
  5014. DROP TABLE t1,t2;
  5015. #
  5016. # BUG#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result
  5017. #
  5018. create table t1(id integer primary key, g integer, v integer, s char(1));
  5019. create table t2(id integer primary key, g integer, v integer, s char(1));
  5020. insert into t1 values
  5021. (10, 10, 10, 'l'),
  5022. (20, 20, 20, 'l'),
  5023. (40, 40, 40, 'l'),
  5024. (41, 40, null, 'l'),
  5025. (50, 50, 50, 'l'),
  5026. (51, 50, null, 'l'),
  5027. (60, 60, 60, 'l'),
  5028. (61, 60, null, 'l'),
  5029. (70, 70, 70, 'l'),
  5030. (90, 90, null, 'l');
  5031. insert into t2 values
  5032. (10, 10, 10, 'r'),
  5033. (30, 30, 30, 'r'),
  5034. (50, 50, 50, 'r'),
  5035. (60, 60, 60, 'r'),
  5036. (61, 60, null, 'r'),
  5037. (70, 70, 70, 'r'),
  5038. (71, 70, null, 'r'),
  5039. (80, 80, 80, 'r'),
  5040. (81, 80, null, 'r'),
  5041. (100,100,null, 'r');
  5042. select *
  5043. from t1
  5044. where v in(select v
  5045. from t2
  5046. where t1.g=t2.g) is unknown;
  5047. id g v s
  5048. 51 50 NULL l
  5049. 61 60 NULL l
  5050. drop table t1, t2;
  5051. #
  5052. # Bug#37822 Correlated subquery with IN and IS UNKNOWN provides wrong result
  5053. #
  5054. create table t1(id integer primary key, g integer, v integer, s char(1));
  5055. create table t2(id integer primary key, g integer, v integer, s char(1));
  5056. insert into t1 values
  5057. (10, 10, 10, 'l'),
  5058. (20, 20, 20, 'l'),
  5059. (40, 40, 40, 'l'),
  5060. (41, 40, null, 'l'),
  5061. (50, 50, 50, 'l'),
  5062. (51, 50, null, 'l'),
  5063. (60, 60, 60, 'l'),
  5064. (61, 60, null, 'l'),
  5065. (70, 70, 70, 'l'),
  5066. (90, 90, null, 'l');
  5067. insert into t2 values
  5068. (10, 10, 10, 'r'),
  5069. (30, 30, 30, 'r'),
  5070. (50, 50, 50, 'r'),
  5071. (60, 60, 60, 'r'),
  5072. (61, 60, null, 'r'),
  5073. (70, 70, 70, 'r'),
  5074. (71, 70, null, 'r'),
  5075. (80, 80, 80, 'r'),
  5076. (81, 80, null, 'r'),
  5077. (100,100,null, 'r');
  5078. select *
  5079. from t1
  5080. where v in(select v
  5081. from t2
  5082. where t1.g=t2.g) is unknown;
  5083. id g v s
  5084. 51 50 NULL l
  5085. 61 60 NULL l
  5086. drop table t1, t2;
  5087. #
  5088. # Bug#33204: INTO is allowed in subselect, causing inconsistent results
  5089. #
  5090. CREATE TABLE t1( a INT );
  5091. INSERT INTO t1 VALUES (1),(2);
  5092. CREATE TABLE t2( a INT, b INT );
  5093. SELECT *
  5094. FROM (SELECT a INTO @var FROM t1 WHERE a = 2) t1a;
  5095. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @var FROM t1 WHERE a = 2) t1a' at line 2
  5096. SELECT *
  5097. FROM (SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2) t1a;
  5098. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' FROM t1 WHERE a = 2) t1a' at line 2
  5099. SELECT *
  5100. FROM (SELECT a INTO DUMPFILE 'file' FROM t1 WHERE a = 2) t1a;
  5101. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' FROM t1 WHERE a = 2) t1a' at line 2
  5102. SELECT * FROM (
  5103. SELECT 1 a
  5104. UNION
  5105. SELECT a INTO @var FROM t1 WHERE a = 2
  5106. ) t1a;
  5107. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @var FROM t1 WHERE a = 2
  5108. ) t1a' at line 4
  5109. SELECT * FROM (
  5110. SELECT 1 a
  5111. UNION
  5112. SELECT a INTO OUTFILE 'file' FROM t1 WHERE a = 2
  5113. ) t1a;
  5114. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' FROM t1 WHERE a = 2
  5115. ) t1a' at line 4
  5116. SELECT * FROM (
  5117. SELECT 1 a
  5118. UNION
  5119. SELECT a INTO DUMPFILE 'file' FROM t1 WHERE a = 2
  5120. ) t1a;
  5121. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' FROM t1 WHERE a = 2
  5122. ) t1a' at line 4
  5123. SELECT * FROM (SELECT a FROM t1 WHERE a = 2) t1a;
  5124. a
  5125. 2
  5126. SELECT * FROM (
  5127. SELECT a FROM t1 WHERE a = 2
  5128. UNION
  5129. SELECT a FROM t1 WHERE a = 2
  5130. ) t1a;
  5131. a
  5132. 2
  5133. SELECT * FROM (
  5134. SELECT 1 a
  5135. UNION
  5136. SELECT a FROM t1 WHERE a = 2
  5137. UNION
  5138. SELECT a FROM t1 WHERE a = 2
  5139. ) t1a;
  5140. a
  5141. 1
  5142. 2
  5143. SELECT * FROM ((SELECT 1 a) UNION SELECT 1 a) q;
  5144. a
  5145. 1
  5146. SELECT * FROM (SELECT 1 a UNION (SELECT 1 a)) alias;
  5147. a
  5148. 1
  5149. SELECT * FROM (SELECT 1 UNION SELECT 1) t1a;
  5150. 1
  5151. 1
  5152. SELECT * FROM ((SELECT 1 a INTO @a)) t1a;
  5153. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a)) t1a' at line 1
  5154. SELECT * FROM ((SELECT 1 a INTO OUTFILE 'file' )) t1a;
  5155. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )) t1a' at line 1
  5156. SELECT * FROM ((SELECT 1 a INTO DUMPFILE 'file' )) t1a;
  5157. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )) t1a' at line 1
  5158. SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO @a)) t1a;
  5159. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a)) t1a' at line 1
  5160. SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO DUMPFILE 'file' )) t1a;
  5161. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )) t1a' at line 1
  5162. SELECT * FROM (SELECT 1 a UNION (SELECT 1 a INTO OUTFILE 'file' )) t1a;
  5163. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )) t1a' at line 1
  5164. SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO @a))) t1a;
  5165. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a))) t1a' at line 1
  5166. SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO DUMPFILE 'file' ))) t1a;
  5167. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' ))) t1a' at line 1
  5168. SELECT * FROM (SELECT 1 a UNION ((SELECT 1 a INTO OUTFILE 'file' ))) t1a;
  5169. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' ))) t1a' at line 1
  5170. SELECT * FROM (SELECT 1 a ORDER BY a) t1a;
  5171. a
  5172. 1
  5173. SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a) t1a;
  5174. a
  5175. 1
  5176. SELECT * FROM (SELECT 1 a UNION SELECT 1 a LIMIT 1) t1a;
  5177. a
  5178. 1
  5179. SELECT * FROM (SELECT 1 a UNION SELECT 1 a ORDER BY a LIMIT 1) t1a;
  5180. a
  5181. 1
  5182. SELECT * FROM t1 JOIN (SELECT 1 UNION SELECT 1) alias ON 1;
  5183. a 1
  5184. 1 1
  5185. 2 1
  5186. SELECT * FROM t1 JOIN ((SELECT 1 UNION SELECT 1)) ON 1;
  5187. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ON 1' at line 1
  5188. SELECT * FROM t1 JOIN (t1 t1a UNION SELECT 1) ON 1;
  5189. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1) ON 1' at line 1
  5190. SELECT * FROM t1 JOIN ((t1 t1a UNION SELECT 1)) ON 1;
  5191. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1)) ON 1' at line 1
  5192. SELECT * FROM t1 JOIN (t1 t1a) t1a ON 1;
  5193. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') t1a ON 1' at line 1
  5194. SELECT * FROM t1 JOIN ((t1 t1a)) t1a ON 1;
  5195. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')) t1a ON 1' at line 1
  5196. SELECT * FROM t1 JOIN (t1 t1a) ON 1;
  5197. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') ON 1' at line 1
  5198. SELECT * FROM t1 JOIN ((t1 t1a)) ON 1;
  5199. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')) ON 1' at line 1
  5200. SELECT * FROM (t1 t1a);
  5201. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1
  5202. SELECT * FROM ((t1 t1a));
  5203. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '))' at line 1
  5204. SELECT * FROM t1 JOIN (SELECT 1 t1a) alias ON 1;
  5205. a t1a
  5206. 1 1
  5207. 2 1
  5208. SELECT * FROM t1 JOIN ((SELECT 1 t1a)) alias ON 1;
  5209. a t1a
  5210. 1 1
  5211. 2 1
  5212. SELECT * FROM t1 JOIN (SELECT 1 a) a ON 1;
  5213. a a
  5214. 1 1
  5215. 2 1
  5216. SELECT * FROM t1 JOIN ((SELECT 1 a)) a ON 1;
  5217. a a
  5218. 1 1
  5219. 2 1
  5220. SELECT * FROM (t1 JOIN (SELECT 1) t1a1 ON 1) t1a2;
  5221. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 't1a2' at line 1
  5222. SELECT * FROM t1 WHERE a = ALL ( SELECT 1 );
  5223. a
  5224. 1
  5225. SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 );
  5226. a
  5227. 1
  5228. SELECT * FROM t1 WHERE a = ANY ( SELECT 3 UNION SELECT 1 );
  5229. a
  5230. 1
  5231. SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO @a);
  5232. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a)' at line 1
  5233. SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
  5234. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1
  5235. SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
  5236. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1
  5237. SELECT * FROM t1 WHERE a = ( SELECT 1 );
  5238. a
  5239. 1
  5240. SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 );
  5241. a
  5242. 1
  5243. SELECT * FROM t1 WHERE a = ( SELECT 1 INTO @a);
  5244. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a)' at line 1
  5245. SELECT * FROM t1 WHERE a = ( SELECT 1 INTO OUTFILE 'file' );
  5246. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1
  5247. SELECT * FROM t1 WHERE a = ( SELECT 1 INTO DUMPFILE 'file' );
  5248. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1
  5249. SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO @a);
  5250. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @a)' at line 1
  5251. SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
  5252. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1
  5253. SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
  5254. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1
  5255. SELECT ( SELECT 1 INTO @v );
  5256. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @v )' at line 1
  5257. SELECT ( SELECT 1 INTO OUTFILE 'file' );
  5258. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1
  5259. SELECT ( SELECT 1 INTO DUMPFILE 'file' );
  5260. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1
  5261. SELECT ( SELECT 1 UNION SELECT 1 INTO @v );
  5262. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @v )' at line 1
  5263. SELECT ( SELECT 1 UNION SELECT 1 INTO OUTFILE 'file' );
  5264. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO OUTFILE 'file' )' at line 1
  5265. SELECT ( SELECT 1 UNION SELECT 1 INTO DUMPFILE 'file' );
  5266. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO DUMPFILE 'file' )' at line 1
  5267. SELECT ( SELECT a FROM t1 WHERE a = 1 ), a FROM t1;
  5268. ( SELECT a FROM t1 WHERE a = 1 ) a
  5269. 1 1
  5270. 1 2
  5271. SELECT ( SELECT a FROM t1 WHERE a = 1 UNION SELECT 1 ), a FROM t1;
  5272. ( SELECT a FROM t1 WHERE a = 1 UNION SELECT 1 ) a
  5273. 1 1
  5274. 1 2
  5275. SELECT * FROM t2 WHERE (a, b) IN (SELECT a, b FROM t2);
  5276. a b
  5277. SELECT 1 UNION ( SELECT 1 UNION SELECT 1 );
  5278. 1
  5279. 1
  5280. ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1;
  5281. 1
  5282. 1
  5283. SELECT ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
  5284. ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) )
  5285. 1
  5286. SELECT ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1;
  5287. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1' at line 1
  5288. SELECT ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
  5289. ( SELECT 1 UNION SELECT 1 UNION SELECT 1 )
  5290. 1
  5291. SELECT ((SELECT 1 UNION SELECT 1 UNION SELECT 1));
  5292. ((SELECT 1 UNION SELECT 1 UNION SELECT 1))
  5293. 1
  5294. SELECT * FROM ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
  5295. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1
  5296. SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
  5297. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1
  5298. SELECT * FROM ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 ) a;
  5299. 1
  5300. 1
  5301. SELECT * FROM ( SELECT 1 UNION SELECT 1 UNION SELECT 1 ) a;
  5302. 1
  5303. 1
  5304. SELECT * FROM t1 WHERE a = ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
  5305. a
  5306. 1
  5307. SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
  5308. a
  5309. 1
  5310. SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
  5311. a
  5312. 1
  5313. SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION ( SELECT 1 UNION SELECT 1 ) );
  5314. a
  5315. 1
  5316. SELECT * FROM t1 WHERE a = ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
  5317. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 )' at line 1
  5318. SELECT * FROM t1 WHERE a = ALL ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
  5319. a
  5320. 1
  5321. SELECT * FROM t1 WHERE a = ANY ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
  5322. a
  5323. 1
  5324. SELECT * FROM t1 WHERE a IN ( ( SELECT 1 UNION SELECT 1 ) UNION SELECT 1 );
  5325. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNION SELECT 1 )' at line 1
  5326. SELECT * FROM t1 WHERE a = ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
  5327. a
  5328. 1
  5329. SELECT * FROM t1 WHERE a = ALL ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
  5330. a
  5331. 1
  5332. SELECT * FROM t1 WHERE a = ANY ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
  5333. a
  5334. 1
  5335. SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION SELECT 1 UNION SELECT 1 );
  5336. a
  5337. 1
  5338. SELECT * FROM t1 WHERE EXISTS ( SELECT 1 UNION SELECT 1 INTO @v );
  5339. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @v )' at line 1
  5340. SELECT EXISTS(SELECT 1+1);
  5341. EXISTS(SELECT 1+1)
  5342. 1
  5343. SELECT EXISTS(SELECT 1+1 INTO @test);
  5344. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @test)' at line 1
  5345. SELECT * FROM t1 WHERE a IN ( SELECT 1 UNION SELECT 1 INTO @v );
  5346. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @v )' at line 1
  5347. SELECT * FROM t1 WHERE EXISTS ( SELECT 1 INTO @v );
  5348. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @v )' at line 1
  5349. SELECT * FROM t1 WHERE a IN ( SELECT 1 INTO @v );
  5350. ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INTO @v )' at line 1
  5351. DROP TABLE t1, t2;
  5352. CREATE TABLE t1 (a ENUM('rainbow'));
  5353. INSERT INTO t1 VALUES (),(),(),(),();
  5354. SELECT 1 FROM t1 GROUP BY (SELECT 1 FROM t1 ORDER BY AVG(LAST_INSERT_ID()));
  5355. 1
  5356. 1
  5357. DROP TABLE t1;
  5358. CREATE TABLE t1 (a LONGBLOB);
  5359. INSERT INTO t1 SET a = 'aaaa';
  5360. INSERT INTO t1 SET a = 'aaaa';
  5361. SELECT 1 FROM t1 GROUP BY
  5362. (SELECT LAST_INSERT_ID() FROM t1 ORDER BY MIN(a) ASC LIMIT 1);
  5363. 1
  5364. 1
  5365. DROP TABLE t1;
  5366. #
  5367. # Bug #49512 : subquery with aggregate function crash
  5368. # subselect_single_select_engine::exec()
  5369. CREATE TABLE t1(a INT);
  5370. INSERT INTO t1 VALUES();
  5371. # should not crash
  5372. SELECT 1 FROM t1 WHERE a <> SOME
  5373. (
  5374. SELECT MAX((SELECT a FROM t1 LIMIT 1)) AS d
  5375. FROM t1,t1 a
  5376. );
  5377. 1
  5378. DROP TABLE t1;
  5379. #
  5380. # Bug #45989 take 2 : memory leak after explain encounters an
  5381. # error in the query
  5382. #
  5383. CREATE TABLE t1(a LONGTEXT);
  5384. INSERT INTO t1 VALUES (repeat('a',@@global.max_allowed_packet));
  5385. INSERT INTO t1 VALUES (repeat('b',@@global.max_allowed_packet));
  5386. EXPLAIN EXTENDED SELECT DISTINCT 1 FROM t1,
  5387. (SELECT DISTINCTROW a AS away FROM t1 GROUP BY a WITH ROLLUP) AS d1
  5388. WHERE t1.a = d1.a;
  5389. ERROR 42S22: Unknown column 'd1.a' in 'where clause'
  5390. DROP TABLE t1;
  5391. Set up test tables.
  5392. CREATE TABLE t1 (
  5393. t1_id INT UNSIGNED,
  5394. PRIMARY KEY(t1_id)
  5395. ) Engine=MyISAM;
  5396. INSERT INTO t1 (t1_id) VALUES (1), (2), (3), (4), (5);
  5397. CREATE TABLE t2 SELECT * FROM t1;
  5398. CREATE TABLE t3 (
  5399. t3_id INT UNSIGNED AUTO_INCREMENT,
  5400. t1_id INT UNSIGNED,
  5401. amount DECIMAL(16,2),
  5402. PRIMARY KEY(t3_id),
  5403. KEY(t1_id)
  5404. ) Engine=MyISAM;
  5405. INSERT INTO t3 (t1_id, t3_id, amount)
  5406. VALUES (1, 1, 100.00), (2, 2, 200.00), (4, 4, 400.00);
  5407. This is the 'inner query' running by itself.
  5408. Produces correct results.
  5409. SELECT
  5410. t1.t1_id,
  5411. IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
  5412. FROM
  5413. t1
  5414. LEFT JOIN t2 ON t2.t1_id=t1.t1_id
  5415. GROUP BY
  5416. t1.t1_id
  5417. ;
  5418. t1_id total_amount
  5419. 1 100.00
  5420. 2 200.00
  5421. 3 0.00
  5422. 4 400.00
  5423. 5 0.00
  5424. SELECT * FROM (the same inner query)
  5425. Produces correct results.
  5426. SELECT * FROM (
  5427. SELECT
  5428. t1.t1_id,
  5429. IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
  5430. FROM
  5431. t1
  5432. LEFT JOIN t2 ON t2.t1_id=t1.t1_id
  5433. GROUP BY
  5434. t1.t1_id
  5435. ) AS t;
  5436. t1_id total_amount
  5437. 1 100.00
  5438. 2 200.00
  5439. 3 0.00
  5440. 4 400.00
  5441. 5 0.00
  5442. Now make t2.t1_id part of a key.
  5443. ALTER TABLE t2 ADD PRIMARY KEY(t1_id);
  5444. Same inner query by itself.
  5445. Still correct results.
  5446. SELECT
  5447. t1.t1_id,
  5448. IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
  5449. FROM
  5450. t1
  5451. LEFT JOIN t2 ON t2.t1_id=t1.t1_id
  5452. GROUP BY
  5453. t1.t1_id;
  5454. t1_id total_amount
  5455. 1 100.00
  5456. 2 200.00
  5457. 3 0.00
  5458. 4 400.00
  5459. 5 0.00
  5460. SELECT * FROM (the same inner query), now with indexes on the LEFT JOIN
  5461. SELECT * FROM (
  5462. SELECT
  5463. t1.t1_id,
  5464. IFNULL((SELECT SUM(amount) FROM t3 WHERE t3.t1_id=t1.t1_id), 0) AS total_amount
  5465. FROM
  5466. t1
  5467. LEFT JOIN t2 ON t2.t1_id=t1.t1_id
  5468. GROUP BY
  5469. t1.t1_id
  5470. ) AS t;
  5471. t1_id total_amount
  5472. 1 100.00
  5473. 2 200.00
  5474. 3 0.00
  5475. 4 400.00
  5476. 5 0.00
  5477. DROP TABLE t3;
  5478. DROP TABLE t2;
  5479. DROP TABLE t1;
  5480. #
  5481. # Bug #52711: Segfault when doing EXPLAIN SELECT with
  5482. # union...order by (select... where...)
  5483. #
  5484. CREATE TABLE t1 (a VARCHAR(10), FULLTEXT KEY a (a));
  5485. INSERT INTO t1 VALUES (1),(2);
  5486. CREATE TABLE t2 (b INT);
  5487. INSERT INTO t2 VALUES (1),(2);
  5488. # Should not crash
  5489. EXPLAIN
  5490. SELECT * FROM t2 UNION SELECT * FROM t2
  5491. ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE));
  5492. # Should not crash
  5493. SELECT * FROM t2 UNION SELECT * FROM t2
  5494. ORDER BY (SELECT * FROM t1 WHERE MATCH(a) AGAINST ('+abc' IN BOOLEAN MODE));
  5495. DROP TABLE t1,t2;
  5496. #
  5497. # Bug #58818: Incorrect result for IN/ANY subquery
  5498. # with HAVING condition
  5499. #
  5500. CREATE TABLE t1(i INT);
  5501. INSERT INTO t1 VALUES (1), (2), (3);
  5502. CREATE TABLE t1s(i INT);
  5503. INSERT INTO t1s VALUES (10), (20), (30);
  5504. CREATE TABLE t2s(i INT);
  5505. INSERT INTO t2s VALUES (100), (200), (300);
  5506. SELECT * FROM t1
  5507. WHERE t1.i NOT IN
  5508. (
  5509. SELECT STRAIGHT_JOIN t2s.i
  5510. FROM
  5511. t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
  5512. HAVING t2s.i = 999
  5513. );
  5514. i
  5515. 1
  5516. 2
  5517. 3
  5518. SELECT * FROM t1
  5519. WHERE t1.I IN
  5520. (
  5521. SELECT STRAIGHT_JOIN t2s.i
  5522. FROM
  5523. t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
  5524. HAVING t2s.i = 999
  5525. ) IS UNKNOWN;
  5526. i
  5527. SELECT * FROM t1
  5528. WHERE NOT t1.I = ANY
  5529. (
  5530. SELECT STRAIGHT_JOIN t2s.i
  5531. FROM
  5532. t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
  5533. HAVING t2s.i = 999
  5534. );
  5535. i
  5536. 1
  5537. 2
  5538. 3
  5539. SELECT * FROM t1
  5540. WHERE t1.i = ANY (
  5541. SELECT STRAIGHT_JOIN t2s.i
  5542. FROM
  5543. t1s LEFT OUTER JOIN t2s ON t2s.i = t1s.i
  5544. HAVING t2s.i = 999
  5545. ) IS UNKNOWN;
  5546. i
  5547. DROP TABLE t1,t1s,t2s;
  5548. # LP BUG#675248 - select->prep_where references on freed memory
  5549. CREATE TABLE t1 (a int, b int);
  5550. insert into t1 values (1,1),(0,0);
  5551. CREATE TABLE t2 (c int);
  5552. insert into t2 values (1),(2);
  5553. prepare stmt1 from "select sum(a),(select sum(c) from t2 where table1.b) as sub
  5554. from t1 as table1 group by sub";
  5555. execute stmt1;
  5556. sum(a) sub
  5557. 0 NULL
  5558. 1 3
  5559. deallocate prepare stmt1;
  5560. prepare stmt1 from "select sum(a),(select sum(c) from t2 having table1.b) as sub
  5561. from t1 as table1";
  5562. execute stmt1;
  5563. sum(a) sub
  5564. 1 3
  5565. deallocate prepare stmt1;
  5566. drop table t1,t2;
  5567. #
  5568. # Bug LP#693935/#58727: Assertion failure with
  5569. # a single row subquery returning more than one row
  5570. #
  5571. create table t1 (a char(1) charset utf8);
  5572. insert into t1 values ('a'), ('b');
  5573. create table t2 (a binary(1));
  5574. insert into t2 values ('x'), ('y');
  5575. select * from t2 where a=(select a from t1) and a='x';
  5576. ERROR 21000: Subquery returns more than 1 row
  5577. drop table t1,t2;
  5578. # End of 5.1 tests
  5579. #
  5580. # Bug #11765713 58705:
  5581. # OPTIMIZER LET ENGINE DEPEND ON UNINITIALIZED VALUES
  5582. # CREATED BY OPT_SUM_QUERY
  5583. #
  5584. CREATE TABLE t1(a INT NOT NULL, KEY (a));
  5585. INSERT INTO t1 VALUES (0), (1);
  5586. SELECT 1 as foo FROM t1 WHERE a < SOME
  5587. (SELECT a FROM t1 WHERE a <=>
  5588. (SELECT a FROM t1)
  5589. );
  5590. ERROR 21000: Subquery returns more than 1 row
  5591. SELECT 1 as foo FROM t1 WHERE a < SOME
  5592. (SELECT a FROM t1 WHERE a <=>
  5593. (SELECT a FROM t1 where a is null)
  5594. );
  5595. foo
  5596. DROP TABLE t1;
  5597. #
  5598. # Bug #57704: Cleanup code dies with void TABLE::set_keyread(bool):
  5599. # Assertion `file' failed.
  5600. #
  5601. CREATE TABLE t1 (a INT);
  5602. SELECT 1 FROM
  5603. (SELECT ROW(
  5604. (SELECT 1 FROM t1 RIGHT JOIN
  5605. (SELECT 1 FROM t1, t1 t2) AS d ON 1),
  5606. 1) FROM t1) AS e;
  5607. ERROR 21000: Operand should contain 1 column(s)
  5608. DROP TABLE t1;
  5609. #
  5610. # Bug#13721076 CRASH WITH TIME TYPE/TIMESTAMP() AND WARNINGS IN SUBQUERY
  5611. #
  5612. CREATE TABLE t1(a TIME NOT NULL);
  5613. INSERT INTO t1 VALUES ('00:00:32');
  5614. SELECT 1 FROM t1 WHERE a >
  5615. (SELECT timestamp(a) AS a FROM t1);
  5616. 1
  5617. DROP TABLE t1;
  5618. #
  5619. # No BUG#, a case brought from 5.2's innodb_mysql_lock.test
  5620. #
  5621. create table t1 (i int not null primary key);
  5622. insert into t1 values (1),(2),(3),(4),(5);
  5623. create table t2 (j int not null primary key);
  5624. insert into t2 values (1),(2),(3),(4),(5);
  5625. create table t3 (k int not null primary key);
  5626. insert into t3 values (1),(2),(3);
  5627. create view v2 as select t2.j as j from t2 where t2.j in (select t1.i from t1);
  5628. select * from t3 where k in (select j from v2);
  5629. k
  5630. 1
  5631. 2
  5632. 3
  5633. drop table t1,t2,t3;
  5634. drop view v2;
  5635. #
  5636. # Bug#52068: Optimizer generates invalid semijoin materialization plan
  5637. #
  5638. drop table if exists ot1, ot2, it1, it2;
  5639. CREATE TABLE ot1(a INTEGER);
  5640. INSERT INTO ot1 VALUES(5), (8);
  5641. CREATE TABLE it2(a INTEGER);
  5642. INSERT INTO it2 VALUES(9), (5), (1), (8);
  5643. CREATE TABLE it3(a INTEGER);
  5644. INSERT INTO it3 VALUES(7), (1), (0), (5), (1), (4);
  5645. CREATE TABLE ot4(a INTEGER);
  5646. INSERT INTO ot4 VALUES(1), (3), (5), (7), (9), (7), (3), (1);
  5647. SELECT * FROM ot1,ot4
  5648. WHERE (ot1.a,ot4.a) IN (SELECT it2.a,it3.a
  5649. FROM it2,it3);
  5650. a a
  5651. 5 1
  5652. 8 1
  5653. 5 5
  5654. 8 5
  5655. 5 7
  5656. 8 7
  5657. 5 7
  5658. 8 7
  5659. 5 1
  5660. 8 1
  5661. explain SELECT * FROM ot1,ot4
  5662. WHERE (ot1.a,ot4.a) IN (SELECT it2.a,it3.a
  5663. FROM it2,it3);
  5664. id select_type table type possible_keys key key_len ref rows Extra
  5665. 1 PRIMARY ot1 ALL NULL NULL NULL NULL 2
  5666. 1 PRIMARY ot4 ALL NULL NULL NULL NULL 8 Using where; Using join buffer (flat, BNL join)
  5667. 2 MATERIALIZED it2 ALL NULL NULL NULL NULL 4
  5668. 2 MATERIALIZED it3 ALL NULL NULL NULL NULL 6 Using join buffer (flat, BNL join)
  5669. DROP TABLE IF EXISTS ot1, ot4, it2, it3;
  5670. #
  5671. # Bug#729039: NULL keys used to evaluate subquery
  5672. #
  5673. CREATE TABLE t1 (a int) ;
  5674. INSERT INTO t1 VALUES (NULL), (1), (NULL), (2);
  5675. CREATE TABLE t2 (a int, INDEX idx(a)) ;
  5676. INSERT INTO t2 VALUES (NULL), (1), (NULL);
  5677. SELECT * FROM t1
  5678. WHERE EXISTS (SELECT a FROM t2 USE INDEX () WHERE t2.a = t1.a);
  5679. a
  5680. 1
  5681. EXPLAIN
  5682. SELECT * FROM t1
  5683. WHERE EXISTS (SELECT a FROM t2 USE INDEX() WHERE t2.a = t1.a);
  5684. id select_type table type possible_keys key key_len ref rows Extra
  5685. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where
  5686. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3
  5687. SELECT * FROM t1
  5688. WHERE EXISTS (SELECT a FROM t2 WHERE t2.a = t1.a);
  5689. a
  5690. 1
  5691. EXPLAIN
  5692. SELECT * FROM t1
  5693. WHERE EXISTS (SELECT a FROM t2 WHERE t2.a = t1.a);
  5694. id select_type table type possible_keys key key_len ref rows Extra
  5695. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where
  5696. 2 MATERIALIZED t2 index idx idx 5 NULL 3 Using index
  5697. DROP TABLE t1,t2;
  5698. #
  5699. # BUG#752992: Wrong results for a subquery with 'semijoin=on'
  5700. #
  5701. CREATE TABLE t1 (pk INTEGER PRIMARY KEY, i INTEGER NOT NULL);
  5702. INSERT INTO t1 VALUES (11,0);
  5703. INSERT INTO t1 VALUES (12,5);
  5704. INSERT INTO t1 VALUES (15,0);
  5705. CREATE TABLE t2 (pk INTEGER PRIMARY KEY, i INTEGER NOT NULL);
  5706. INSERT INTO t2 VALUES (11,1);
  5707. INSERT INTO t2 VALUES (12,2);
  5708. INSERT INTO t2 VALUES (15,4);
  5709. SET @save_join_cache_level=@@join_cache_level;
  5710. SET join_cache_level=0;
  5711. EXPLAIN SELECT * FROM t1 WHERE pk IN (SELECT it.pk FROM t2 JOIN t2 AS it ON 1);
  5712. id select_type table type possible_keys key key_len ref rows Extra
  5713. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
  5714. 2 MATERIALIZED t2 index NULL PRIMARY 4 NULL 3 Using index
  5715. 2 MATERIALIZED it index PRIMARY PRIMARY 4 NULL 3 Using index
  5716. SELECT * FROM t1 WHERE pk IN (SELECT it.pk FROM t2 JOIN t2 AS it ON 1);
  5717. pk i
  5718. 11 0
  5719. 12 5
  5720. 15 0
  5721. SET join_cache_level=@save_join_cache_level;
  5722. DROP table t1,t2;
  5723. #
  5724. # Bug#751350: crash with pushed condition for outer references when
  5725. # there should be none of such conditions
  5726. #
  5727. CREATE TABLE t1 (a int, b int) ;
  5728. INSERT INTO t1 VALUES (0,0),(0,0);
  5729. set @optimizer_switch_save=@@optimizer_switch;
  5730. set @@optimizer_switch='semijoin=off,materialization=on,in_to_exists=on';
  5731. EXPLAIN
  5732. SELECT b FROM t1
  5733. WHERE ('0') IN ( SELECT a FROM t1 GROUP BY a )
  5734. GROUP BY b;
  5735. id select_type table type possible_keys key key_len ref rows Extra
  5736. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using temporary; Using filesort
  5737. 2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where
  5738. SELECT b FROM t1
  5739. WHERE ('0') IN ( SELECT a FROM t1 GROUP BY a )
  5740. GROUP BY b;
  5741. b
  5742. 0
  5743. set @@optimizer_switch=@optimizer_switch_save;
  5744. DROP TABLE t1;
  5745. #
  5746. # Bug #11765713 58705:
  5747. # OPTIMIZER LET ENGINE DEPEND ON UNINITIALIZED VALUES
  5748. # CREATED BY OPT_SUM_QUERY
  5749. #
  5750. CREATE TABLE t1(a INT NOT NULL, KEY (a));
  5751. INSERT INTO t1 VALUES (0), (1);
  5752. SELECT 1 as foo FROM t1 WHERE a < SOME
  5753. (SELECT a FROM t1 WHERE a <=>
  5754. (SELECT a FROM t1)
  5755. );
  5756. ERROR 21000: Subquery returns more than 1 row
  5757. SELECT 1 as foo FROM t1 WHERE a < SOME
  5758. (SELECT a FROM t1 WHERE a <=>
  5759. (SELECT a FROM t1 where a is null)
  5760. );
  5761. foo
  5762. DROP TABLE t1;
  5763. CREATE TABLE t1 (a int(11), b varchar(1));
  5764. INSERT INTO t1 VALUES (2,NULL),(5,'d'),(7,'g');
  5765. SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 GROUP BY b );
  5766. a
  5767. 5
  5768. SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 );
  5769. a
  5770. 5
  5771. SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 GROUP BY b );
  5772. a
  5773. 7
  5774. SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 );
  5775. a
  5776. 7
  5777. SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 GROUP BY b );
  5778. a
  5779. 5
  5780. 7
  5781. SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 );
  5782. a
  5783. 5
  5784. 7
  5785. SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 GROUP BY b );
  5786. a
  5787. 5
  5788. 7
  5789. SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 );
  5790. a
  5791. 5
  5792. 7
  5793. SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 );
  5794. a
  5795. 5
  5796. 7
  5797. SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 GROUP BY b );
  5798. a
  5799. 5
  5800. 7
  5801. SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 );
  5802. a
  5803. 5
  5804. 7
  5805. SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 GROUP BY b );
  5806. a
  5807. 5
  5808. 7
  5809. SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 GROUP BY b );
  5810. a
  5811. SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 );
  5812. a
  5813. SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 GROUP BY b );
  5814. a
  5815. SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 );
  5816. a
  5817. SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 GROUP BY b );
  5818. a
  5819. SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 );
  5820. a
  5821. SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 GROUP BY b );
  5822. a
  5823. SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 );
  5824. a
  5825. SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 );
  5826. a
  5827. SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 GROUP BY b );
  5828. a
  5829. SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 );
  5830. a
  5831. SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 GROUP BY b );
  5832. a
  5833. delete from t1;
  5834. INSERT INTO t1 VALUES (2,NULL),(5,'d'),(7,'g');
  5835. SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 GROUP BY b );
  5836. a
  5837. 5
  5838. SELECT a FROM t1 WHERE b < ANY ( SELECT b FROM t1 );
  5839. a
  5840. 5
  5841. SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 GROUP BY b );
  5842. a
  5843. 7
  5844. SELECT a FROM t1 WHERE b > ANY ( SELECT b FROM t1 );
  5845. a
  5846. 7
  5847. SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 GROUP BY b );
  5848. a
  5849. 5
  5850. 7
  5851. SELECT a FROM t1 WHERE b <= ANY ( SELECT b FROM t1 );
  5852. a
  5853. 5
  5854. 7
  5855. SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 GROUP BY b );
  5856. a
  5857. 5
  5858. 7
  5859. SELECT a FROM t1 WHERE b >= ANY ( SELECT b FROM t1 );
  5860. a
  5861. 5
  5862. 7
  5863. SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 );
  5864. a
  5865. 5
  5866. 7
  5867. SELECT a FROM t1 WHERE b = ANY ( SELECT b FROM t1 GROUP BY b );
  5868. a
  5869. 5
  5870. 7
  5871. SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 );
  5872. a
  5873. 5
  5874. 7
  5875. SELECT a FROM t1 WHERE b <> ANY ( SELECT b FROM t1 GROUP BY b );
  5876. a
  5877. 5
  5878. 7
  5879. SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 GROUP BY b );
  5880. a
  5881. SELECT a FROM t1 WHERE b < ALL ( SELECT b FROM t1 );
  5882. a
  5883. SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 GROUP BY b );
  5884. a
  5885. SELECT a FROM t1 WHERE b > ALL ( SELECT b FROM t1 );
  5886. a
  5887. SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 GROUP BY b );
  5888. a
  5889. SELECT a FROM t1 WHERE b <= ALL ( SELECT b FROM t1 );
  5890. a
  5891. SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 GROUP BY b );
  5892. a
  5893. SELECT a FROM t1 WHERE b >= ALL ( SELECT b FROM t1 );
  5894. a
  5895. SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 );
  5896. a
  5897. SELECT a FROM t1 WHERE b = ALL ( SELECT b FROM t1 GROUP BY b );
  5898. a
  5899. SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 );
  5900. a
  5901. SELECT a FROM t1 WHERE b <> ALL ( SELECT b FROM t1 GROUP BY b );
  5902. a
  5903. drop table t1;
  5904. #
  5905. # Fix of lp:780386 (NULL left part with empty ALL subquery).
  5906. #
  5907. CREATE TABLE t1 ( f11 int) ;
  5908. INSERT IGNORE INTO t1 VALUES (0),(0);
  5909. CREATE TABLE t2 ( f3 int, f10 int, KEY (f10,f3)) ;
  5910. INSERT IGNORE INTO t2 VALUES (NULL,NULL),(5,0);
  5911. DROP TABLE IF EXISTS t3;
  5912. Warnings:
  5913. Note 1051 Unknown table 'test.t3'
  5914. CREATE TABLE t3 ( f3 int) ;
  5915. INSERT INTO t3 VALUES (0),(0);
  5916. SELECT a1.f3 AS r FROM t2 AS a1 , t1 WHERE a1.f3 < ALL ( SELECT f3 FROM t3 WHERE f3 = 1 ) ;
  5917. r
  5918. NULL
  5919. 5
  5920. NULL
  5921. 5
  5922. DROP TABLE t1, t2, t3;
  5923. #
  5924. # Bug#12763207 - ASSERT IN SUBSELECT::SINGLE_VALUE_TRANSFORMER
  5925. #
  5926. CREATE TABLE t1(a1 int);
  5927. INSERT INTO t1 VALUES (1),(2);
  5928. CREATE TABLE t2(a1 int);
  5929. INSERT INTO t2 VALUES (3);
  5930. SELECT @@session.sql_mode INTO @old_sql_mode;
  5931. SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
  5932. SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2 FROM t2);
  5933. 1
  5934. 1
  5935. 1
  5936. SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 2.0 FROM t2);
  5937. 1
  5938. 1
  5939. 1
  5940. SELECT 1 FROM t1 WHERE 1 < SOME (SELECT 'a' FROM t2);
  5941. 1
  5942. Warnings:
  5943. Warning 1292 Truncated incorrect DOUBLE value: 'a'
  5944. SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t2);
  5945. 1
  5946. 1
  5947. 1
  5948. SET SESSION sql_mode=@old_sql_mode;
  5949. DROP TABLE t1, t2;
  5950. create table t2(i int);
  5951. insert into t2 values(0);
  5952. SELECT @@session.sql_mode INTO @old_sql_mode;
  5953. SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
  5954. CREATE VIEW v1 AS
  5955. SELECT 'f' FROM t2 UNION SELECT 'x' FROM t2
  5956. ;
  5957. CREATE TABLE t1 (
  5958. pk int NOT NULL,
  5959. col_varchar_key varchar(1) DEFAULT NULL,
  5960. PRIMARY KEY (pk),
  5961. KEY col_varchar_key (col_varchar_key)
  5962. );
  5963. SELECT t1.pk
  5964. FROM t1
  5965. WHERE t1.col_varchar_key < ALL ( SELECT * FROM v1 )
  5966. ;
  5967. pk
  5968. SET SESSION sql_mode=@old_sql_mode;
  5969. drop table t2, t1;
  5970. drop view v1;
  5971. #
  5972. # BUG#50257: Missing info in REF column of the EXPLAIN
  5973. # lines for subselects
  5974. #
  5975. CREATE TABLE t1 (a INT, b INT, INDEX (a));
  5976. INSERT INTO t1 VALUES (3, 10), (2, 20), (7, 10), (5, 20);
  5977. EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a=7) t;
  5978. id select_type table type possible_keys key key_len ref rows Extra
  5979. 1 SIMPLE t1 ref a a 5 const 1
  5980. EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE a=7);
  5981. id select_type table type possible_keys key key_len ref rows Extra
  5982. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4
  5983. 2 SUBQUERY t1 ref a a 5 const 1 Using index
  5984. DROP TABLE t1;
  5985. #
  5986. # BUG#12616253 - WRONG RESULT WITH EXISTS(SUBQUERY) (MISSING ROWS)
  5987. # (duplicate of LP bug #888456)
  5988. #
  5989. CREATE TABLE t1 (f1 varchar(1));
  5990. INSERT INTO t1 VALUES ('v'),('s');
  5991. CREATE TABLE t2 (f1_key varchar(1), KEY (f1_key));
  5992. INSERT INTO t2 VALUES ('j'),('v'),('c'),('m'),('d'),
  5993. ('d'),('y'),('t'),('d'),('s');
  5994. EXPLAIN
  5995. SELECT table1.f1, table2.f1_key FROM t1 AS table1, t2 AS table2
  5996. WHERE EXISTS (SELECT DISTINCT f1_key FROM t2
  5997. WHERE f1_key != table2.f1_key AND f1_key >= table1.f1);
  5998. id select_type table type possible_keys key key_len ref rows Extra
  5999. 1 PRIMARY table1 ALL NULL NULL NULL NULL 2
  6000. 1 PRIMARY table2 index NULL f1_key 4 NULL 10 Using where; Using index; Using join buffer (flat, BNL join)
  6001. 2 DEPENDENT SUBQUERY t2 index f1_key f1_key 4 NULL 10 Using where; Using index
  6002. SELECT table1.f1, table2.f1_key FROM t1 AS table1, t2 AS table2
  6003. WHERE EXISTS (SELECT DISTINCT f1_key FROM t2
  6004. WHERE f1_key != table2.f1_key AND f1_key >= table1.f1);
  6005. f1 f1_key
  6006. s c
  6007. s d
  6008. s d
  6009. s d
  6010. s j
  6011. s m
  6012. s s
  6013. s t
  6014. s v
  6015. s y
  6016. v c
  6017. v d
  6018. v d
  6019. v d
  6020. v j
  6021. v m
  6022. v s
  6023. v t
  6024. v v
  6025. v y
  6026. DROP TABLE t1,t2;
  6027. #
  6028. # LP bug 919427: EXPLAIN for a query over a single-row table
  6029. # with IN subquery in WHERE condition
  6030. #
  6031. CREATE TABLE ot (
  6032. col_int_nokey int(11),
  6033. col_varchar_nokey varchar(1)
  6034. ) ;
  6035. INSERT INTO ot VALUES (1,'x');
  6036. CREATE TABLE it1(
  6037. col_int_key int(11),
  6038. col_varchar_key varchar(1),
  6039. KEY idx_cvk_cik (col_varchar_key,col_int_key)
  6040. );
  6041. INSERT INTO it1 VALUES (NULL,'x'), (NULL,'f');
  6042. CREATE TABLE it2 (
  6043. col_int_key int(11),
  6044. col_varchar_key varchar(1),
  6045. col_varchar_key2 varchar(1),
  6046. KEY idx_cvk_cvk2_cik (col_varchar_key, col_varchar_key2, col_int_key),
  6047. KEY idx_cvk_cik (col_varchar_key, col_int_key)
  6048. );
  6049. INSERT INTO it2 VALUES (NULL,'x','x'), (NULL,'f','f');
  6050. EXPLAIN
  6051. SELECT col_int_nokey FROM ot
  6052. WHERE col_varchar_nokey IN
  6053. (SELECT col_varchar_key FROM it1 WHERE col_int_key IS NULL);
  6054. id select_type table type possible_keys key key_len ref rows Extra
  6055. 1 PRIMARY ot system NULL NULL NULL NULL 1
  6056. 2 DEPENDENT SUBQUERY it1 index_subquery idx_cvk_cik idx_cvk_cik 9 func,const 2 Using index; Using where
  6057. SELECT col_int_nokey FROM ot
  6058. WHERE col_varchar_nokey IN
  6059. (SELECT col_varchar_key FROM it1 WHERE col_int_key IS NULL);
  6060. col_int_nokey
  6061. 1
  6062. EXPLAIN
  6063. SELECT col_int_nokey FROM ot
  6064. WHERE (col_varchar_nokey, 'x') IN
  6065. (SELECT col_varchar_key, col_varchar_key2 FROM it2);
  6066. id select_type table type possible_keys key key_len ref rows Extra
  6067. 1 PRIMARY ot system NULL NULL NULL NULL 1
  6068. 2 DEPENDENT SUBQUERY it2 index_subquery idx_cvk_cvk2_cik,idx_cvk_cik idx_cvk_cvk2_cik 8 func,const 1 Using index; Using where
  6069. SELECT col_int_nokey FROM ot
  6070. WHERE (col_varchar_nokey, 'x') IN
  6071. (SELECT col_varchar_key, col_varchar_key2 FROM it2);
  6072. col_int_nokey
  6073. 1
  6074. DROP TABLE ot,it1,it2;
  6075. #
  6076. # MDEV-746
  6077. # Bug#13651009 WRONG RESULT FROM DERIVED TABLE IF THE SUBQUERY
  6078. # HAS AN EMPTY RESULT
  6079. #
  6080. CREATE TABLE t1 (
  6081. pk int NOT NULL,
  6082. col_int_nokey int NOT NULL,
  6083. col_int_key int NOT NULL,
  6084. col_time_key time NOT NULL,
  6085. col_varchar_key varchar(1) NOT NULL,
  6086. col_varchar_nokey varchar(1) NOT NULL,
  6087. PRIMARY KEY (pk),
  6088. KEY col_int_key (col_int_key),
  6089. KEY col_time_key (col_time_key),
  6090. KEY col_varchar_key (col_varchar_key,col_int_key)
  6091. ) ENGINE=MyISAM;
  6092. CREATE TABLE t2 (
  6093. pk int NOT NULL AUTO_INCREMENT,
  6094. col_int_nokey int NOT NULL,
  6095. col_int_key int NOT NULL,
  6096. col_time_key time NOT NULL,
  6097. col_varchar_key varchar(1) NOT NULL,
  6098. col_varchar_nokey varchar(1) NOT NULL,
  6099. PRIMARY KEY (pk),
  6100. KEY col_int_key (col_int_key),
  6101. KEY col_time_key (col_time_key),
  6102. KEY col_varchar_key (col_varchar_key,col_int_key)
  6103. ) ENGINE=MyISAM;
  6104. INSERT INTO t2 VALUES (1,4,4,'00:00:00','b','b');
  6105. SET @var2:=4, @var3:=8;
  6106. Testcase without inner subquery
  6107. EXPLAIN SELECT @var3:=12, sq4_alias1.*
  6108. FROM t1 AS sq4_alias1
  6109. WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
  6110. sq4_alias1.col_varchar_key = @var3;
  6111. id select_type table type possible_keys key key_len ref rows Extra
  6112. 1 SIMPLE sq4_alias1 system NULL NULL NULL NULL 0 Const row not found
  6113. SELECT @var3:=12, sq4_alias1.*
  6114. FROM t1 AS sq4_alias1
  6115. WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
  6116. sq4_alias1.col_varchar_key = @var3;
  6117. @var3:=12 pk col_int_nokey col_int_key col_time_key col_varchar_key col_varchar_nokey
  6118. SELECT @var3;
  6119. @var3
  6120. 8
  6121. EXPLAIN SELECT * FROM ( SELECT @var3:=12, sq4_alias1.*
  6122. FROM t1 AS sq4_alias1
  6123. WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
  6124. sq4_alias1.col_varchar_key = @var3 ) AS alias3;
  6125. id select_type table type possible_keys key key_len ref rows Extra
  6126. 1 PRIMARY <derived2> system NULL NULL NULL NULL 0 Const row not found
  6127. 2 DERIVED NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  6128. SELECT * FROM ( SELECT @var3:=12, sq4_alias1.*
  6129. FROM t1 AS sq4_alias1
  6130. WHERE (sq4_alias1.col_varchar_key + NULL) IS NULL OR
  6131. sq4_alias1.col_varchar_key = @var3 ) AS alias3;
  6132. @var3:=12 pk col_int_nokey col_int_key col_time_key col_varchar_key col_varchar_nokey
  6133. SELECT @var3;
  6134. @var3
  6135. 8
  6136. Testcase with inner subquery; crashed WL#6095
  6137. SET @var3=8;
  6138. EXPLAIN SELECT sq4_alias1.*
  6139. FROM t1 AS sq4_alias1
  6140. WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
  6141. NOT IN
  6142. (SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
  6143. c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
  6144. FROM t2 AS c_sq1_alias1
  6145. WHERE (c_sq1_alias1.col_int_nokey != @var2
  6146. OR c_sq1_alias1.pk != @var3));
  6147. id select_type table type possible_keys key key_len ref rows Extra
  6148. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  6149. 2 DEPENDENT SUBQUERY c_sq1_alias1 system PRIMARY NULL NULL NULL 1
  6150. SELECT sq4_alias1.*
  6151. FROM t1 AS sq4_alias1
  6152. WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
  6153. NOT IN
  6154. (SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
  6155. c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
  6156. FROM t2 AS c_sq1_alias1
  6157. WHERE (c_sq1_alias1.col_int_nokey != @var2
  6158. OR c_sq1_alias1.pk != @var3));
  6159. pk col_int_nokey col_int_key col_time_key col_varchar_key col_varchar_nokey
  6160. EXPLAIN SELECT * FROM ( SELECT sq4_alias1.*
  6161. FROM t1 AS sq4_alias1
  6162. WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
  6163. NOT IN
  6164. (SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
  6165. c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
  6166. FROM t2 AS c_sq1_alias1
  6167. WHERE (c_sq1_alias1.col_int_nokey != @var2
  6168. OR c_sq1_alias1.pk != @var3)) ) AS alias3;
  6169. id select_type table type possible_keys key key_len ref rows Extra
  6170. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  6171. 3 DEPENDENT SUBQUERY c_sq1_alias1 system PRIMARY NULL NULL NULL 1
  6172. SELECT * FROM ( SELECT sq4_alias1.*
  6173. FROM t1 AS sq4_alias1
  6174. WHERE (sq4_alias1.col_varchar_key , sq4_alias1.col_varchar_nokey)
  6175. NOT IN
  6176. (SELECT c_sq1_alias1.col_varchar_key AS c_sq1_field1,
  6177. c_sq1_alias1.col_varchar_nokey AS c_sq1_field2
  6178. FROM t2 AS c_sq1_alias1
  6179. WHERE (c_sq1_alias1.col_int_nokey != @var2
  6180. OR c_sq1_alias1.pk != @var3)) ) AS alias3;
  6181. pk col_int_nokey col_int_key col_time_key col_varchar_key col_varchar_nokey
  6182. DROP TABLE t1,t2;
  6183. # End of 5.2 tests
  6184. #
  6185. # BUG#779885: Crash in eliminate_item_equal with materialization=on in
  6186. #
  6187. CREATE TABLE t1 ( f1 int );
  6188. INSERT INTO t1 VALUES (19), (20);
  6189. CREATE TABLE t2 ( f10 varchar(32) );
  6190. INSERT INTO t2 VALUES ('c'),('d');
  6191. CREATE TABLE t3 ( f10 varchar(32) );
  6192. INSERT INTO t3 VALUES ('a'),('b');
  6193. SELECT *
  6194. FROM t1
  6195. WHERE
  6196. ( 't' ) IN (
  6197. SELECT t3.f10
  6198. FROM t3
  6199. JOIN t2
  6200. ON t2.f10 = t3.f10
  6201. );
  6202. f1
  6203. DROP TABLE t1,t2,t3;
  6204. #
  6205. # BUG lp:813473: Wrong result with outer join + NOT IN subquery
  6206. # This bug is a duplicate of Bug#11764086 whose test case is added below
  6207. #
  6208. CREATE TABLE t1 (c int) ;
  6209. INSERT INTO t1 VALUES (5),(6);
  6210. CREATE TABLE t2 (a int, b int) ;
  6211. INSERT INTO t2 VALUES (20,9),(20,9);
  6212. create table t3 (d int, e int);
  6213. insert into t3 values (2, 9), (3,10);
  6214. SET @save_optimizer_switch=@@optimizer_switch;
  6215. SET optimizer_switch='outer_join_with_cache=off';
  6216. EXPLAIN
  6217. SELECT t2.b , t1.c
  6218. FROM t2 LEFT JOIN t1 ON t1.c < 3
  6219. WHERE (t2.b , t1.c) NOT IN (SELECT * from t3);
  6220. id select_type table type possible_keys key key_len ref rows Extra
  6221. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2
  6222. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
  6223. 2 MATERIALIZED t3 ALL NULL NULL NULL NULL 2
  6224. SELECT t2.b , t1.c
  6225. FROM t2 LEFT JOIN t1 ON t1.c < 3
  6226. WHERE (t2.b, t1.c) NOT IN (SELECT * from t3);
  6227. b c
  6228. 9 NULL
  6229. 9 NULL
  6230. SET optimizer_switch=@save_optimizer_switch;
  6231. drop table t1, t2, t3;
  6232. #
  6233. # BUG#50257: Missing info in REF column of the EXPLAIN
  6234. # lines for subselects
  6235. #
  6236. CREATE TABLE t1 (a INT, b INT, INDEX (a));
  6237. INSERT INTO t1 VALUES (3, 10), (2, 20), (7, 10), (5, 20);
  6238. set @tmp_optimizer_switch=@@optimizer_switch;
  6239. set optimizer_switch='derived_merge=off,derived_with_keys=off';
  6240. EXPLAIN SELECT * FROM (SELECT * FROM t1 WHERE a=7) t;
  6241. id select_type table type possible_keys key key_len ref rows Extra
  6242. 1 PRIMARY <derived2> ALL NULL NULL NULL NULL 2
  6243. 2 DERIVED t1 ref a a 5 const 1
  6244. set optimizer_switch=@tmp_optimizer_switch;
  6245. EXPLAIN SELECT * FROM t1 WHERE EXISTS (SELECT * FROM t1 WHERE a=7);
  6246. id select_type table type possible_keys key key_len ref rows Extra
  6247. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4
  6248. 2 SUBQUERY t1 ref a a 5 const 1 Using index
  6249. DROP TABLE t1;
  6250. #
  6251. # Bug#11764086: Null left operand to NOT IN in WHERE clause
  6252. # behaves differently than real NULL
  6253. #
  6254. CREATE TABLE parent (id int);
  6255. INSERT INTO parent VALUES (1), (2);
  6256. CREATE TABLE child (parent_id int, other int);
  6257. INSERT INTO child VALUES (1,NULL);
  6258. # Offending query (c.parent_id is NULL for null-complemented rows only)
  6259. SELECT p.id, c.parent_id
  6260. FROM parent p
  6261. LEFT JOIN child c
  6262. ON p.id = c.parent_id
  6263. WHERE c.parent_id NOT IN (
  6264. SELECT parent_id
  6265. FROM child
  6266. WHERE parent_id = 3
  6267. );
  6268. id parent_id
  6269. 1 1
  6270. 2 NULL
  6271. # Some syntactic variations with IS FALSE and IS NOT TRUE
  6272. SELECT p.id, c.parent_id
  6273. FROM parent p
  6274. LEFT JOIN child c
  6275. ON p.id = c.parent_id
  6276. WHERE c.parent_id IN (
  6277. SELECT parent_id
  6278. FROM child
  6279. WHERE parent_id = 3
  6280. ) IS NOT TRUE;
  6281. id parent_id
  6282. 1 1
  6283. 2 NULL
  6284. SELECT p.id, c.parent_id
  6285. FROM parent p
  6286. LEFT JOIN child c
  6287. ON p.id = c.parent_id
  6288. WHERE c.parent_id IN (
  6289. SELECT parent_id
  6290. FROM child
  6291. WHERE parent_id = 3
  6292. ) IS FALSE;
  6293. id parent_id
  6294. 1 1
  6295. 2 NULL
  6296. DROP TABLE parent, child;
  6297. # End of test for bug#11764086.
  6298. #
  6299. # Bug 11765699 - 58690: !TABLE || (!TABLE->READ_SET ||
  6300. # BITMAP_IS_SET(TABLE->READ_SET, FIELD_INDEX
  6301. #
  6302. CREATE TABLE t1(a INT);
  6303. INSERT INTO t1 VALUES (0), (1);
  6304. CREATE TABLE t2(
  6305. b TEXT,
  6306. c INT,
  6307. PRIMARY KEY (b(1))
  6308. );
  6309. INSERT INTO t2 VALUES ('a', 2), ('b', 3);
  6310. SELECT 1 FROM t1 WHERE a =
  6311. (SELECT 1 FROM t2 WHERE b =
  6312. (SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2)
  6313. ORDER BY b
  6314. );
  6315. 1
  6316. Warnings:
  6317. Warning 1292 Truncated incorrect DOUBLE value: 'a'
  6318. Warning 1292 Truncated incorrect DOUBLE value: 'b'
  6319. Warning 1292 Truncated incorrect DOUBLE value: 'a'
  6320. Warning 1292 Truncated incorrect DOUBLE value: 'b'
  6321. SELECT 1 FROM t1 WHERE a =
  6322. (SELECT 1 FROM t2 WHERE b =
  6323. (SELECT 1 FROM t1 t11 WHERE c = 1 OR t1.a = 1 AND 1 = 2)
  6324. GROUP BY b
  6325. );
  6326. 1
  6327. Warnings:
  6328. Warning 1292 Truncated incorrect DOUBLE value: 'a'
  6329. Warning 1292 Truncated incorrect DOUBLE value: 'b'
  6330. Warning 1292 Truncated incorrect DOUBLE value: 'a'
  6331. Warning 1292 Truncated incorrect DOUBLE value: 'b'
  6332. DROP TABLE t1, t2;
  6333. #
  6334. # BUG#12616253 - WRONG RESULT WITH EXISTS(SUBQUERY) (MISSING ROWS)
  6335. #
  6336. CREATE TABLE t1 (f1 varchar(1));
  6337. INSERT INTO t1 VALUES ('v'),('s');
  6338. CREATE TABLE t2 (f1_key varchar(1), KEY (f1_key));
  6339. INSERT INTO t2 VALUES ('j'),('v'),('c'),('m'),('d'),
  6340. ('d'),('y'),('t'),('d'),('s');
  6341. SELECT table1.f1, table2.f1_key
  6342. FROM t1 AS table1, t2 AS table2
  6343. WHERE EXISTS
  6344. (
  6345. SELECT DISTINCT f1_key
  6346. FROM t2
  6347. WHERE f1_key != table2.f1_key AND f1_key >= table1.f1 );
  6348. f1 f1_key
  6349. s c
  6350. s d
  6351. s d
  6352. s d
  6353. s j
  6354. s m
  6355. s s
  6356. s t
  6357. s v
  6358. s y
  6359. v c
  6360. v d
  6361. v d
  6362. v d
  6363. v j
  6364. v m
  6365. v s
  6366. v t
  6367. v v
  6368. v y
  6369. explain SELECT table1.f1, table2.f1_key
  6370. FROM t1 AS table1, t2 AS table2
  6371. WHERE EXISTS
  6372. (
  6373. SELECT DISTINCT f1_key
  6374. FROM t2
  6375. WHERE f1_key != table2.f1_key AND f1_key >= table1.f1 );
  6376. id select_type table type possible_keys key key_len ref rows Extra
  6377. 1 PRIMARY table1 ALL NULL NULL NULL NULL 2
  6378. 1 PRIMARY table2 index NULL f1_key 4 NULL 10 Using where; Using index; Using join buffer (flat, BNL join)
  6379. 2 DEPENDENT SUBQUERY t2 index f1_key f1_key 4 NULL 10 Using where; Using index
  6380. DROP TABLE t1,t2;
  6381. #
  6382. # lp:826279: assertion failure with GROUP BY a result of subquery
  6383. #
  6384. CREATE TABLE t1 (a int);
  6385. INSERT INTO t1 VALUES (0), (0);
  6386. CREATE TABLE t2 (a int, b int, c int);
  6387. INSERT INTO t2 VALUES (10,7,0), (0,7,0);
  6388. CREATE TABLE t3 (a int, b int);
  6389. INSERT INTO t3 VALUES (10,7), (0,7);
  6390. SELECT SUM(DISTINCT b),
  6391. (SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
  6392. WHERE t.a != 0 AND t2.a != 0)
  6393. FROM (SELECT * FROM t3) AS t
  6394. GROUP BY 2;
  6395. SUM(DISTINCT b) (SELECT t2.a FROM t1 JOIN t2 ON t2.c != 0
  6396. WHERE t.a != 0 AND t2.a != 0)
  6397. 7 NULL
  6398. SELECT SUM(DISTINCT b),
  6399. (SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1)
  6400. FROM (SELECT * FROM t3) AS t
  6401. GROUP BY 2;
  6402. SUM(DISTINCT b) (SELECT t2.a FROM t1,t2 WHERE t.a != 0 or 1=2 LIMIT 1)
  6403. 7 NULL
  6404. 7 10
  6405. DROP TABLE t1,t2,t3;
  6406. #
  6407. # Bug#12329653
  6408. # EXPLAIN, UNION, PREPARED STATEMENT, CRASH, SQL_FULL_GROUP_BY
  6409. #
  6410. CREATE TABLE t1(a1 int);
  6411. INSERT INTO t1 VALUES (1),(2);
  6412. SELECT @@session.sql_mode INTO @old_sql_mode;
  6413. SET SESSION sql_mode='ONLY_FULL_GROUP_BY';
  6414. SELECT 1 FROM t1 WHERE 1 < SOME (SELECT a1 FROM t1);
  6415. 1
  6416. 1
  6417. 1
  6418. PREPARE stmt FROM
  6419. 'SELECT 1 UNION ALL
  6420. SELECT 1 FROM t1
  6421. ORDER BY
  6422. (SELECT 1 FROM t1 AS t1_0
  6423. WHERE 1 < SOME (SELECT a1 FROM t1)
  6424. )' ;
  6425. EXECUTE stmt ;
  6426. ERROR 21000: Subquery returns more than 1 row
  6427. EXECUTE stmt ;
  6428. ERROR 21000: Subquery returns more than 1 row
  6429. SET SESSION sql_mode=@old_sql_mode;
  6430. DEALLOCATE PREPARE stmt;
  6431. DROP TABLE t1;
  6432. #
  6433. # LP BUG#833777 Performance regression with deeply nested subqueries
  6434. #
  6435. create table t1 (a int not null, b char(10) not null);
  6436. insert into t1 values (1, 'a');
  6437. set @@optimizer_switch='in_to_exists=on,semijoin=off,materialization=off,subquery_cache=off';
  6438. 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)))))))))))))))))))))))))))));
  6439. a
  6440. 1
  6441. set @@optimizer_switch=@subselect_tmp;
  6442. drop table t1;
  6443. #
  6444. # LP BUG#894397 Wrong result with in_to_exists, constant table , semijoin=OFF,materialization=OFF
  6445. #
  6446. CREATE TABLE t1 (a varchar(3));
  6447. INSERT INTO t1 VALUES ('AAA'),('BBB');
  6448. CREATE TABLE t2 (a varchar(3));
  6449. INSERT INTO t2 VALUES ('CCC');
  6450. set @@optimizer_switch='semijoin=off,materialization=off,in_to_exists=on,subquery_cache=off';
  6451. SELECT * FROM t1 WHERE t1.a IN (SELECT t2.a FROM t2 WHERE t2.a < 'ZZZ');
  6452. a
  6453. set @@optimizer_switch=@subselect_tmp;
  6454. drop table t1, t2;
  6455. #
  6456. # LP bug #859375: Assertion `0' failed in st_select_lex_unit::optimize
  6457. # with view , UNION and prepared statement (rewriting fake_select
  6458. # condition).
  6459. #
  6460. CREATE TABLE t1 ( f1 int NOT NULL, f4 varchar(1) NOT NULL) ;
  6461. INSERT INTO t1 VALUES (6,'d'),(7,'y');
  6462. CREATE TABLE t2 ( f1 int NOT NULL, f2 int NOT NULL) ;
  6463. INSERT INTO t2 VALUES (10,7);
  6464. CREATE VIEW v2 AS SELECT * FROM t2;
  6465. PREPARE st1 FROM "
  6466. SELECT *
  6467. FROM t1
  6468. LEFT JOIN v2 ON ( v2.f2 = t1.f1 )
  6469. WHERE v2.f1 NOT IN (
  6470. SELECT 1 UNION
  6471. SELECT 247
  6472. )
  6473. ";
  6474. EXECUTE st1;
  6475. f1 f4 f1 f2
  6476. 7 y 10 7
  6477. deallocate prepare st1;
  6478. DROP VIEW v2;
  6479. DROP TABLE t1,t2;
  6480. #
  6481. # LP bug #887458 Crash in subselect_union_engine::no_rows with
  6482. # double UNION and join_cache_level=3,8
  6483. # (IN/ALL/ANY optimizations should not be applied to fake_select)
  6484. CREATE TABLE t2 ( a int, b varchar(1)) ;
  6485. INSERT IGNORE INTO t2 VALUES (8,'y'),(8,'y');
  6486. CREATE TABLE t1 ( b varchar(1)) ;
  6487. INSERT IGNORE INTO t1 VALUES (NULL),(NULL);
  6488. set @save_join_cache_level=@@join_cache_level;
  6489. SET SESSION join_cache_level=3;
  6490. SELECT *
  6491. FROM t1, t2
  6492. WHERE t2.b IN (
  6493. SELECT 'm' UNION
  6494. SELECT 'm'
  6495. ) OR t1.b <> SOME (
  6496. SELECT 'v' UNION
  6497. SELECT 't'
  6498. );
  6499. b a b
  6500. set @@join_cache_level= @save_join_cache_level;
  6501. drop table t1,t2;
  6502. #
  6503. # LP bug #885162 Got error 124 from storage engine with UNION inside
  6504. # subquery and join_cache_level=3..8
  6505. # (IN/ALL/ANY optimizations should not be applied to fake_select)
  6506. #
  6507. CREATE TABLE t1 (
  6508. f1 varchar(1) DEFAULT NULL
  6509. );
  6510. INSERT INTO t1 VALUES ('c');
  6511. set @save_join_cache_level=@@join_cache_level;
  6512. SET SESSION join_cache_level=8;
  6513. SELECT * FROM t1 WHERE t1.f1 IN ( SELECT 'k' UNION SELECT 'e' );
  6514. f1
  6515. set @@join_cache_level= @save_join_cache_level;
  6516. drop table t1;
  6517. #
  6518. # LP BUG#747278 incorrect values of the NULL (no rows) single
  6519. # row subquery requested via element_index() interface
  6520. #
  6521. CREATE TABLE t1 (f1a int, f1b int) ;
  6522. INSERT IGNORE INTO t1 VALUES (1,1),(2,2);
  6523. CREATE TABLE t2 ( f2 int);
  6524. INSERT IGNORE INTO t2 VALUES (3),(4);
  6525. CREATE TABLE t3 (f3a int default 1, f3b int default 2);
  6526. INSERT INTO t3 VALUES (1,1),(2,2);
  6527. set @old_optimizer_switch = @@session.optimizer_switch;
  6528. set @@optimizer_switch='materialization=on,partial_match_rowid_merge=on,partial_match_table_scan=off,subquery_cache=off,semijoin=off';
  6529. SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
  6530. (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
  6531. NULL
  6532. NULL
  6533. SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
  6534. (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
  6535. NULL
  6536. NULL
  6537. SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
  6538. (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
  6539. NULL
  6540. NULL
  6541. SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
  6542. (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
  6543. NULL
  6544. SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
  6545. (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
  6546. NULL
  6547. NULL
  6548. SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
  6549. (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
  6550. NULL
  6551. NULL
  6552. SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
  6553. (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
  6554. NULL
  6555. NULL
  6556. SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
  6557. (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
  6558. NULL
  6559. set @@session.optimizer_switch=@old_optimizer_switch;
  6560. SELECT (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1) FROM t2;
  6561. (SELECT f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a FROM t1)
  6562. NULL
  6563. NULL
  6564. SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1) FROM t2;
  6565. (SELECT f3a,f3a FROM t3 where f3a > 3) NOT IN (SELECT f1a,f1a FROM t1)
  6566. NULL
  6567. NULL
  6568. SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1) FROM t2;
  6569. (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
  6570. NULL
  6571. NULL
  6572. SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1);
  6573. (SELECT f3a, f3b FROM t3 where f3a > 3) NOT IN (SELECT f1a, f1b FROM t1)
  6574. NULL
  6575. SELECT (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1) FROM t2;
  6576. (SELECT f3a FROM t3 where f3a > 3) IN (SELECT f1a FROM t1)
  6577. NULL
  6578. NULL
  6579. SELECT (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1) FROM t2;
  6580. (SELECT f3a,f3a FROM t3 where f3a > 3) IN (SELECT f1a,f1a FROM t1)
  6581. NULL
  6582. NULL
  6583. SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1) FROM t2;
  6584. (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
  6585. NULL
  6586. NULL
  6587. SELECT (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1);
  6588. (SELECT f3a, f3b FROM t3 where f3a > 3) IN (SELECT f1a, f1b FROM t1)
  6589. NULL
  6590. select (null, null) = (null, null);
  6591. (null, null) = (null, null)
  6592. NULL
  6593. SELECT (SELECT f3a, f3a FROM t3 where f3a > 3) = (0, 0);
  6594. (SELECT f3a, f3a FROM t3 where f3a > 3) = (0, 0)
  6595. NULL
  6596. drop tables t1,t2,t3;
  6597. #
  6598. # LP BUG#825051 Wrong result with date/datetime and subquery with GROUP BY and in_to_exists
  6599. #
  6600. CREATE TABLE t1 (a date, KEY (a)) ;
  6601. INSERT INTO t1 VALUES ('2009-01-01'),('2009-02-02');
  6602. set @old_optimizer_switch = @@optimizer_switch;
  6603. SET @@optimizer_switch='semijoin=off,materialization=off,in_to_exists=on,subquery_cache=off';
  6604. EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
  6605. id select_type table type possible_keys key key_len ref rows Extra
  6606. 1 PRIMARY t1 index NULL a 4 NULL 2 Using where; Using index
  6607. 2 DEPENDENT SUBQUERY t1 index_subquery a a 4 func 2 Using index
  6608. SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
  6609. a
  6610. 2009-01-01
  6611. 2009-02-02
  6612. SET @@optimizer_switch='semijoin=off,materialization=on,in_to_exists=off,subquery_cache=off';
  6613. EXPLAIN SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
  6614. id select_type table type possible_keys key key_len ref rows Extra
  6615. 1 PRIMARY t1 index NULL a 4 NULL 2 Using where; Using index
  6616. 2 MATERIALIZED t1 index NULL a 4 NULL 2 Using index
  6617. SELECT * FROM t1 WHERE a IN (SELECT a AS field1 FROM t1 GROUP BY field1);
  6618. a
  6619. 2009-01-01
  6620. 2009-02-02
  6621. set @@optimizer_switch=@old_optimizer_switch;
  6622. drop table t1;
  6623. #
  6624. # LP BUG#908269 incorrect condition in case of subqueries depending
  6625. # on constant tables
  6626. #
  6627. CREATE TABLE t1 ( a INT );
  6628. INSERT INTO t1 VALUES (1),(5);
  6629. CREATE TABLE t2 ( b INT ) ENGINE=MyISAM;
  6630. INSERT INTO t2 VALUES (1);
  6631. CREATE TABLE t3 ( c INT );
  6632. INSERT INTO t3 VALUES (4),(5);
  6633. SET optimizer_switch='subquery_cache=off';
  6634. SELECT ( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1;
  6635. ( SELECT b FROM t2 WHERE b = a OR EXISTS ( SELECT c FROM t3 WHERE c = b ) )
  6636. 1
  6637. NULL
  6638. SELECT ( SELECT b FROM t2 WHERE b = a OR b * 0) FROM t1;
  6639. ( SELECT b FROM t2 WHERE b = a OR b * 0)
  6640. 1
  6641. NULL
  6642. SELECT ( SELECT b FROM t2 WHERE b = a OR rand() * 0) FROM t1;
  6643. ( SELECT b FROM t2 WHERE b = a OR rand() * 0)
  6644. 1
  6645. NULL
  6646. drop table t1,t2,t3;
  6647. set optimizer_switch=@subselect_tmp;
  6648. #
  6649. # LP BUG#905353 Wrong non-empty result with a constant table,
  6650. # aggregate function in subquery, MyISAM or Aria
  6651. #
  6652. CREATE TABLE t1 ( a INT ) ENGINE=MyISAM;
  6653. INSERT INTO t1 VALUES (1);
  6654. SELECT a FROM t1 WHERE ( SELECT MIN(a) = 100 );
  6655. a
  6656. drop table t1;
  6657. #
  6658. # LP BUG#985667 Wrong result with subquery in SELECT clause, and constant table in
  6659. # main query and implicit grouping
  6660. #
  6661. CREATE TABLE t1 (f1 int) engine=MyISAM;
  6662. INSERT INTO t1 VALUES (7),(8);
  6663. CREATE TABLE t2 (f2 int, f3 varchar(1)) engine=MyISAM;
  6664. INSERT INTO t2 VALUES (3,'f');
  6665. EXPLAIN
  6666. SELECT COUNT(f1), (SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
  6667. id select_type table type possible_keys key key_len ref rows Extra
  6668. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  6669. 2 SUBQUERY t1 ALL NULL NULL NULL NULL 2
  6670. SELECT COUNT(f1), (SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
  6671. COUNT(f1) f4
  6672. 0 NULL
  6673. EXPLAIN
  6674. SELECT COUNT(f1), exists(SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
  6675. id select_type table type possible_keys key key_len ref rows Extra
  6676. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  6677. 2 SUBQUERY t1 ALL NULL NULL NULL NULL 2
  6678. SELECT COUNT(f1), exists(SELECT f1 FROM t1 WHERE f2 > 0 limit 1) AS f4 FROM t2, t1 WHERE 'v'= f3;
  6679. COUNT(f1) f4
  6680. 0 0
  6681. EXPLAIN
  6682. SELECT COUNT(f1), f2 > ALL (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
  6683. id select_type table type possible_keys key key_len ref rows Extra
  6684. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  6685. 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where
  6686. SELECT COUNT(f1), f2 > ALL (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
  6687. COUNT(f1) f4
  6688. 0 1
  6689. EXPLAIN
  6690. SELECT COUNT(f1), f2 IN (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
  6691. id select_type table type possible_keys key key_len ref rows Extra
  6692. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  6693. 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where
  6694. SELECT COUNT(f1), f2 IN (SELECT f1 FROM t1 WHERE f2 > 0) AS f4 FROM t2, t1 WHERE 'v'= f3;
  6695. COUNT(f1) f4
  6696. 0 0
  6697. drop table t1,t2;
  6698. #
  6699. # LP BUG#1002079 Server crashes in Item_singlerow_subselect::val_int with constant table,
  6700. # HAVING, UNION in subquery
  6701. #
  6702. CREATE TABLE t1 (a INT);
  6703. INSERT INTO t1 VALUES (7),(0);
  6704. CREATE TABLE t2 (b INT);
  6705. EXPLAIN
  6706. SELECT SUM(a) AS f1, a AS f2 FROM (t1, t2) HAVING f2 >= ALL (SELECT 4 UNION SELECT 5) AND f1 = 7;
  6707. id select_type table type possible_keys key key_len ref rows Extra
  6708. 1 PRIMARY t2 system NULL NULL NULL NULL 0 Const row not found
  6709. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2
  6710. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used
  6711. 3 UNION NULL NULL NULL NULL NULL NULL NULL No tables used
  6712. NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
  6713. SELECT SUM(a) AS f1, a AS f2 FROM (t1, t2) HAVING f2 >= ALL (SELECT 4 UNION SELECT 5) AND f1 = 7;
  6714. f1 f2
  6715. drop table t1,t2;
  6716. #
  6717. # LP BUG#1008686 Server crashes in subselect_union_engine::no_rows on SELECT with impossible
  6718. # WHERE and UNION in HAVING
  6719. #
  6720. CREATE TABLE t1 (a INT);
  6721. INSERT INTO t1 VALUES (1),(7);
  6722. EXPLAIN
  6723. SELECT MIN(a) AS min_a, a FROM t1 WHERE 0 HAVING a NOT IN ( SELECT 2 UNION SELECT 5 ) OR min_a != 1;
  6724. id select_type table type possible_keys key key_len ref rows Extra
  6725. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  6726. 2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL No tables used
  6727. 3 DEPENDENT UNION NULL NULL NULL NULL NULL NULL NULL No tables used
  6728. NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
  6729. SELECT MIN(a) AS min_a, a FROM t1 WHERE 0 HAVING a NOT IN ( SELECT 2 UNION SELECT 5 ) OR min_a != 1;
  6730. min_a a
  6731. EXPLAIN
  6732. SELECT MIN(a) AS min_a, a FROM t1 WHERE 1=2 HAVING a NOT IN ( SELECT a from t1 UNION select a+1 from t1 ) OR min_a != 1;
  6733. id select_type table type possible_keys key key_len ref rows Extra
  6734. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  6735. 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where
  6736. 3 DEPENDENT UNION t1 ALL NULL NULL NULL NULL 2 Using where
  6737. NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL
  6738. SELECT MIN(a) AS min_a, a FROM t1 WHERE 1=2 HAVING a NOT IN ( SELECT a from t1 UNION select a+1 from t1 ) OR min_a != 1;
  6739. min_a a
  6740. drop table t1;
  6741. #
  6742. # MDEV-367: Different results with and without subquery_cache on
  6743. # a query with a constant NOT IN condition
  6744. #
  6745. CREATE TABLE t1 (a INT) ENGINE=MyISAM;
  6746. INSERT INTO t1 VALUES (1),(2),(3);
  6747. set @mdev367_optimizer_switch = @@optimizer_switch;
  6748. set optimizer_switch = 'subquery_cache=on';
  6749. SELECT * FROM t1 WHERE ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) OR a > 100;
  6750. a
  6751. SELECT *, ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) FROM t1;
  6752. a ( 3, 3 ) NOT IN ( SELECT NULL, NULL )
  6753. 1 NULL
  6754. 2 NULL
  6755. 3 NULL
  6756. set optimizer_switch=@mdev367_optimizer_switch;
  6757. set optimizer_switch = 'subquery_cache=off';
  6758. SELECT * FROM t1 WHERE ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) OR a > 100;
  6759. a
  6760. SELECT *, ( 3, 3 ) NOT IN ( SELECT NULL, NULL ) FROM t1;
  6761. a ( 3, 3 ) NOT IN ( SELECT NULL, NULL )
  6762. 1 NULL
  6763. 2 NULL
  6764. 3 NULL
  6765. set optimizer_switch=@mdev367_optimizer_switch;
  6766. DROP TABLE t1;
  6767. #
  6768. # MDEV-521 single value subselect transformation problem
  6769. #
  6770. CREATE TABLE t1 (f1 char(2), PRIMARY KEY (f1)) ENGINE=MyISAM;
  6771. INSERT INTO t1 VALUES ('u1'),('u2');
  6772. SELECT a.* FROM t1 a WHERE ( SELECT EXISTS ( SELECT 1 FROM t1 b WHERE b.f1 = a.f1 ) );
  6773. f1
  6774. u1
  6775. u2
  6776. FLUSH TABLES;
  6777. SELECT a.* FROM t1 a WHERE ( SELECT EXISTS ( SELECT 1 FROM t1 b WHERE b.f1 = a.f1 ) );
  6778. f1
  6779. u1
  6780. u2
  6781. DROP TABLE t1;
  6782. # return optimizer switch changed in the beginning of this test
  6783. set optimizer_switch=@subselect_tmp;
  6784. #
  6785. # lp:944706 Query with impossible or constant subquery in WHERE or HAVING is not
  6786. # precomputed and thus not part of optimization
  6787. #
  6788. CREATE TABLE t1 ( a VARCHAR(16), KEY (a) );
  6789. INSERT INTO t1 VALUES ('Abilene'),('Akron'),('Albany'),('Albuquerque'),('Alexandria'),('Allentown'),
  6790. ('Amarillo'),('Anaheim'),('Anchorage'),('Ann Arbor'),('Arden-Arcade');
  6791. EXPLAIN
  6792. SELECT MAX( alias2.a ) AS field
  6793. FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
  6794. WHERE alias1.a = alias2.a OR alias1.a = 'y'
  6795. HAVING field>'B' AND ( 'Moscow' ) IN ( SELECT a FROM t1 );
  6796. id select_type table type possible_keys key key_len ref rows Extra
  6797. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible HAVING
  6798. 2 SUBQUERY t1 index_subquery a a 19 const 1 Using index; Using where
  6799. SELECT MAX( alias2.a ) AS field
  6800. FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
  6801. WHERE alias1.a = alias2.a OR alias1.a = 'y'
  6802. HAVING field>'B' AND ( 'Moscow' ) IN ( SELECT a FROM t1 );
  6803. field
  6804. EXPLAIN
  6805. SELECT MAX( alias2.a )
  6806. FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
  6807. WHERE alias1.a = alias2.a OR ('Moscow') IN ( SELECT a FROM t1 );
  6808. id select_type table type possible_keys key key_len ref rows Extra
  6809. 1 PRIMARY alias1 index a a 19 NULL 11 Using where; Using index
  6810. 1 PRIMARY alias2 ref a a 19 test.alias1.a 2 Using index
  6811. 1 PRIMARY alias3 index NULL a 19 NULL 11 Using index; Using join buffer (flat, BNL join)
  6812. 2 SUBQUERY t1 index_subquery a a 19 const 1 Using index; Using where
  6813. SELECT MAX( alias2.a )
  6814. FROM t1 AS alias1, t1 AS alias2, t1 AS alias3
  6815. WHERE alias1.a = alias2.a OR ('Moscow') IN ( SELECT a FROM t1 );
  6816. MAX( alias2.a )
  6817. Arden-Arcade
  6818. drop table t1;
  6819. #
  6820. # MDEV-277 CHEAP SQ: Server crashes in st_join_table::get_examined_rows
  6821. # with semijoin+materialization, IN and = subqueries
  6822. #
  6823. CREATE TABLE t1 (a1 INT);
  6824. INSERT INTO t1 VALUES (4),(6);
  6825. CREATE TABLE t2 (b1 INT);
  6826. INSERT INTO t2 VALUES (1),(7);
  6827. EXPLAIN
  6828. SELECT * FROM t1
  6829. WHERE a1 = (SELECT COUNT(*) FROM t1 WHERE a1 IN (SELECT a1 FROM t1, t2));
  6830. id select_type table type possible_keys key key_len ref rows Extra
  6831. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
  6832. 2 SUBQUERY t1 ALL NULL NULL NULL NULL 2
  6833. 2 SUBQUERY <subquery3> eq_ref distinct_key distinct_key 4 func 1
  6834. 3 MATERIALIZED t1 ALL NULL NULL NULL NULL 2
  6835. 3 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join)
  6836. SELECT * FROM t1
  6837. WHERE a1 = (SELECT COUNT(*) FROM t1 WHERE a1 IN (SELECT a1 FROM t1, t2));
  6838. a1
  6839. drop table t1, t2;
  6840. #
  6841. # MDEV-287 CHEAP SQ: A query with subquery in SELECT list, EXISTS,
  6842. # inner joins takes hundreds times longer
  6843. #
  6844. CREATE TABLE t1 (a INT);
  6845. INSERT INTO t1 VALUES (1),(7);
  6846. CREATE TABLE t2 (b INT);
  6847. INSERT INTO t2 VALUES (4),(5);
  6848. CREATE TABLE t3 (c INT);
  6849. INSERT INTO t3 VALUES (8),(3);
  6850. set @@expensive_subquery_limit= 0;
  6851. EXPLAIN
  6852. SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
  6853. FROM t2 alias1, t1 alias2, t1 alias3;
  6854. id select_type table type possible_keys key key_len ref rows Extra
  6855. 1 PRIMARY alias1 ALL NULL NULL NULL NULL 2
  6856. 1 PRIMARY alias2 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join)
  6857. 1 PRIMARY alias3 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join)
  6858. 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where
  6859. 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
  6860. 3 SUBQUERY t3 ALL NULL NULL NULL NULL 2
  6861. flush status;
  6862. SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
  6863. FROM t2 alias1, t1 alias2, t1 alias3;
  6864. (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
  6865. NULL
  6866. NULL
  6867. NULL
  6868. NULL
  6869. NULL
  6870. NULL
  6871. NULL
  6872. NULL
  6873. show status like "subquery_cache%";
  6874. Variable_name Value
  6875. Subquery_cache_hit 6
  6876. Subquery_cache_miss 2
  6877. show status like '%Handler_read%';
  6878. Variable_name Value
  6879. Handler_read_first 0
  6880. Handler_read_key 8
  6881. Handler_read_last 0
  6882. Handler_read_next 0
  6883. Handler_read_prev 0
  6884. Handler_read_retry 0
  6885. Handler_read_rnd 0
  6886. Handler_read_rnd_deleted 0
  6887. Handler_read_rnd_next 22
  6888. set @@expensive_subquery_limit= default;
  6889. EXPLAIN
  6890. SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
  6891. FROM t2 alias1, t1 alias2, t1 alias3;
  6892. id select_type table type possible_keys key key_len ref rows Extra
  6893. 1 PRIMARY alias1 ALL NULL NULL NULL NULL 2
  6894. 1 PRIMARY alias2 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join)
  6895. 1 PRIMARY alias3 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join)
  6896. 2 SUBQUERY t1 ALL NULL NULL NULL NULL 2
  6897. 2 SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
  6898. 3 SUBQUERY t3 ALL NULL NULL NULL NULL 2
  6899. flush status;
  6900. SELECT (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
  6901. FROM t2 alias1, t1 alias2, t1 alias3;
  6902. (SELECT MIN(b) FROM t1, t2 WHERE b = a AND (b = alias1.b OR EXISTS (SELECT * FROM t3)))
  6903. NULL
  6904. NULL
  6905. NULL
  6906. NULL
  6907. NULL
  6908. NULL
  6909. NULL
  6910. NULL
  6911. show status like "subquery_cache%";
  6912. Variable_name Value
  6913. Subquery_cache_hit 0
  6914. Subquery_cache_miss 0
  6915. show status like '%Handler_read%';
  6916. Variable_name Value
  6917. Handler_read_first 0
  6918. Handler_read_key 0
  6919. Handler_read_last 0
  6920. Handler_read_next 0
  6921. Handler_read_prev 0
  6922. Handler_read_retry 0
  6923. Handler_read_rnd 0
  6924. Handler_read_rnd_deleted 0
  6925. Handler_read_rnd_next 16
  6926. drop table t1, t2, t3;
  6927. #
  6928. # MDEV-288 CHEAP SQ: Valgrind warnings "Memory lost" with IN and EXISTS nested subquery, materialization+semijoin
  6929. #
  6930. CREATE TABLE t1 (a INT);
  6931. INSERT INTO t1 VALUES (0),(8);
  6932. CREATE TABLE t2 (b INT PRIMARY KEY);
  6933. INSERT INTO t2 VALUES (1),(2);
  6934. EXPLAIN
  6935. SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1));
  6936. id select_type table type possible_keys key key_len ref rows Extra
  6937. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  6938. 2 MATERIALIZED NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
  6939. 3 SUBQUERY t1 ALL NULL NULL NULL NULL 2
  6940. SELECT * FROM t1 WHERE 4 IN (SELECT MAX(b) FROM t2 WHERE EXISTS (SELECT * FROM t1));
  6941. a
  6942. drop table t1,t2;
  6943. #
  6944. # MDEV-410: EXPLAIN shows type=range, while SHOW EXPLAIN and userstat show full table scan is used
  6945. #
  6946. CREATE TABLE t1 (a VARCHAR(3) PRIMARY KEY) ENGINE=MyISAM;
  6947. INSERT INTO t1 VALUES ('USA');
  6948. CREATE TABLE t2 (b INT, c VARCHAR(52), KEY(b)) ENGINE=MyISAM;
  6949. INSERT INTO t2 VALUES (3813,'United States'),(3940,'Russia');
  6950. CREATE TABLE t3 (d INT, KEY(d)) ENGINE=MyISAM;
  6951. INSERT INTO t3 VALUES (12),(22),(9),(45);
  6952. create table t4 like t3;
  6953. insert into t4 select * from t3;
  6954. # This should not show range access for table t2
  6955. explain
  6956. SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1
  6957. WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
  6958. id select_type table type possible_keys key key_len ref rows Extra
  6959. 1 PRIMARY t1 system NULL NULL NULL NULL 1
  6960. 1 PRIMARY t2 ALL b NULL NULL NULL 2 Using where
  6961. 1 PRIMARY t3 ref d d 5 test.t2.b 2 Using where; Using index
  6962. 3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
  6963. set @tmp_mdev410=@@global.userstat;
  6964. set global userstat=on;
  6965. flush table_statistics;
  6966. flush index_statistics;
  6967. SELECT MIN(b) FROM ( SELECT * FROM t1, t2, t3 WHERE d = b ) AS alias1
  6968. WHERE SLEEP(0.1) OR c < 'p' OR b = ( SELECT MIN(b) FROM t2 );
  6969. MIN(b)
  6970. NULL
  6971. # The following shows that t2 was indeed scanned with a full scan.
  6972. show table_statistics;
  6973. Table_schema Table_name Rows_read Rows_changed Rows_changed_x_#indexes
  6974. test t1 2 0 0
  6975. test t2 3 0 0
  6976. show index_statistics;
  6977. Table_schema Table_name Index_name Rows_read
  6978. test t2 b 1
  6979. set global userstat=@tmp_mdev410;
  6980. DROP TABLE t1,t2,t3,t4;
  6981. #
  6982. # MDEV-430: Server crashes in select_describe on EXPLAIN with
  6983. # materialization+semijoin, 2 nested subqueries, aggregate functions
  6984. #
  6985. CREATE TABLE t1 (a INT, KEY(a));
  6986. INSERT INTO t1 VALUES (1),(8);
  6987. CREATE TABLE t2 (b INT, KEY(b));
  6988. INSERT INTO t2 VALUES (45),(17),(20);
  6989. EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
  6990. id select_type table type possible_keys key key_len ref rows Extra
  6991. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  6992. 2 SUBQUERY t1 range a a 5 NULL 2 Using where; Using index
  6993. 2 SUBQUERY t2 ref b b 5 test.t1.a 2 Using index
  6994. DROP TABLE t1,t2;
  6995. #
  6996. # MDEV-435: Expensive subqueries may be evaluated during optimization in merge_key_fields
  6997. #
  6998. CREATE TABLE t1 (a INT, KEY(a)) ENGINE=MyISAM;
  6999. INSERT INTO t1 VALUES (8),(0);
  7000. CREATE TABLE t2 (b INT, c VARCHAR(1)) ENGINE=MyISAM;
  7001. INSERT INTO t2 VALUES (4,'j'),(6,'v');
  7002. CREATE TABLE t3 (d VARCHAR(1)) ENGINE=MyISAM;
  7003. INSERT INTO t3 VALUES ('b'),('c');
  7004. EXPLAIN
  7005. SELECT * FROM t1
  7006. WHERE a = (SELECT MAX(b) FROM t2 WHERE c IN (SELECT MAX(d) FROM t3)) OR a = 10;
  7007. id select_type table type possible_keys key key_len ref rows Extra
  7008. 1 PRIMARY t1 index a a 5 NULL 2 Using where; Using index
  7009. 2 SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where
  7010. 2 SUBQUERY <subquery3> eq_ref distinct_key distinct_key 4 test.t2.c 1
  7011. 3 MATERIALIZED t3 ALL NULL NULL NULL NULL 2
  7012. SELECT * FROM t1
  7013. WHERE a = (SELECT MAX(b) FROM t2 WHERE c IN (SELECT MAX(d) FROM t3)) OR a = 10;
  7014. a
  7015. drop table t1, t2, t3;
  7016. #
  7017. # MDEV-405: Server crashes in test_if_skip_sort_order on EXPLAIN with GROUP BY and HAVING in EXISTS subquery
  7018. #
  7019. CREATE TABLE t1 (a INT, KEY(a));
  7020. INSERT INTO t1 VALUES (1),(8);
  7021. CREATE TABLE t2 (b INT, KEY(b));
  7022. INSERT INTO t2 VALUES (45),(17),(20);
  7023. EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
  7024. id select_type table type possible_keys key key_len ref rows Extra
  7025. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  7026. 2 SUBQUERY t1 range a a 5 NULL 2 Using where; Using index
  7027. 2 SUBQUERY t2 ref b b 5 test.t1.a 2 Using index
  7028. DROP TABLE t1,t2;
  7029. #
  7030. # MDEV-5991: crash in Item_field::used_tables
  7031. #
  7032. create table t1 (c int);
  7033. select exists(select 1 from t1 group by `c` in (select `c` from t1));
  7034. exists(select 1 from t1 group by `c` in (select `c` from t1))
  7035. 0
  7036. drop table t1;
  7037. #
  7038. # MDEV-7565: Server crash with Signal 6 (part 2)
  7039. #
  7040. Select
  7041. (Select Sum(`TestCase`.Revenue) From mysql.slow_log E
  7042. Where TestCase.TemplateID not in (Select 1 from mysql.slow_log where 2=2)
  7043. ) As `ControlRev`
  7044. From
  7045. (Select 3 as Revenue, 4 as TemplateID) As `TestCase`
  7046. Group By TestCase.Revenue, TestCase.TemplateID;
  7047. ControlRev
  7048. NULL
  7049. #
  7050. # MDEV-7445:Server crash with Signal 6
  7051. #
  7052. CREATE PROCEDURE procedure2()
  7053. BEGIN
  7054. Select
  7055. (Select Sum(`TestCase`.Revenue) From mysql.slow_log E
  7056. Where TestCase.TemplateID not in (Select 1 from mysql.slow_log where 2=2)
  7057. ) As `ControlRev`
  7058. From
  7059. (Select 3 as Revenue, 4 as TemplateID) As `TestCase`
  7060. Group By TestCase.Revenue, TestCase.TemplateID;
  7061. END |
  7062. call procedure2();
  7063. ControlRev
  7064. NULL
  7065. call procedure2();
  7066. ControlRev
  7067. NULL
  7068. drop procedure procedure2;
  7069. #
  7070. # MDEV-7846:Server crashes in Item_subselect::fix
  7071. #_fields or fails with Thread stack overrun
  7072. #
  7073. CREATE TABLE t1 (column1 INT) ENGINE=MyISAM;
  7074. INSERT INTO t1 VALUES (3),(9);
  7075. CREATE TABLE t2 (column2 INT) ENGINE=MyISAM;
  7076. INSERT INTO t2 VALUES (1),(4);
  7077. CREATE TABLE t3 (column3 INT) ENGINE=MyISAM;
  7078. INSERT INTO t3 VALUES (6),(8);
  7079. CREATE TABLE t4 (column4 INT) ENGINE=MyISAM;
  7080. INSERT INTO t4 VALUES (2),(5);
  7081. PREPARE stmt FROM "
  7082. SELECT (
  7083. SELECT MAX( table1.column1 ) AS field1
  7084. FROM t1 AS table1
  7085. WHERE table3.column3 IN ( SELECT table2.column2 AS field2 FROM t2 AS table2 )
  7086. ) AS sq
  7087. FROM t3 AS table3, t4 AS table4 GROUP BY sq
  7088. ";
  7089. EXECUTE stmt;
  7090. sq
  7091. NULL
  7092. EXECUTE stmt;
  7093. sq
  7094. NULL
  7095. deallocate prepare stmt;
  7096. drop table t1,t2,t3,t4;
  7097. #
  7098. # MDEV-7122
  7099. # Assertion `0' failed in subselect_hash_sj_engine::init
  7100. #
  7101. SET SESSION big_tables=1;
  7102. CREATE TABLE t1(a char(255) DEFAULT '', KEY(a(10))) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
  7103. INSERT INTO t1 VALUES(0),(0),(0);
  7104. SELECT * FROM t1 WHERE a IN(SELECT MIN(a) FROM t1);
  7105. a
  7106. 0
  7107. 0
  7108. 0
  7109. DROP TABLE t1;
  7110. SET SESSION big_tables=0;
  7111. #
  7112. # MDEV-10776: Server crash on query
  7113. #
  7114. create table t1 (field1 int);
  7115. insert into t1 values (1);
  7116. select round((select 1 from t1 limit 1))
  7117. from t1
  7118. group by round((select 1 from t1 limit 1));
  7119. round((select 1 from t1 limit 1))
  7120. 1
  7121. drop table t1;
  7122. #
  7123. # MDEV-7930: Assertion `table_share->tmp_table != NO_TMP_TABLE ||
  7124. # m_lock_type != 2' failed in handler::ha_index_read_map
  7125. #
  7126. CREATE TABLE t1 (f1 INT);
  7127. INSERT INTO t1 VALUES (1),(2);
  7128. CREATE TABLE t2 (f2 INT, KEY(f2));
  7129. INSERT INTO t2 VALUES (3);
  7130. CREATE ALGORITHM=MERGE VIEW v2 AS SELECT * FROM t2;
  7131. SELECT ( SELECT MIN(t2.f2) FROM t1 ) AS sq FROM t2 GROUP BY sq;
  7132. ERROR 42000: Can't group on 'sq'
  7133. SELECT ( SELECT MIN(v2.f2) FROM t1 ) AS sq FROM v2 GROUP BY sq;
  7134. ERROR 42000: Can't group on 'sq'
  7135. SELECT * FROM v2 where ( SELECT MIN(v2.f2) FROM t1 ) > 0;
  7136. f2
  7137. 3
  7138. SELECT count(*) FROM v2 group by ( SELECT MIN(v2.f2) FROM t1 );
  7139. count(*)
  7140. 1
  7141. delete from t1;
  7142. SELECT ( SELECT MIN(t2.f2) FROM t1 ) AS sq FROM t2 GROUP BY sq;
  7143. ERROR 42000: Can't group on 'sq'
  7144. SELECT ( SELECT MIN(v2.f2) FROM t1 ) AS sq FROM v2 GROUP BY sq;
  7145. ERROR 42000: Can't group on 'sq'
  7146. drop view v2;
  7147. drop table t1,t2;
  7148. #
  7149. # MDEV-10386 Assertion `fixed == 1' failed in virtual String* Item_func_conv_charset::val_str(String*)
  7150. #
  7151. CREATE TABLE t1 (f1 CHAR(3) CHARACTER SET utf8 NULL, f2 CHAR(3) CHARACTER SET latin1 NULL);
  7152. INSERT INTO t1 VALUES ('foo','bar');
  7153. SELECT * FROM t1 WHERE f2 >= SOME ( SELECT f1 FROM t1 );
  7154. f1 f2
  7155. SELECT * FROM t1 WHERE f2 <= SOME ( SELECT f1 FROM t1 );
  7156. f1 f2
  7157. foo bar
  7158. DROP TABLE t1;
  7159. #
  7160. # MDEV-10146: Wrong result (or questionable result and behavior)
  7161. # with aggregate function in uncorrelated SELECT subquery
  7162. #
  7163. CREATE TABLE t1 (f1 INT);
  7164. CREATE VIEW v1 AS SELECT * FROM t1;
  7165. INSERT INTO t1 VALUES (1),(2);
  7166. CREATE TABLE t2 (f2 int);
  7167. INSERT INTO t2 VALUES (3);
  7168. SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1;
  7169. ( SELECT MAX(f1) FROM t2 )
  7170. 2
  7171. SELECT ( SELECT MAX(f1) FROM t2 ) FROM v1;
  7172. ( SELECT MAX(f1) FROM t2 )
  7173. 2
  7174. INSERT INTO t2 VALUES (4);
  7175. SELECT ( SELECT MAX(f1) FROM t2 ) FROM v1;
  7176. ERROR 21000: Subquery returns more than 1 row
  7177. SELECT ( SELECT MAX(f1) FROM t2 ) FROM t1;
  7178. ERROR 21000: Subquery returns more than 1 row
  7179. drop view v1;
  7180. drop table t1,t2;
  7181. CREATE TABLE t1 (f1 INT, KEY(f1)) ENGINE=MyISAM;
  7182. INSERT t1 VALUES (4),(8);
  7183. CREATE TABLE t2 (f2 INT, KEY(f2)) ENGINE=MyISAM;
  7184. INSERT t2 VALUES (6);
  7185. SELECT (SELECT MAX(sq.f2) FROM t1) FROM (SELECT * FROM t2) AS sq WHERE f2 = 2;
  7186. (SELECT MAX(sq.f2) FROM t1)
  7187. NULL
  7188. #
  7189. # Disable this query till MDEV-13399 is resolved
  7190. #
  7191. # INSERT t2 VALUES (9);
  7192. # --error ER_SUBQUERY_NO_1_ROW
  7193. # SELECT (SELECT MAX(sq.f2) FROM t1) FROM (SELECT * FROM t2) AS sq WHERE f2 = 2;
  7194. #
  7195. drop table t1, t2;
  7196. #
  7197. # MDEV-13933: Wrong results in COUNT() query with EXISTS and exists_to_in
  7198. # (5.5 test)
  7199. #
  7200. SET @optimiser_switch_save= @@optimizer_switch;
  7201. CREATE TABLE t1 (a INT NOT NULL);
  7202. INSERT INTO t1 VALUES (1),(1),(1),(5),(5);
  7203. CREATE TABLE t2 (b INT);
  7204. INSERT INTO t2 VALUES (5),(1);
  7205. CREATE TABLE t3 (c INT, KEY(c));
  7206. INSERT INTO t3 VALUES (5),(5);
  7207. SET optimizer_switch='semijoin=on';
  7208. select t1.a from t1 where t1.a in (select `test`.`t2`.`b` from `test`.`t2`)
  7209. and t1.a in (select `test`.`t3`.`c` from `test`.`t3`);
  7210. a
  7211. 5
  7212. 5
  7213. SET optimizer_switch='semijoin=off';
  7214. select t1.a from t1 where t1.a in (select `test`.`t2`.`b` from `test`.`t2`)
  7215. and t1.a in (select `test`.`t3`.`c` from `test`.`t3`);
  7216. a
  7217. 5
  7218. 5
  7219. SET @@optimizer_switch= @optimiser_switch_save;
  7220. DROP TABLE t1, t2, t3;
  7221. #
  7222. # MDEV-16820: impossible where with inexpensive subquery
  7223. #
  7224. create table t1 (a int) engine=myisam;
  7225. insert into t1 values (3), (1), (7);
  7226. create table t2 (b int, index idx(b));
  7227. insert into t2 values (2), (5), (3), (2);
  7228. explain select * from t1 where (select max(b) from t2) = 10;
  7229. id select_type table type possible_keys key key_len ref rows Extra
  7230. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  7231. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
  7232. explain select * from t1 where (select max(b) from t2) = 10 and t1.a > 3;
  7233. id select_type table type possible_keys key key_len ref rows Extra
  7234. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  7235. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
  7236. drop table t1,t2;
  7237. End of 5.5 tests
  7238. # End of 10.0 tests
  7239. #
  7240. # MDEV-9487: Server crashes in Time_and_counter_tracker::incr_loops
  7241. # with UNION in ALL subquery
  7242. #
  7243. SET NAMES utf8;
  7244. CREATE TABLE t1 (f VARCHAR(8)) ENGINE=MyISAM;
  7245. INSERT INTO t1 VALUES ('foo');
  7246. SELECT f FROM t1 WHERE f > ALL ( SELECT 'bar' UNION SELECT 'baz' );
  7247. f
  7248. foo
  7249. SELECT f FROM t1 WHERE f > ALL ( SELECT 'bar');
  7250. f
  7251. foo
  7252. drop table t1;
  7253. SET NAMES default;
  7254. #
  7255. # MDEV-10045: Server crashes in Time_and_counter_tracker::incr_loops
  7256. #
  7257. SET NAMES utf8;
  7258. CREATE TABLE t1 (f1 VARCHAR(3), f2 INT UNSIGNED) ENGINE=MyISAM;
  7259. CREATE TABLE t2 (f3 INT) ENGINE=MyISAM;
  7260. SELECT * FROM t1, t2 WHERE f3 = f2 AND f1 > ANY ( SELECT 'foo' UNION SELECT 'bar' );
  7261. f1 f2 f3
  7262. SELECT * FROM t1, t2 WHERE f3 = f2 AND f1 > ANY ( SELECT 'foo');
  7263. f1 f2 f3
  7264. DROP TABLE t1, t2;
  7265. SET NAMES default;
  7266. # End of 10.1 tests
  7267. #
  7268. # MDEV-12564: IN TO EXISTS transformation for rows after
  7269. # conversion an outer join to inner join
  7270. #
  7271. CREATE TABLE t (
  7272. pk int PRIMARY KEY, i int NOT NULL, c varchar(8), KEY(c)
  7273. ) ENGINE=MyISAM;
  7274. INSERT INTO t VALUES (1,10,'foo'),(2,20,'bar');
  7275. SELECT * FROM t t1 RIGHT JOIN t t2 ON (t2.pk = t1.pk)
  7276. WHERE (t2.i, t2.pk) NOT IN ( SELECT t3.i, t3.i FROM t t3, t t4 ) AND t1.c = 'foo';
  7277. pk i c pk i c
  7278. 1 10 foo 1 10 foo
  7279. DROP TABLE t;
  7280. # End of 10.2 tests
  7281. #
  7282. # Start of 10.4 tests
  7283. #
  7284. #
  7285. # MDEV-16861 Split Item::update_null_value() into a new virtual method in Type_handler
  7286. #
  7287. SELECT ROW(1,2) = EXISTS (SELECT 1);
  7288. ERROR HY000: Illegal parameter data types row and boolean for operation '='
  7289. SELECT ROW(1,2) = 1 IN (SELECT 1 UNION SELECT 2);
  7290. ERROR HY000: Illegal parameter data types row and boolean for operation '='
  7291. SELECT ROW(1,2) = (1 = ANY (SELECT 1 UNION SELECT 2));
  7292. ERROR HY000: Illegal parameter data types row and boolean for operation '='
  7293. #
  7294. # End of 10.4 tests
  7295. #
  7296. set @optimizer_switch_for_subselect_test=null;
  7297. set @join_cache_level_for_subselect_test=NULL;