如何告诉MariaDB / MySQL由于HAVING子句,列是唯一的

问题描述 投票:0回答:1

设置了ONLY_FULL_GROUP_BY(我不希望/不能更改),出现以下查询错误,错误为“ ERROR 1055(42000):'dbname.test1.person_id'不在GROUP BY中”(或等效项) mariadb中的较长消息)。

CREATE TABLE test1 (
    thing VARCHAR(16),
    person_id INT
);

SELECT
    thing,
    person_id
FROM
    test1
GROUP BY
    thing
HAVING
    COUNT(DISTINCT person_id) = 1

如何通过HAVING子句“告诉”我知道person_id明确的MySQL?我不想使用ANY_VALUE,因为并非在需要运行此代码的所有数据库中都可用。

mysql sql group-by mariadb having
1个回答
0
投票
SELECT thing, MAX(person_id) AS person_id FROM test1 GROUP BY thing HAVING COUNT(DISTINCT person_id) = 1

person_id已经希望被明确选择。

© www.soinside.com 2019 - 2024. All rights reserved.