and limit mysql,mysql:order by and limit给出错误的结果

MySQL 5.1.26版

我选了一个包含where、order by和limit子句的选项,结果是错误的。

只有当order by使用id列时才有问题。

我从阅读手册中推测,主键ID上的索引有问题。但是我不知道从这里应该去哪里…

问题:我应该怎么做才能最好地解决这个问题?

Works correctly:

mysql> SELECT id, created_at FROM billing_invoices

WHERE (billing_invoices.account_id = 5) ORDER BY id DESC ;

+------+---------------------+

| id | created_at |

+------+---------------------+

| 1336 | 2010-05-14 08:05:25 |

| 1334 | 2010-05-06 08:05:25 |

| 1331 | 2010-05-05 23:18:11 |

+------+---------------------+

3 rows in set (0.00 sec)

WRONG result when limit added! Should be the first row, id - 1336

mysql> SELECT id, created_at FROM billing_invoices

WHERE (billing_invoices.account_id = 5) ORDER BY id DESC limit 1;

+------+---------------------+

| id | created_at |

+------+---------------------+

| 1331 | 2010-05-05 23:18:11 |

+------+---------------------+

1 row in set (0.00 sec)

Works correctly:

mysql> SELECT id, created_at FROM billing_invoices

WHERE (billing_invoices.account_id = 5) ORDER BY created_at DESC ;

+------+---------------------+

| id | created_at |

+------+---------------------+

| 1336 | 2010-05-14 08:05:25 |

| 1334 | 2010-05-06 08:05:25 |

| 1331 | 2010-05-05 23:18:11 |

+------+---------------------+

3 rows in set (0.01 sec)

Works correctly with limit:

mysql> SELECT id, created_at FROM billing_invoices

WHERE (billing_invoices.account_id = 5) ORDER BY created_at DESC limit 1;

+------+---------------------+

| id | created_at |

+------+---------------------+

| 1336 | 2010-05-14 08:05:25 |

+------+---------------------+

1 row in set (0.01 sec)

Additional info:

explain SELECT id, created_at FROM billing_invoices WHERE (billing_invoices.account_id = 5) ORDER BY id DESC limit 1;

+----+-------------+------------------+-------+--------------------------------------+--------------------------------------+---------+------+------+-------------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+----+-------------+------------------+-------+--------------------------------------+--------------------------------------+---------+------+------+-------------+

| 1 | SIMPLE | billing_invoices | range | index_billing_invoices_on_account_id | index_billing_invoices_on_account_id | 4 | NULL | 3 | Using where |

+----+-------------+------------------+-------+--------------------------------------+--------------------------------------+---------+------+------+-------------+

添加显示创建表开票\发票结果:

Table -- billing_invoices

Create Table --

CREATE TABLE `billing_invoices` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`account_id` int(11) NOT NULL,

`invoice_date` date NOT NULL,

`prior_invoice_id` int(11) DEFAULT NULL,

`closing_balance` decimal(8,2) NOT NULL,

`note` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,

`monthly_invoice` tinyint(1) NOT NULL,

`created_at` datetime DEFAULT NULL,

`updated_at` datetime DEFAULT NULL,

PRIMARY KEY (`id`),

KEY `index_billing_invoices_on_account_id` (`account_id`)

) ENGINE=InnoDB AUTO_INCREMENT=1337 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

添加更多:

我现在看到在我的开发机器上,一切都正常工作。那台机器的版本是5.1.26-rc-log

在我身上

生产

machine,在问题所在,我看到version()返回5.1.26-rc-percona-log

所以现在,我想问题出在Percona软件上?

添加更多:

在这一点上,我认为它是Percona InnoDB驱动程序中的一个bug。我放了一个

question to their forum

. 作为一项即时的工作,我将由Created_at订购。我还将研究升级我的系统上的数据库,看看是否有帮助。

感谢Rabbott和MDMA的帮助。我也感谢你的帮助,我没有做傻事,这真的是个问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值