2017-10-06 33 views
1

我有一個MySQL表,我試圖運行一個/ 嵌套查詢。多重嵌套SELECT查詢和總計兩列

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 
+0

哪些TOTAL_CAMPAIGNS和total_opens查詢?您不能使用total_campaigns和total_opens作爲百分比,因爲這些列不在表格campaign_table – LAS

+0

正確。 total_campaigns和total_opens不是有效的列。我只是用它作爲我想要做的一個例子。 – user2712040

回答

0

它通過表看起來對我來說,可以用條件彙總解決的問題,以單道次,而不需要任何嵌套查詢。

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)將有更高效的訪問計劃。如果有要求使用嵌套查詢(我不明白爲什麼會有),一般來說,我寧願使用內聯視圖來覆蓋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 
+0

如何使用結果更新另一個表格?我需要找到每個'subscriber_id',找到一個'open_campaigns' = 0,並在另一個表('subscribers')中設置'status'。 – user2712040