我试图从两个表中获取查询。这应该是直截了当的;但是,我的结果重复了。我不能使用DISTINCT或GROUP BY,因为没有唯一的ID。为了测试,我的主表有87行。没有确定WHERE我得到174结果。我的两张表如下:
table - lifegroups_attendance(日期是日期字段,时间是varchar)
没有唯一ID - 日期/时间/组的组合最接近它
date | time | group| attendance
--------------------------------
1/3/15 | 6:30 PM | Adults | 125
1/3/15 | 11:00 AM | Adults | 621
1/4/15 | Saturday | Kids | 289
1/4/15 | Sun PM | Adults | 621
table - sat_week_date_map(本周的开头是星期六,而不是MySQL内置的Sun或Mon选项)
日期是主要ID
date | week
---------------
1/3/15 | 1
1/4/15 | 1
1/5/15 | 1
1/10/15 | 2
我需要得到一个如下所示的结果:
week | group | attendance
1 | adults | 125
1 | adults | 621
1 | adults | 621 (This is not a duplicate - date/time is different)
2 | adults | 475
2 | adults | 276
2 | kids | 289
相反,我得到了
week | group | attendance
1 | adults | 125
1 | adults | 621
1 | adults | 621 (This is not a duplicate - date/time is different)
2 | adults | 475
2 | adults | 276
2 | kids | 289
1 | adults | 125 (repeats every record over again)
1 | adults | 621
1 | adults | 621 (This is not a duplicate - date/time is different)
2 | adults | 475
2 | adults | 276
2 | kids | 289
我目前使用的查询是:
SELECT wd.week, la.group AS group, la.attendance
FROM sat_week_date_map AS wd JOIN lifegroups_attendance AS la
ON la.date = wd.date;
答案 0 :(得分:0)
您可以使用其他查询来扭曲查询,然后使用不同的:
SELECT distinct *
FROM (SELECT wd.week, la.group AS group, la.attendance
FROM sat_week_date_map AS wd JOIN lifegroups_attendance AS la
ON la.date = wd.date)
答案 1 :(得分:0)
请仔细检查sat_week_date_map中是否有多个相同日期的条目。如果有多个这样的条目,它们将导致明显重复的结果。
SELECT date, COUNT(*) FROM sat_week_date_map GROUP BY date HAVING COUNT(*) > 1;