Details
-
Bug
-
Status: Closed (View Workflow)
-
Major
-
Resolution: Duplicate
-
10.0.13
-
None
-
CentOS release 6.5 (Final)
Description
I get the wrong result when I join 2 tables and use group by at the same time.
How I can fix this problem?
MariaDB [workspace]> DROP TABLE IF EXISTS t1;
|
Query OK, 0 rows affected (0.00 sec)
|
|
MariaDB [workspace]> DROP TABLE IF EXISTS t2;
|
Query OK, 0 rows affected (0.00 sec)
|
|
MariaDB [workspace]> CREATE TABLE t1(id CHAR(16), PRIMARY KEY(id));
|
Query OK, 0 rows affected (0.02 sec)
|
|
MariaDB [workspace]> INSERT INTO t1 VALUES ('78622');
|
Query OK, 1 row affected (0.00 sec)
|
|
MariaDB [workspace]> INSERT INTO t1 VALUES ('786220');
|
Query OK, 1 row affected (0.00 sec)
|
|
MariaDB [workspace]> CREATE TABLE t2(id CHAR(16), VAR INT DEFAULT NULL, PRIMARY KEY(id));
|
Query OK, 0 rows affected (0.04 sec)
|
|
MariaDB [workspace]> INSERT INTO t2 VALUES ('78622',1);
|
Query OK, 1 row affected (0.00 sec)
|
MariaDB [workspace]> SELECT id, VAR FROM t1 LEFT JOIN t2 USING(id) GROUP BY id;
|
+--------+------+
|
| id | VAR |
|
+--------+------+
|
| 78622 | NULL |
|
| 786220 | NULL |
|
+--------+------+
|
2 rows in set (0.00 sec)
|
|
MariaDB [workspace]> SELECT id, VAR FROM t1 LEFT JOIN t2 USING(id);
|
+--------+------+
|
| id | VAR |
|
+--------+------+
|
| 78622 | 1 |
|
| 786220 | NULL |
|
+--------+------+
|
2 rows in set (0.00 sec)
|
Attachments
Issue Links
- duplicates
-
MDEV-5719 Wrong result with GROUP BY and LEFT OUTER JOIN
-
- Closed
-
Hi,
Thanks for the report. It's likely to be a duplicate of
MDEV-5719, But I'll assign it to psergey so he could check your test case as well after the bugfix (never hurts to double-check).Regarding your question how to fix the problem, it somewhat depends on what your real data and structures are (I presume you submtted a simplified test case, and it's great, thanks for that!)
In general, a query which uses partial GROUP BY is nonndeterministic. If there were more than one matching row in t2, the returned value of VAR would have been unpredictable, and then the advice would have depended on what your expectations are. But if it's a PK on t2 like in your test case, you'll always get no more than one matching row; so it should be safe just to add VAR to GROUP BY, thus making it more reliable and getting rid of the wrong result, all at once:
| id | VAR |
| 78622 | 1 |
Although, since it's a PK on t1 too, I'm not sure if you need a GROUP BY at all.