Details
Description
A query with scalar subquery in a comparison returns no result when using "ORDER BY" and "LIMIT" in the scalar subquery. (https://mariadb.com/kb/en/sql-99-complete-really/31-searching-with-subqueries/scalar-subqueries/#comment_1325)
Sample SQL statement:
SELECT SQL_NO_CACHE a.project_number FROM projects a WHERE ( SELECT z.country FROM projects_history z WHERE z.project_number = a.project_number AND z.history_date <= '2014-09-01' ORDER BY z.id DESC LIMIT 1 ) IN ( SELECT r.country FROM region r WHERE r.region = 'eame' );
The query was executed in MariaDB (10.0.3), MySQL(5.6.17) and MySQL(5.0.96). The MySQL(5.0.96) returns the expected result.
See attached files for details (db dump, explain, actual result and expected result)
Gliffy Diagrams
Attachments
Issue Links
- links to
Activity
- All
- Comments
- Work Log
- History
- Activity
- Transitions
Test case (the same as in the description, it's perfect as it is, just put the data and query together and removed comments for MTR's benefit):
CREATE TABLE `projects` ( `project_number` varchar(50) NOT NULL, PRIMARY KEY (`project_number`) ) ENGINE=MyISAM; INSERT INTO `projects` (`project_number`) VALUES ('aaa'),('bbb'); CREATE TABLE IF NOT EXISTS `projects_history` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `project_number` varchar(50) NOT NULL, `history_date` date NOT NULL, `country` varchar(50) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM; INSERT INTO `projects_history` (`id`, `project_number`, `history_date`, `country`) VALUES (1, 'aaa', '2014-08-09', 'france'),(2, 'aaa', '2014-09-09', 'singapore'); CREATE TABLE IF NOT EXISTS `region` ( `region` varchar(50) NOT NULL, `country` varchar(50) NOT NULL ) ENGINE=MyISAM; INSERT INTO `region` (`region`, `country`) VALUES ('apac', 'singapore'),('eame', 'france'); SELECT SQL_NO_CACHE a.project_number FROM projects a WHERE ( SELECT z.country FROM projects_history z WHERE z.project_number = a.project_number AND z.history_date <= '2014-09-01' ORDER BY z.id DESC LIMIT 1 ) IN ( SELECT r.country FROM region r WHERE r.region = 'eame' );