在使用WHERE时在SQL Join中包含NULL

时间:2017-03-15 15:01:17

标签: mysql sql database join

我有以下两个表:

  • TempUser22 :57,000行:
+------+-----------+
| Id   | Followers |
+------+-----------+
|  874 |     55542 |
| 1081 |    330624 |
| 1378 |     17919 |
| 1621 |       920 |
| 1688 |    255463 |
| 2953 |       751 |
| 3382 |    204466 |
| 3840 |    273489 |
| 4145 |       376 |
| ...  |       ... |
+------+-----------+
  • temporal_users :总计10,000,000行,3200行其中Date = 2010-12-31:
+---------------------+---------+--------------------+
| Date                | User_Id | has_original_tweet |
+---------------------+---------+--------------------+
| 2008-02-22 12:00:00 |  676493 |                  2 |
| 2008-02-22 12:00:00 |  815263 |                  1 |
| 2008-02-22 12:00:00 | 6245822 |                  1 |
| 2008-02-22 12:00:00 | 8854092 |                  1 |
| 2008-02-23 12:00:00 |  676493 |                  2 |
| 2008-02-23 12:00:00 |  815263 |                  1 |
| 2008-02-23 12:00:00 | 6245822 |                  1 |
| 2008-02-23 12:00:00 | 8854092 |                  1 |
| 2008-02-24 12:00:00 |  676493 |                  2 |
|    .............    |   ...   |                 .. |
+---------------------+---------+--------------------+

我在这些表上运行以下连接查询:

SELECT sum(has_original_tweet), b.Id
  FROM temporal_users AS a
  RIGHT JOIN TempUser22 AS b
  ON a.User_ID = b.Id
  GROUP BY b.Id;

按预期返回57,00行,第一个字段为NULL回答:

+-------------------------+------+
| sum(has_original_tweet) | Id   |
+-------------------------+------+
|                    NULL |  874 |
|                    NULL | 1081 |
|                     135 | 1378 |
|                     164 | 1621 |
|                     652 | 1688 |
|                     691 | 2953 |
|                    NULL | 3382 |
|                    NULL | 3840 |
|                    NULL | 4145 |
|                     ... | .... |
+-------------------------+------+

但是,在添加指定日期的WHERE行时,如下所示:

SELECT sum(has_original_tweet), b.Id
  FROM temporal_users AS a
  RIGHT JOIN TempUser22 AS b
  ON a.User_ID = b.Id
  WHERE a.Date BETWEEN '2010-12-31-00:00:00' AND '2010-12-31-23:59:59'
  GROUP BY b.Id;

我收到以下答案,只有3200行,第一个字段中没有任何NULL。

+-------------------------+---------+
| sum(has_original_tweet) | Id      |
+-------------------------+---------+
|                       1 |  797194 |
|                       1 |  815263 |
|                       0 |  820678 |
|                       1 | 1427511 |
|                       0 | 4653731 |
|                       1 | 5933862 |
|                       2 | 7530552 |
|                       1 | 7674072 |
|                       1 | 8149632 |
|                      .. |   ....  |
+-------------------------+---------+

我的问题是:对于给定日期,如果给定日期的temporal_user中没有has_original_tweet,那么 TempUser22 中每个用户的大小为57,000行的答案是否为NULL值? / p>

感谢。

2 个答案:

答案 0 :(得分:2)

SELECT b.Id, SUM(a.has_original_tweet) s
FROM TempUser22 b
LEFT JOIN temporal_users a ON b.Id = a.User_Id
  AND a.Date BETWEEN '2010-12-31-00:00:00' AND '2010-12-31-23:59:59'
GROUP BY b.Id;

Id  s
1   null
2   1
3   null
4   3
5   null
6   null

为了调试,我使用了:

CREATE TEMPORARY TABLE TempUser22(Id INT, Followers INT)
  SELECT 1 Id, 10 Followers UNION ALL
  SELECT 2, 20 UNION ALL
  SELECT 3, 30 UNION ALL
  SELECT 4, 40 UNION ALL
  SELECT 5, 50 UNION ALL
  SELECT 6, 60
;
CREATE TEMPORARY TABLE temporal_users(`Date` DATETIME, User_Id INT, has_original_tweet INT)
  SELECT '2008-02-22 12:00:00' `Date`, 1 User_Id, 1 has_original_tweet UNION ALL

  SELECT '2008-12-31 12:00:00', 2, 1 UNION ALL
  SELECT '2010-12-31 12:00:00', 2, 1 UNION ALL
  SELECT '2012-12-31 12:00:00', 2, 1 UNION ALL

  SELECT '2008-12-31 12:00:00', 4, 9 UNION ALL
  SELECT '2010-12-31 12:00:00', 4, 1 UNION ALL
  SELECT '2010-12-31 12:00:00', 4, 2 UNION ALL
  SELECT '2012-12-31 12:00:00', 4, 9
;

答案 1 :(得分:1)

这是因为NULL子句中的where值总是被丢弃

您可以在where子句中使用合并。

WHERE coalesce(a.Date, 'some-date-in-the-range') BETWEEN '2010-12-31-00:00:00' AND '2010-12-31-23:59:59'

相反,您可以强制null值被视为有效。