pingcap / pingcap/tidb

subquery with aggregated column is not working with ONLY_FULL_GROUP_BY mode

Open
#35,660 1 comment 0 reactions 1 assignee Claimed by @AilinKid View on GitHub
severity/moderate sig/planner type/bug
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

Please answer these questions before submitting your issue. Thanks!

### 1. Minimal reproduce step (Required)
```sql
SET @@sql_mode='ONLY_FULL_GROUP_BY';
drop table if exists t1,t2;
CREATE TABLE t1 (a INT, b INT, c INT DEFAULT 0);
INSERT INTO t1 (a, b) VALUES (3,3), (2,2), (3,3), (2,2), (3,3), (4,4);
CREATE TABLE t2 (a INT, b INT, c INT DEFAULT 0);
INSERT INTO t2 (a, b) VALUES (3,3), (2,2), (3,3), (2,2), (3,3), (4,4);
SELECT t1.a FROM t1 GROUP BY t1.a HAVING (1, 1) = (SELECT SUM(t1.a), t1.a FROM t2 LIMIT 1);
SET @@sql_mode='';
SELECT t1.a FROM t1 GROUP BY t1.a HAVING (1, 1) = (SELECT SUM(t1.a), t1.a FROM t2 LIMIT 1);

```

### 2. What did you expect to see? (Required)
```sql

mysql> SELECT t1.a FROM t1 GROUP BY t1.a HAVING (1, 1) = (SELECT SUM(t1.a), t1.a FROM t2 LIMIT 1);
Empty set (0.00 sec)

mysql> SET @@sql_mode='';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT t1.a FROM t1 GROUP BY t1.a HAVING (1, 1) = (SELECT SUM(t1.a), t1.a FROM t2 LIMIT 1);
Empty set (0.00 sec)

```
### 3. What did you see instead (Required)
```sql
mysql> SET @@sql_mode='ONLY_FULL_GROUP_BY';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT t1.a FROM t1 GROUP BY t1.a HAVING (1, 1) = (SELECT SUM(t1.a), t1.a FROM t2 LIMIT 1);
ERROR 8123 (HY000): In aggregated query without GROUP BY, expression #2 of SELECT list contains nonaggregated column 'a'; this is incompatible with sql_mode=only_full_group_by

mysql> SET @@sql_mode='';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT t1.a FROM t1 GROUP BY t1.a HAVING (1, 1) = (SELECT SUM(t1.a), t1.a FROM t2 LIMIT 1);
ERROR 1052 (23000): Column 'a' in field list is ambiguous
```
### 4. What is your TiDB version? (Required)

```sql
| Release Version: v6.2.0-alpha-103-g395ccbe22
Edition: Community
Git Commit Hash: 395ccbe22d5e0d0f6412f33d9bbf8d90fe52cc66
Git Branch: master
UTC Build Time: 2022-06-13 17:05:18
GoVersion: go1.18
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
Store: unistore |
```

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.