MySQL select 值在范围内,而该值不在另一个范围内
MySQL select value in range where that value not in another range
我们有一个 table 记录每个客户请求,每个请求都有一个 created_at
和 customer_email
列。
mysql> select * from requests;
+---------------------+-----------------------+
| created_at | customer_email |
+---------------------+-----------------------+
| 2016-08-08 14:05:50 | user1@example.com |
| 2016-08-08 14:05:51 | user2@example.com |
| 2017-08-15 14:41:40 | user3@example.com |
| 2017-08-15 15:17:25 | user2@example.com |
| 2018-08-15 15:30:41 | user1@example.com |
+---------------------+-----------------------+
我正在尝试获取 08/2018 中 created_at
的 customer email
的记录,但在此之前没有创建请求。换句话说,找出哪些在 08/2018 中创建请求的客户是新客户。
这是我正在尝试的查询,它返回 0
SELECT DISTINCT
customer_email
FROM
requests
WHERE
created_at > '2018-08-01'
AND customer_email NOT IN (SELECT DISTINCT
customer_email
FROM
requests
WHERE
created_at < '2018-08-01')
如有任何帮助,我们将不胜感激!
尝试使用 GROUP BY
和 HAVING
子句。
使用 MIN function, we can find out the first_created_at
for a customer_email
, and then utilizing HAVING 子句,我们将结果集限制为在 08/18.
之后具有 first_created_at
的结果集
SELECT customer_email, DATE(MIN(created_at)) AS first_created_at
FROM requests
GROUP BY customer_email
HAVING first_created_at >= DATE('2018-08-01')
我们有一个 table 记录每个客户请求,每个请求都有一个 created_at
和 customer_email
列。
mysql> select * from requests;
+---------------------+-----------------------+
| created_at | customer_email |
+---------------------+-----------------------+
| 2016-08-08 14:05:50 | user1@example.com |
| 2016-08-08 14:05:51 | user2@example.com |
| 2017-08-15 14:41:40 | user3@example.com |
| 2017-08-15 15:17:25 | user2@example.com |
| 2018-08-15 15:30:41 | user1@example.com |
+---------------------+-----------------------+
我正在尝试获取 08/2018 中 created_at
的 customer email
的记录,但在此之前没有创建请求。换句话说,找出哪些在 08/2018 中创建请求的客户是新客户。
这是我正在尝试的查询,它返回 0
SELECT DISTINCT
customer_email
FROM
requests
WHERE
created_at > '2018-08-01'
AND customer_email NOT IN (SELECT DISTINCT
customer_email
FROM
requests
WHERE
created_at < '2018-08-01')
如有任何帮助,我们将不胜感激!
尝试使用 GROUP BY
和 HAVING
子句。
使用 MIN function, we can find out the first_created_at
for a customer_email
, and then utilizing HAVING 子句,我们将结果集限制为在 08/18.
first_created_at
的结果集
SELECT customer_email, DATE(MIN(created_at)) AS first_created_at
FROM requests
GROUP BY customer_email
HAVING first_created_at >= DATE('2018-08-01')