【发布时间】:2019-10-09 22:55:13
【问题描述】:
我正在尝试做一个简单的选择查询,它将过滤掉重复数据,就像在 5.6 以下的 mysql 版本中可能的那样。
我在网上做了很多研究,我已经尝试了我发现的所有东西,但我似乎仍然没有得到要点。
CREATE TABLE Test (
id int(11) unsigned NOT NULL AUTO_INCREMENT,
Comment text,
commentOnId int(11) DEFAULT NULL,
Time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
insert into test (comment, commentonid)
VALUES
('My first comment', null),
('reply 1',1),
('reply 2',1);`
select id, comment, commentonid from Test group by commentonid;
select id, comment, distinct commentonid from Test group by commentonid;
select id, comment, distinct commentonid from Test;
select id, comment, ANY_VALUE(commentonid) as unique from Test group by unique;
select id, comment, distinct ANY_VALUE(commentonid) from Test;`
`select id, comment, MIN(commentonid) from Test;
每一个选择查询都会失败,或者至少没有得到想要的结果。
我想获得一个唯一的commentOn 字段,因为有两个值为1。为了在 mysql 8.0 中获取唯一数据,正确的选择语句是什么?
【问题讨论】:
-
您是说这些查询在 5.6 中有效,但在 8.0 中无效?你得到什么错误?到目前为止,您尝试过什么来解决问题?文档的哪些方面支持您的解决方案?