在子查询中计算年龄返回null

时间:2017-03-02 07:03:47

标签: mysql subquery left-join

使用此查询来计算会议中的参与者数量:

SELECT
a.`ID`, a.`Start`, 
`Attendance`.`Ant` `Participants` 
FROM `school_group_occasion` a 
LEFT JOIN (
  SELECT COUNT(`Id`) `Ant`, `OccId`, `user_birthdate` 
  FROM `school_user_attendance` 
  INNER JOIN `school_user` 
  ON `UserId` = `user_id` 
  WHERE `Type` = 1 

  GROUP BY `OccId` 
) `Attendance` 
ON `Attendance`.`OccId` = a.`ID` 
AND (DATE_FORMAT(a.`Start`, '%Y') - DATE_FORMAT(`Attendance`.`user_birthdate`, '%Y')) BETWEEN 7 AND 25 

出于某种原因,大多数情况下会返回NULL参与者,即使我知道有匹配项:

+------+---------------------+--------------+
| ID   | Start               | Participants |
+------+---------------------+--------------+
| 1384 | 2016-08-28 17:30:00 |         NULL |
| 1385 | 2016-09-04 17:30:00 |         NULL |
| 1386 | 2016-09-11 17:30:00 |         NULL |
| 1387 | 2016-09-18 17:30:00 |         NULL |
| 1388 | 2016-09-25 17:30:00 |         NULL |
| 1389 | 2016-10-02 17:30:00 |         NULL |
| 1390 | 2016-10-09 17:30:00 |           21 |
| 1391 | 2016-10-16 17:30:00 |           17 |
| 1392 | 2016-10-23 17:30:00 |         NULL |
| 1393 | 2016-10-30 17:30:00 |         NULL |
| 1394 | 2016-11-13 17:30:00 |         NULL |
| 1395 | 2016-11-20 17:30:00 |           18 |
+------+---------------------+--------------+

例如,以ID 1384签出场合:(设置自实际开始日期以来的日期不包括在以下查询中)

SET @TheDate = '2016-08-28 17:30:00';

SELECT 
`Id`, `OccId` `Occasion`, 'Foo' `Firstname`, 'Bar' `Lastname`, `user_birthdate`, (DATE_FORMAT(@TheDate, '%Y') - DATE_FORMAT(`user_birthdate`, '%Y')) `Age`
FROM `school_user_attendance` 
INNER JOIN `school_user` 
ON `UserId` = `user_id` 
WHERE `Type` = 1 
AND `Leader` = 0 
AND `OccId` = 1384 
AND (DATE_FORMAT(@TheDate, '%Y') - DATE_FORMAT(`user_birthdate`, '%Y')) BETWEEN 7 AND 25;

当从返回NULL的单个场合列出partipants时:

+-------+----------+-----------+-----------+----------------+------+
| Id    | Occasion | Firstname | Lastname  | user_birthdate | Age  |
+-------+----------+-----------+-----------+----------------+------+
| 36830 |     1384 | Foo       | Bar       | 2009-11-02     |    7 |
| 36831 |     1384 | Foo       | Bar       | 2009-11-20     |    7 |
| 36820 |     1384 | Foo       | Bar       | 2009-04-09     |    7 |
| 36827 |     1384 | Foo       | Bar       | 2009-06-18     |    7 |
| 36819 |     1384 | Foo       | Bar       | 2009-12-21     |    7 |
| 36836 |     1384 | Foo       | Bar       | 2008-12-23     |    8 |
| 36835 |     1384 | Foo       | Bar       | 2009-02-07     |    7 |
+-------+----------+-----------+-----------+----------------+------+

如何根据DOB字段计算用户的年龄,并使用特定日期作为参考?在我的情况下,年龄将是“用户在开始日期/开始年份开始时的年龄”。

谢谢!

更新

将我的子查询更改为:

SELECT COUNT(`t1`.`Id`) `Ant`, `t2`.`OccId`, `user_birthdate` 
FROM `school_group_occasion` `t1` 
INNER JOIN `school_user_attendance` `t2` 
ON `t1`.ID = `t2`.`OccId` 
INNER JOIN `school_user` 
ON `t2`.`UserId` = `user_id` 
WHERE `t2`.`Type` = 1 
AND `t2`.`Leader` = 0 
AND (DATE_FORMAT(`t1`.`Start`, '%Y') - DATE_FORMAT(`user_birthdate`, '%Y')) BETWEEN 7 AND 25 
GROUP BY `t2`.`OccId` 

即。再次加入“第一”表只是为了让开始日期正确。这似乎有效,但有人可以帮我澄清为什么原始加入不能按预期工作?感谢

0 个答案:

没有答案