据我了解,这样的事情应该有所帮助。我已经包括了第三个团队,其中一个团队成员仍在努力回答这个问题......
create table Teams
(
id int unsigned not null auto_increment primary key,
name varchar(50) not null,
created datetime not null
);
create table Friends
(
id int unsigned not null auto_increment primary key,
name varchar(50) not null,
team_id int unsigned not null,
question varchar(100) default null,
answer varchar(100) default null,
date_answer timestamp default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
insert into Teams (name,created) values ("Team 1",now());
insert into Teams (name,created) values ("Team 2",now());
insert into Teams (name,created) values ("Team 3",now());
insert into Friends (name,team_id,question,answer,date_answer) values ("Bob",1,"What Is Your Name?","Bob",'2012-08-15 01:00:00');
insert into Friends (name,team_id,question,answer,date_answer) values ("Joe",1,"What Is Your Name?","Joe",'2012-08-15 02:00:00');
insert into Friends (name,team_id,question,answer,date_answer) values ("Tim",1,"What Is Your Name?","Tim",'2012-08-15 03:00:00');
insert into Friends (name,team_id,question,answer,date_answer) values ("Tom",1,"What Is Your Name?","Tom",'2012-08-15 15:00:00');
insert into Friends (name,team_id,question,answer,date_answer) values ("Jon",2,"What Is Your Name?","Jon",'2012-08-15 01:00:00');
insert into Friends (name,team_id,question,answer,date_answer) values ("Jim",2,"What Is Your Name?","Jim",'2012-08-15 04:00:00');
insert into Friends (name,team_id,question,answer,date_answer) values ("Josh",2,"What Is Your Name?","Josh",'2012-08-15 01:00:00');
insert into Friends (name,team_id,question,answer,date_answer) values ("Jack",2,"What Is Your Name?","Jack",'2012-08-15 18:00:00');
insert into Friends (name,team_id,question,answer,date_answer) values ("Dave",3,"What Is Your Name?","Jon",'2012-08-15 01:00:00');
insert into Friends (name,team_id,question,answer,date_answer) values ("Dug",3,"What Is Your Name?","Jim",'2012-08-15 04:00:00');
insert into Friends (name,team_id,question,answer,date_answer) values ("Dappy",3,"What Is Your Name?",null,null);
-- Winning team must have an answer from all teams members and have earliest average
-- date_answer
select t.name as "Winning Team",
sum(case when f.answer is not null then 1 else 0 end) as answerCount,
count(*) as teamCount,
timestamp(avg(f.date_answer)) as averageAnswerDate
from Teams t
left join Friends f on f.team_id = t.id
group by t.name
having answerCount = teamCount
order by averageAnswerDate asc
limit 1;