There is an issue with the SQL result set

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: SQL结果集有问题

| username: 特雷西-迈克-格雷迪

【TiDB Usage Environment】Production
【TiDB Version】v6.1
【Problem Encountered】The result set of the query is incorrect
【Reproduction Path】What operations were performed to cause the issue
【Problem Phenomenon and Impact】

create table test.Tab_A (id int primary key, bid int, cid int, name varchar(20), type varchar(20), num int, amt decimal(11,2));
create table test.Tab_B (id int primary key, name varchar(20));
create table test.Tab_C (id int primary key, name varchar(20), amt decimal(11,2));

insert into test.Tab_A values(1,1,1,‘A01’,‘01’,111,111);
insert into test.Tab_A values(2,2,2,‘A01’,‘01’,112,111);
insert into test.Tab_A values(3,3,3,‘A02’,‘02’,113,111);
insert into test.Tab_A values(4,4,4,‘A02’,‘02’,112,111);
insert into test.Tab_A values(5,5,5,‘A01’,‘01’,111,111);
insert into test.Tab_A values(6,6,6,‘A02’,‘02’,113,111);
insert into test.Tab_A values(7,5,7,‘A01’,‘01’,111,88);
insert into test.Tab_A values(8,6,8,‘A02’,‘02’,113,88);
insert into test.Tab_B values(1,‘B01’);
insert into test.Tab_B values(2,‘B01’);
insert into test.Tab_B values(3,‘B02’);
insert into test.Tab_B values(4,‘B02’);
insert into test.Tab_B values(5,‘B02’);
insert into test.Tab_B values(6,‘B02’);
insert into test.Tab_C values(1,‘C01’,3);
insert into test.Tab_C values(2,‘C01’,22);
insert into test.Tab_C values(3,‘C01’,32);
insert into test.Tab_C values(4,‘C01’,5);
insert into test.Tab_C values(5,‘C01’,6);
insert into test.Tab_C values(6,‘C01’,9);

The following SQL is executed

select Tab_A.name AAA, Tab_B.name BBB, Tab_A.amt Aamt, Tab_C.amt Bamt, IFNULL(Tab_C.amt, 0)
FROM Tab_A left join Tab_B on Tab_A.bid=Tab_B.id
left join Tab_C on Tab_A.cid=Tab_C.id and Tab_A.type=‘01’
where Tab_A.num=112
±-----------------------------------------------------------+
AAA BBB Aamt Bamt IFNULL(Tab_C.amt, 0)
A01 B01 111.00 22.00 22.00
A02 B02 111.00 NULL 0
±-----------------------------------------------------------+

select Tab_A.name AAA, Tab_B.name BBB, COUNT(Tab_A.id) times,
SUM(Tab_A.amt) - SUM(IFNULL(Tab_C.amt, 0)) amt
FROM Tab_A left join Tab_B on Tab_A.bid=Tab_B.id
left join Tab_C on Tab_A.cid=Tab_C.id and Tab_A.type=‘01’
where Tab_A.num=112
group by Tab_A.name, Tab_B.name
±---------------------------------------------+
AAA BBB times amt
A01 B01 1 89.00
A02 NULL 1 106.00
±--------------------------------------------+
【Attachments】

Please provide the version information of each component, such as cdc/tikv, which can be obtained by executing cdc version/tikv-server --version.

| username: h5n1 | Original post link

Tried it on 5.2.3 and 4.0.13, and the condition after “and” didn’t work.

| username: 特雷西-迈克-格雷迪 | Original post link

I can’t view images directly. Please provide the text you need translated.

| username: ShawnYan | Original post link

Is the example SQL correct? The results don’t match up? This is 6.1

| username: OnTheRoad | Original post link

The results from version 6.1 of TiDB are not quite the same.

| username: 特雷西-迈克-格雷迪 | Original post link

select tidb_version();
====================================
Release Version: v6.1.0
Edition: Community
Git Commit Hash: 1a89decdb192cbdce6a7b0020d71128bc964d30f
Git Branch: heads/refs/tags/v6.1.0
UTC Build Time: 2022-06-05 05:15:11
GoVersion: go1.18.2
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false

select * from Tab_A
====================================
1	1	1	A01	01	111	111.00
2	2	2	A01	01	112	111.00
3	3	3	A02	02	113	111.00
4	4	4	A02	02	112	111.00
5	5	5	A01	01	111	111.00
6	6	6	A02	02	113	111.00
7	5	7	A01	01	111	88.00
8	6	8	A02	02	113	88.00

select * from Tab_B
=====================================
1	B01
2	B01
3	B02
4	B02
5	B02
6	B02

select * from Tab_C
=====================================
1	C01	3.00
2	C01	22.00
3	C01	32.00
4	C01	5.00
5	C01	6.00
6	C01	9.00

select Tab_A.name AAA,Tab_A.id,Tab_B.name BBB,Tab_A.amt Aamt, Tab_C.amt Bamt,IFNULL(Tab_C.amt, 0)
FROM Tab_A left join Tab_B on Tab_A.bid=Tab_B.id
left join Tab_C on Tab_A.cid=Tab_C.id and Tab_A.type='01'
where Tab_A.num=112
===============================================
A01	2	B01	111.00	22.00	22.00
A02	4	B02	111.00	NULL     0

select Tab_A.name AAA,Tab_B.name BBB,COUNT(Tab_A.id) times,
       SUM(Tab_A.amt) - SUM(IFNULL(Tab_C.amt, 0)) amt
FROM Tab_A left join Tab_B on Tab_A.bid=Tab_B.id
     left join Tab_C on Tab_A.cid=Tab_C.id and Tab_A.type='01'
where Tab_A.num=112
group by Tab_A.name,Tab_B.name
===============================================
A01	B01	    1	89.00
A02	NULL 	1	106.00
| username: 特雷西-迈克-格雷迪 | Original post link

I have an offline installation of v6.1 here. Could the offline and online installation packages be different?
Based on the image you posted, I have two questions:

  1. Why does the second row in your image have a NULL value? Shouldn’t it display the name from Tab_B?
  2. Can you check the row with id=4 in Tab_A, where the type is ‘02’? According to SQL semantics, shouldn’t the Bamt in the second row of your image be null?
| username: 特雷西-迈克-格雷迪 | Original post link

I repeated the operation, and the result is the same as what I posted; I didn’t make a mistake.

| username: forever | Original post link

I am also using 6.0, but mine is different from yours :sweat_smile:

| username: 数据小黑 | Original post link

6.1 Test Results on k8s


image

| username: crazycs520-PingCAP | Original post link

I reproduced the results comparing TiDB and MySQL, and they are different. It seems that the result set from TiDB has some issues. Let’s investigate further.

| username: OnTheRoad | Original post link

The image you provided is not accessible. Please provide the text content you need translated.

| username: crazycs520-PingCAP | Original post link

The issue has been preliminarily identified, and it should be a bug introduced in v6.1.0. Previous versions did not have this problem. Thank you for discovering the bug :heart:

| username: crazycs520-PingCAP | Original post link

For the follow-up repair progress, see the issue: select with join return wrong result · Issue #37238 · pingcap/tidb · GitHub

| username: OnTheRoad | Original post link

  1. Currently, TiDB does not support directly modifying the primary key of a table. You can achieve this through the following steps:

    • Create a new table with the new primary key.
    • Import the data from the old table into the new table.
    • Delete the old table.
    • Rename the new table to the old table’s name.
  2. You can use the SHOW CREATE TABLE command to view the table definition, including information about the primary key.

| username: wuxiangdong | Original post link

A left join B on A.id=B.id and A.id=num,
A left join B on A.id=B.id where A.id=num
The condition after “on” is for equality association, and the filter condition should be written after “where”. I am not sure if there are any parameters in sql_mode that restrict the first way of writing.

| username: system | Original post link

This topic was automatically closed 60 days after the last reply. No new replies are allowed.