通过左联接获得Group by以在MySQL 5.7中工作

时间:2019-10-10 11:06:48

标签: mysql

我试图获取一个查询,以便为每个唯一ID仅返回一行。

给出以下内容:

CREATE TABLE `groups` (   `id` int(11) NOT NULL AUTO_INCREMENT,  
`name` varchar(45) DEFAULT NULL,   `type` int(1) DEFAULT NULL,  
`date` datetime DEFAULT NULL,   PRIMARY KEY (`id`) ) ENGINE=InnoDB
AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

CREATE TABLE `groupmembers` (   `id` int(11) NOT NULL AUTO_INCREMENT, 
`username` varchar(255) DEFAULT NULL,   `readmessage` int(1) DEFAULT
NULL,   `status` int(1) DEFAULT NULL,   `group_id` int(20) DEFAULT
NULL,   PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT
CHARSET=latin1;

CREATE TABLE `messages` (   `id` bigint(10) NOT NULL AUTO_INCREMENT,  
`send_to` varchar(50) NOT NULL DEFAULT '',   `name` varchar(50) NOT
NULL DEFAULT '',   `email` varchar(50) NOT NULL DEFAULT '',  
`subject` varchar(100) NOT NULL DEFAULT 'No Subject',   `message`
longtext NOT NULL,   `timestamp` timestamp NOT NULL DEFAULT
CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,   `message_read`
enum('Yes','No') NOT NULL DEFAULT 'No',   `send_time` datetime NOT
NULL DEFAULT '1970-01-01 00:00:00',
  `hide_message` enum('0','1') NOT NULL,   `grouper` int(20) DEFAULT
NULL,   PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT
CHARSET=latin1;

insert into groups (type,date) values ('0',now()); 
insert into groupmembers (username,group_id) values ('user1',5),('user2',5);
insert into messages (send_to,name,send_time,grouper,message) values
('user1','user2','2019-10-09 19:18:12',5,'hello'); 
insert into messages (send_to,name,send_time,grouper,message) values
('','user2','2019-10-10 09:18:39',5,'hello');

在mysql 5.6中,此查询按预期工作:

select groups.id,send_to,messages.name,max(send_time) from groups
inner join messages on groups.id=messages.grouper left join
groupmembers on groups.id=groupmembers.group_id where
groupmembers.username='user1' group by groups.id  ORDER BY
MAX(send_time)

在mysql 5.7中,这将返回错误

Error Code: 1055. Expression #2 of SELECT list is not in GROUP BY
clause and contains nonaggregated column 'messages.send_to' which is
not functionally dependent on columns in GROUP BY clause; this is
incompatible with sql_mode=only_full_group_by

我需要做些什么才能在不调整SQL模式的情况下使它在MySQL 5.7中工作?

在此处查看SQL Fiddle:http://sqlfiddle.com/#!9/9061da/4

它在运行5.6的SQL小提琴上按预期工作。

1 个答案:

答案 0 :(得分:1)

只是一个猜测...

SELECT DISTINCT g.id
              , m.send_to
              , m.name
              , m.send_time 
           FROM groups g
           JOIN messages m
             ON m.grouper = g.id 
           JOIN
              ( SELECT send_to
                     , name
                     , MAX(send_time) send_time
                  FROM messages m
                 GROUP
                    BY send_to
                     , name
              ) x
             ON x.send_to = m.send_to
            AND x.name = m.name
            AND x.send_time = m.send_time
           JOIN groupmembers gm
             ON gm.group_id = g.id 
          WHERE gm.username = 'user1' 
          ORDER 
             BY send_time;