PHP MySQL输出两个SQL Select语句

时间:2015-10-29 11:25:34

标签: php mysql

我有以下两个查询,我想组合成一个日期为第一列的查询。我遇到的问题是子查询的条件不同,第二个查询可能没有每个月的值。我一直在不同的页面上运行它们,因此两者的AS num_custs是相同的。

我用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

1 个答案:

答案 0 :(得分:0)

您提到您希望将日期作为第一个结果,您可以指定所有字段或只是将其添加到前面。

如果您使用PHP构建查询,则可以轻松注入额外条件。

条件将插入"%s"位于sprintf function

    // 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
    );

我希望我正确地解释你的问题。