2015-10-29 351 views
2

我有以下兩個查詢,我想合併成一個,其中日期是第一列。我遇到的問題是子查詢的條件不同,第二個查詢可能沒有每個月的值。我一直在不同的頁面上運行它們,爲什麼AS num_custs在兩者上都是相同的。PHP MySQL輸出兩個SQL Select語句

我已經嘗試了幾個不同的事情與SQL,但迄今失敗。我最初的想法是在SQL中使用UNION,但沒有奏效。我想我正在試圖用SQL而不是PHP來實現它。

第一個問題是我應該使用哪個SQL命令來實現這個目標?

希望這一切都有道理。

查詢1:

  SELECT * , COUNT(entity_id) AS num_custs 
       FROM (

       SELECT e. * , e.created_at AS abc123, o.status, o.total_invoiced, o.shipping_description, o.subtotal_incl_tax, MAX(o.created_at) AS last_order_date 
       FROM mg_customer_entity AS e 
       LEFT JOIN mg_sales_flat_order AS o ON o.customer_id = e.entity_id 
       WHERE e.entity_type_id = '1' 
       AND o.status NOT LIKE 'canceled' 
       AND o.status NOT LIKE 'closed' 
       AND o.status NOT LIKE 'fraud' 
       AND o.status NOT LIKE 'holded' 
       AND o.status NOT LIKE 'paypal_canceled_reversal' 
       AND e.store_id 
       BETWEEN 1 
       AND 2 
       AND o.total_invoiced IS NOT NULL 
       AND o.subtotal_incl_tax IS NOT NULL 

       GROUP BY e.entity_id 
       HAVING last_order_date IS NOT NULL 

       )sub_query 
       GROUP BY YEAR(abc123) , MONTH(abc123) 
       ORDER BY abc123 DESC 

查詢2:

  SELECT * , COUNT(entity_id) AS num_custs 
       FROM (

       SELECT e. * , e.created_at AS abc123, o.status, o.total_invoiced, o.shipping_description, o.subtotal_incl_tax, MAX(o.created_at) AS last_order_date 
       FROM mg_customer_entity AS e 
       LEFT JOIN mg_sales_flat_order AS o ON o.customer_id = e.entity_id 
       WHERE e.entity_type_id = '1' 
       AND o.status NOT LIKE 'canceled' 
       AND o.status NOT LIKE 'closed' 
       AND o.status NOT LIKE 'fraud' 
       AND o.status NOT LIKE 'holded' 
       AND o.status NOT LIKE 'paypal_canceled_reversal' 
       AND e.store_id 
       BETWEEN 1 
       AND 2 
       AND o.total_invoiced IS NOT NULL 
       AND o.subtotal_incl_tax IS NOT NULL 

       GROUP BY e.entity_id 
       HAVING last_order_date >= DATE_SUB(CURDATE() , INTERVAL 91 DAY) 
       AND last_order_date IS NOT NULL 

       )sub_query 
       GROUP BY YEAR(abc123) , MONTH(abc123) 
       ORDER BY abc123 DESC 

回答

0

你提到你想要的日期作爲第一個結果,你可以指定所有字段或只是添加它在前面。

如果使用PHP構建查詢,則可以輕鬆注入額外的條件。

該條件將插入sprintf function中的「%s」位置。

// set $limit_order_date depending on the page 

    $having = ''; 

    if (true === $limit_order_date) { 
     $having = 'AND last_order_date >= DATE_SUB(CURDATE() , INTERVAL 91 DAY)'; 
    } 


    $sql = sprintf(" 
     SELECT abc123, 
       * , 
       COUNT(entity_id) AS num_custs 
      FROM (
      SELECT e. *, 
        e.created_at AS abc123, 
        o.status, 
        o.total_invoiced, 
        o.shipping_description, 
        o.subtotal_incl_tax, 
        MAX(o.created_at) AS last_order_date 
       FROM mg_customer_entity AS e 
     LEFT JOIN mg_sales_flat_order AS o 
       ON o.customer_id = e.entity_id 
      WHERE e.entity_type_id = '1' 
       AND o.status NOT LIKE 'canceled' 
       AND o.status NOT LIKE 'closed' 
       AND o.status NOT LIKE 'fraud' 
       AND o.status NOT LIKE 'holded' 
       AND o.status NOT LIKE 'paypal_canceled_reversal' 
       AND e.store_id BETWEEN 1 AND 2 
       AND o.total_invoiced IS NOT NULL 
       AND o.subtotal_incl_tax IS NOT NULL 
      GROUP BY e.entity_id 
      HAVING last_order_date IS NOT NULL %s 
       )sub_query 
    GROUP BY YEAR(abc123), 
       MONTH(abc123) 
    ORDER BY abc123 DESC", 
     $having 
    ); 

我希望我能正確解釋你的問題。

+0

感謝您的回答,如果您有多於一個$的話會發生什麼?那可能嗎?另外你將如何循環通過每一行? –

+0

你可以有一個select語句或多個if語句將'having'條件收集到一個數組$ having [] =「AND SOME CONDITION」中;''你可以與'$ having_string = implode('',$ having )' – moorscode