pingcap / pingcap/tidb

The query result of the sql statement in tidb is inconsistent with mysql.

Open
#58,129 1 comment 0 reactions 0 assignees View on GitHub
severity/major sig/planner type/bug
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

The query result of the sql statement in tidb is inconsistent with mysql.

### 1. Minimal reproduce step (Required)
```
use test;
drop table a;
CREATE TABLE a (
id BIGINT(20) UNSIGNED NOT NULL,
version INT(11) NOT NULL,
PRIMARY KEY (id, version)
);
insert into a values(1,100),(1,102),(2,29),(2,34),(2,60),(2,50),(1,129),(1,128);

SELECT id, version,
@row_num := IF(@id_last = id, @row_num + 1, 1) as rn,
@id_last := id
FROM a, (SELECT @row_num := 0, @id_last := NULL) r
ORDER BY id, version DESC
```

### 2. What did you expect to see? (Required)
```
mysql> SELECT id, version,
-> @row_num := IF(@id_last = id, @row_num + 1, 1) as rn,
-> @id_last := id
-> FROM a, (SELECT @row_num := 0, @id_last := NULL) r
-> ORDER BY id, version DESC;
+----+---------+------+----------------+
| id | version | rn | @id_last := id |
+----+---------+------+----------------+
| 1 | 129 | 1 | 1 |
| 1 | 128 | 2 | 1 |
| 1 | 102 | 3 | 1 |
| 1 | 100 | 4 | 1 |
| 2 | 60 | 1 | 2 |
| 2 | 50 | 2 | 2 |
| 2 | 34 | 3 | 2 |
| 2 | 29 | 4 | 2 |
+----+---------+------+----------------+
8 rows in set, 4 warnings (0.00 sec)

mysql>
```

### 3. What did you see instead (Required)
```
MySQL [test]> SELECT id, version,
-> @row_num := IF(@id_last = id, @row_num + 1, 1) as rn,
-> @id_last := id
-> FROM a, (SELECT @row_num := 0, @id_last := NULL) r
-> ORDER BY id, version DESC;
+----+---------+------+----------------+
| id | version | rn | @id_last := id |
+----+---------+------+----------------+
| 1 | 129 | 4 | 1 |
| 1 | 128 | 3 | 1 |
| 1 | 102 | 2 | 1 |
| 1 | 100 | 1 | 1 |
| 2 | 60 | 4 | 2 |
| 2 | 50 | 3 | 2 |
| 2 | 34 | 2 | 2 |
| 2 | 29 | 1 | 2 |
+----+---------+------+----------------+
8 rows in set (0.01 sec)

MySQL [test]>
```
### 4. What is your TiDB version? (Required)
```
MySQL [test]> select tidb_version()\G
*************************** 1. row ***************************
tidb_version(): Release Version: v8.1.1
Edition: Community
Git Commit Hash: a7df4f9845d5d6a590c5d45dad0dcc9f21aa8765
Git Branch: HEAD
UTC Build Time: 2024-08-22 05:50:03
GoVersion: go1.21.13
Race Enabled: false
Check Table Before Drop: false
Store: tikv
1 row in set (0.00 sec)

MySQL [test]>
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.