mysql结果group_相同的查询返回不同的结果(MySQL Group By)

bd96500e110b49cbb3cd949968f18be7.png

This only happens for queries that force GROUP BY after ORDER BY.

Goal:

Get latest balance for each unit for the given cardID.

Table:

cardID | unit | balance | date

--------|-----------|-----------|--------------

A1 | DEPOSIT | 100 | 2016-05-01

A1 | DEPOSIT | 90 | 2016-05-02

A1 | DEPOSIT | 80 | 2016-05-03

A1 | DEPOSIT | 75 | 2016-05-04

A1 | MINUTE | 1000 | 2016-05-01

A1 | MINUTE | 900 | 2016-05-02

A1 | MINUTE | 800 | 2016-05-03

Query:

SELECT * FROM (

SELECT unit, balance

FROM cardBalances

WHERE cardID = 'A1'

ORDER BY date DESC

) AS cb

GROUP BY cb.unit;

Expected Result (MySQL v5.5.38):

unit | balance

---------|-----------

DEPOSIT | 75

MINUTE | 800

Unexpected Result (MySQL v5.7.13):

unit | balance

---------|-----------

DEPOSIT | 100

MINUTE | 1000

After upgrading to MySQL v5.7.13, the result returns the initial balances; as if no deduction occurred for the given card.

Is this a bug in MySQL version?

Would you suggest any other, more reliable way to solve this?

解决方案

This is a bug in your use of the database. MySQL is quite explicit that when you include columns in the SELECT clause in an aggregation query -- and they are not in the GROUP BY -- then they come from indeterminate rows.

Such syntax is specific to MySQL. It is not only a bad idea to learn, but it simply normally not work in other databases.

You can do what you want in various ways. Here is one:

SELECT cb.*

FROM cardBalances cb

WHERE cardId = 'A1' AND

cb.date = (SELECT MAX(date)

FROM cardBalances cb2

WHERE cb2.cardId = 'A1' AND cb2.unit = cb.unit

);

This has the advantage that it can use an index on cardBalances(unit, CardId, date).

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值