mysql - 多个嵌套的 SELECT 查询和两列求和

标签 mysql sql database

我有一个 mysql 表,我正在尝试对其运行单个/嵌套 查询。

SELECT 
`subscriber_id`, 
(SELECT...?) AS total_campaigns, 
(SELECT...?) AS total_opens,
(total_opens / total_campaigns) as percentage
FROM
`campaigns_table`

值为 2 的 type 列表明事件已打开。

我希望返回一个结果集,其中包含所有 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 |时间戳 |计数 |类型 |链接 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

最佳答案

在我看来,这是一个可以通过条件聚合解决的问题,只需一次遍历表,无需任何嵌套查询。

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  
 ORDER BY t.subscriber_id

可以使用嵌套查询生成等效结果,但一般来说,单次遍历表(使用单个 SELECT)将具有更高效的访问计划。

如果需要使用嵌套查询(我不明白为什么会有),一般来说,我更愿意使用内联 View 而不是 SELECT 列表中的相关查询。

SELECT q.subscriber_id
     , SUM(1)                       AS total_campaigns
     , SUM(q.open_campaign)         AS open_campaigns
     , SUM(q.open_campaign)/SUM(1)  AS percentage
  FROM ( SELECT t.subscriber_id
              , t.campaign
              , MAX(t.type=2) AS `open_campaign`
           FROM `campaigns_table` t
          WHERE t.campaign IS NOT NULL
          GROUP
             BY t.subscriber_id
              , t.campaign
       ) q
 ORDER BY q.subscriber

如果我们想在 SELECT 列表中使用嵌套查询,那些将是相关的子查询...

SELECT s.subscriber
     , (      SELECT COUNT(DISTINCT t.campaign) 
                FROM `campaigns_table` t
               WHERE t.subscriber = s.subscriber
       ) AS total_campaigns

     , (      SELECT COUNT(DISTINCT o.campaign) 
                FROM `campaigns_table` o
               WHERE o.subscriber = s.subscriber
                 AND o.type=2
       ) AS open_campaigns

     , (      SELECT COUNT(DISTINCT po.campaign)
                FROM `campaigns_table` po
               WHERE po.subscriber = s.subscriber
                 AND po.type=2
       )
     / (      SELECT COUNT(DISTINCT pt.campaign) 
                FROM `campaigns_table` pt
                WHERE pt.subscriber = s.subscriber
       ) AS percentage

  FROM ( SELECT r.subscriber
           FROM `campaigns_table` r
         GROUP BY r.subscriber
       ) s
 ORDER BY s.subscriber

关于mysql - 多个嵌套的 SELECT 查询和两列求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46614484/

相关文章:

带有修剪的mysql concat

MySQL native 错误处理与唯一值的预期错误处理

SQL触发器更新另一个表

java - 在持久化期间忽略 JPA 字段的最简单方法是什么?

java - 更新: Problem with executing SQL query from Java

database - 跨所有客户端实时同步数据库数据

php - json_decode 函数 php ://input return Invalid JSON Format

PHP - 每周显示 1 条随机记录

sql - Active Record 查询以匹配每个子集元素

php - 查询字符串中未捕获异常