【发布时间】:2017-01-05 10:06:00
【问题描述】:
我想使用下表提取过去两个月的数据: 我已经走到这一步了:
select * from requests r
INNER JOIN request_status rs
ON r.id = rs.request_id
where r.customer_id = <some-customer-id>
AND r.created_time_stamp
BETWEEN '2016-011-01 00:00:00.000000'
AND '2017-01-05 00:00:00.000000' limit 10
问题是我不确定如何在上述查询的结果集上放置另一个内部连接。我必须实现的是对上述查询的结果集执行内部连接,并从该结果集中提取一些数据。有什么帮助吗?
- 请求:
+-----+--------------------+
| id | created_time_stamp |
+-----+--------------------+
| 123 | 12:23:00 |
+-----+--------------------+
- 请求状态:
+----+------------+-------------+
| id | request_id | customer_id |
+----+------------+-------------+
| 12 | 123 | 3453 |
+----+------------+-------------+
- request_process :
+----+-------------+------------+
| id | customer_id | process_id |
+----+-------------+------------+
| 12 | 3453 | 23 |
+----+-------------+------------+
编辑 1:
请考虑以下带有冒号后列出的字段的表格:
- push_requests:id、customer_id、created_time_stamp
- push_status : id, request_id
- 订阅者:id、customer_id、is_inactive
注意:push_status 中的 request_id 是 push_request id 的外键
我想在查询中实现什么:
- 从 push_requests 中提取记录,其中 customer_id A 在某某 created_time_stamp 之间。
- 将其与基于外键的 push_status 表连接。
- 将步骤 1 和 2 的结果集与订阅表连接起来。
我期待什么输出:最终结果集包含一整行 push_requests、push_status 和一个显示 is_inactive 值为 0 的订阅者数量的附加列。
【问题讨论】:
-
您需要对当前查询进行哪些修改?
-
我想对上述查询的结果集进行内连接。
-
如果你使用 sql server 使用 CTE ,docs here
-
好的...但是您想加入哪个表?
-
基于customer_id字段的request_process上面列出的第三张表。