使用IN和ORDER BY子句的Mysql问题

时间:2015-04-05 17:22:35

标签: mysql sql-order-by sql-in

对于以下Mysql表的查询,我遇到了一个奇怪的问题:

CREATE TABLE `BulkTransuploads` (
  `bulktransuploads_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id of the upload',
  `bulktransuploads_time` datetime NOT NULL COMMENT 'The upload time',
  `bulktransuploads_val_id` int(10) unsigned NOT NULL COMMENT 'TransactionID value. value 1',
  `bulktransuploads_val_orderid` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'OrderID value. value 2',
  `bulktransuploads_val_url` varchar(400) NOT NULL COMMENT 'URL value. value 3',
  `bulktransuploads_val_tracking` varchar(40) NOT NULL COMMENT 'Tracking value. value 4',
  `bulktransuploads_status_v1` varchar(150) NOT NULL COMMENT 'Status for value1. OK or error message',
  `bulktransuploads_status_v2` varchar(150) NOT NULL COMMENT 'Status for value2. OK or error message',
  `bulktransuploads_status_v3` varchar(150) NOT NULL COMMENT 'Status for value3. OK or error message',
  `bulktransuploads_status_v4` varchar(150) NOT NULL COMMENT 'Status for value4. OK or error message',
  PRIMARY KEY (`bulktransuploads_id`)
) ENGINE=ARCHIVE AUTO_INCREMENT=242 DEFAULT CHARSET=utf8

我需要提取一些具有id的值,所以我认为最短的方法是使用IN子句。

mysql> SELECT * FROM BulkTransuploads WHERE bulktransuploads_id IN (233,231,232)\G;

以上查询返回以下结果:

*************************** 1. row ***************************
          bulktransuploads_id: 231
*************************** 2. row ***************************
          bulktransuploads_id: 232
*************************** 3. row ***************************
          bulktransuploads_id: 233
3 rows in set (0.00 sec)

我删除了其他列值以在这里节省一些空间。总而言之,这个查询几乎完成了我的预期,尽管我期望一个不同的顺序(233,231,232),因为这个顺序是在查询中编写的(IN子句)。由于我需要将结果按顺序递减,因此修改了我的查询,如下所示:

mysql> SELECT * FROM BulkTransuploads WHERE bulktransuploads_id IN (233,231,232) ORDER BY bulktransuploads_id DESC\G;

我的新查询返回了一个尴尬的结果:

*************************** 1. row ***************************
          bulktransuploads_id: 200
*************************** 2. row ***************************
          bulktransuploads_id: 200
*************************** 3. row ***************************
          bulktransuploads_id: 200
3 rows in set (0.00 sec)

我无法理解为什么它返回3次id 200.查询之间的唯一区别是ORDER BY bulktransuploads_id DESC,因此无疑ORDER BY子句会弄乱我的结果,但我的问题是为什么?

1 个答案:

答案 0 :(得分:0)

  

您需要更改正在使用的ENGINE,而不是使用InnoDB。

将CREATE TABLE查询修改为:

CREATE TABLE `BulkTransuploads` (
  `bulktransuploads_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id of the upload',
  `bulktransuploads_time` datetime NOT NULL COMMENT 'The upload time',
  `bulktransuploads_val_id` int(10) unsigned NOT NULL COMMENT 'TransactionID value. value 1',
  `bulktransuploads_val_orderid` varchar(100) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT 'OrderID value. value 2',
  `bulktransuploads_val_url` varchar(400) NOT NULL COMMENT 'URL value. value 3',
  `bulktransuploads_val_tracking` varchar(40) NOT NULL COMMENT 'Tracking value. value 4',
  `bulktransuploads_status_v1` varchar(150) NOT NULL COMMENT 'Status for value1. OK or error message',
  `bulktransuploads_status_v2` varchar(150) NOT NULL COMMENT 'Status for value2. OK or error message',
  `bulktransuploads_status_v3` varchar(150) NOT NULL COMMENT 'Status for value3. OK or error message',
  `bulktransuploads_status_v4` varchar(150) NOT NULL COMMENT 'Status for value4. OK or error message',
  PRIMARY KEY (`bulktransuploads_id`)
) ENGINE=InnoDB AUTO_INCREMENT=200 DEFAULT CHARSET=utf8;