【发布时间】:2017-10-06 21:58:45
【问题描述】:
我有一个 mysql 表,我试图在其上运行单个/嵌套查询。
SELECT
`subscriber_id`,
(SELECT...?) AS total_campaigns,
(SELECT...?) AS total_opens,
(total_opens / total_campaigns) as percentage
FROM
`campaigns_table`
type 列的值 = 2 表示活动已打开。
我希望返回一个包含所有 subscriber_id 的结果集,其中包含总广告系列和总打开次数
查询结果数据
+---------------+-----------------+-------------+------------+
| subscriber_id | total_campaigns | total_opens | percentage |
+---------------+-----------------+-------------+------------+
| 1 | 2 | 1 | 0.5 |
| 2 | 2 | 2 | 1.0 |
| 3 | 2 | 0 | 0.0 |
| 4 | 1 | 0 | 0.0 |
| 5 | 1 | 1 | 1.0 |
+---------------+-----------------+-------------+------------+
subscriber_id 1 将是 total_campaigns = 2(campaign_id 的 37428,37239)和total_opens = 1(只有campaign_id 27428 具有类型 2 记录)
示例表数据
+---------------+-------------+------------+-------+------+---------+
| subscriber_id | campaign_id | timestamp | count | type | link_id |
+---------------+-------------+------------+-------+------+---------+
| 1 | 37428 | 1434513738 | 1 | 1 | 0 |
| 1 | 37428 | 1434513758 | 1 | 1 | 0 |
| 1 | 37428 | 1434513338 | 2 | 2 | 0 |
| 1 | 37429 | 1434511738 | 1 | 1 | 0 |
| 1 | 37429 | 1434311738 | 1 | 1 | 1 |
| 2 | 37428 | 1534513738 | 1 | 1 | 0 |
| 2 | 37428 | 1534513758 | 1 | 1 | 0 |
| 2 | 37428 | 1534513338 | 2 | 2 | 0 |
| 2 | 37429 | 1534511738 | 1 | 1 | 1 |
| 2 | 37429 | 1534311738 | 1 | 2 | 0 |
| 3 | 37428 | 1534513738 | 1 | 1 | 0 |
| 3 | 37429 | 1534511738 | 1 | 1 | 1 |
| 4 | 57428 | 1534513738 | 1 | 1 | 0 |
| 4 | 57428 | 1534513758 | 1 | 1 | 0 |
| 5 | 57428 | 1534513338 | 3 | 2 | 0 |
+---------------+-------------+------------+-------+------+---------+
使用下面@spencer7593 的答案。然后如何使用结果更新另一个表?
尝试做这样的事情(我的方法不行)
UPDATE `subscribers` a
LEFT JOIN `campaigns_table` b ON a.`ID` = b.`subscriber_id`
SET a.`STATUS` = 2
FROM(
SELECT t.subscriber_id
, COUNT(DISTINCT t.campaign_id) AS total_campaigns
, COUNT(DISTINCT IF(t.type=2,t.campaign_id,NULL)) AS open_campaigns
, COUNT(DISTINCT IF(t.type=2,t.campaign_id,NULL))
/ COUNT(DISTINCT t.campaign_id) AS percentage
FROM `campaigns_table` t
GROUP BY t.subscriber_id
HAVING COUNT(DISTINCT t.campaign_id) > 5 AND COUNT(DISTINCT IF(t.type=2,t.campaign_id,NULL)) = 0
ORDER BY t.subscriber_id
) i
【问题讨论】:
-
total_campaigns 和 total_opens 的查询是什么?您不能使用 total_campaigns 和 total_opens 作为您编写的百分比,因为这些列不在表campaigns_table中
-
正确。 total_campaigns 和 total_opens 不是有效的列。我只是用它作为我尝试做的一个例子。