Bug #38767 combination of 'ORDER BY' and 'LIMIT' yields incorrect output
Submitted: 13 Aug 2008 11:50 Modified: 13 Sep 2008 15:16
Reporter: INSOO KIM Email Updates:
Status: No Feedback Impact on me:
None 
Category:MySQL Server: DML Severity:S1 (Critical)
Version:5.0.67 OS:MacOS (10.5.1 leopard)
Assigned to: CPU Architecture:Any
Tags: incorrect result, misorder

[13 Aug 2008 11:50] INSOO KIM
Description:
SELECT id, commentable_type, parent_id FROM exp_comments WHERE commentable_type = 'ExpChart' ORDER BY id DESC LIMIT 3;
+----+------------------+-----------+
| id | commentable_type | parent_id |
+----+------------------+-----------+
|  4 | ExpChart         |      NULL | 
|  5 | ExpChart         |      NULL | 
|  6 | ExpChart         |      NULL | 
+----+------------------+-----------+
3 rows in set (0.00 sec)

SELECT id, commentable_type FROM exp_comments WHERE commentable_type = 'ExpChart' ORDER BY id DESC LIMIT 3;
+----+------------------+
| id | commentable_type |
+----+------------------+
| 10 | ExpChart         | 
|  8 | ExpChart         | 
|  7 | ExpChart         | 
+----+------------------+
3 rows in set (0.00 sec)

SELECT * FROM exp_comments WHERE commentable_type = 'ExpChart' ORDER BY id DESC LIMIT 6;
+----+------------------+-----------+
| id | commentable_type | parent_id |
+----+------------------+-----------+
| 10 | ExpChart         |      NULL | 
|  8 | ExpChart         |      NULL | 
|  7 | ExpChart         |      NULL | 
|  6 | ExpChart         |      NULL | 
|  5 | ExpChart         |      NULL | 
|  4 | ExpChart         |      NULL | 
+----+------------------+--

How to repeat:
import the table below

then it can be easily repeated

Suggested fix:
-- MySQL dump 10.11
--
-- Host: localhost    Database: cayman_development
-- ------------------------------------------------------
-- Server version	5.0.67

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `exp_comments`
--

DROP TABLE IF EXISTS `exp_comments`;
SET @saved_cs_client     = @@character_set_client;
SET character_set_client = utf8;
CREATE TABLE `exp_comments` (
  `id` int(11) NOT NULL auto_increment,
  `commentable_type` varchar(50) NOT NULL,
  `parent_id` int(11) default NULL,
  PRIMARY KEY  (`id`),
  KEY `exp_comments_index2` (`commentable_type`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
SET character_set_client = @saved_cs_client;

--
-- Dumping data for table `exp_comments`
--

LOCK TABLES `exp_comments` WRITE;
/*!40000 ALTER TABLE `exp_comments` DISABLE KEYS */;
INSERT INTO `exp_comments` VALUES (1,'ExpStory',NULL),(4,'ExpChart',NULL),(5,'ExpChart',NULL),(6,'ExpChart',NULL),(7,'ExpChart',NULL),(8,'ExpChart',NULL),(9,'ExpStory',NULL),(10,'ExpChart',NULL);
/*!40000 ALTER TABLE `exp_comments` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2008-08-13 11:25:53
[13 Aug 2008 11:52] INSOO KIM
sql dump file

Attachment: a.sql (application/octet-stream, text), 844 bytes.

[13 Aug 2008 15:16] MySQL Verification Team
Thank you for the bug report. Looks like duplicate of: http://bugs.mysql.com/bug.php?id=37964.
[13 Sep 2008 23:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".