Mysql临时表中断连接

时间:2014-05-15 17:00:45

标签: mysql sql

是否有任何可能导致mysql失败的事情"临时表中间连接?

我得到的错误是:

phppos_sales_items_temp does not exist"

我正在创建一个临时表" phppos_sales_items_temp"如下图所示。

我的代码类似于

数据库代码:

//We create a temp table that allows us to do easy report/sales queries
public function create_sales_items_temp_table($params)
{
    if (!$this->session->userdata('sales_report_running'))
    {
        $this->session->set_userdata('sales_report_running', TRUE);

        $location_id = $this->Employee->get_logged_in_employee_current_location_id();

        $where = '';

        if (isset($params['start_date']) && isset($params['end_date']))
        {
            $where = 'WHERE sale_time BETWEEN "'.$params['start_date'].'" and "'.$params['end_date'].'"'.' and '.$this->db->dbprefix('sales').'.location_id='.$this->db->escape($location_id). (($this->config->item('hide_store_account_payments_in_reports') ) ? ' and '.$this->db->dbprefix('sales').'.store_account_payment=0' : '');

            if ($this->config->item('hide_suspended_sales_in_reports'))
            {
                $where .=' and suspended = 0';
            }
        }
        elseif ($this->config->item('hide_suspended_sales_in_reports'))
        {
            $where .='WHERE suspended = 0'.' and '.$this->db->dbprefix('sales').'.location_id='.$this->db->escape($location_id).(($this->config->item('hide_store_account_payments_in_reports') ) ? ' and '.$this->db->dbprefix('sales').'.store_account_payment=0' : '');
        }

        if ($where == '')
        {
            $where = 'WHERE '.$this->db->dbprefix('sales').'.location_id='.$this->db->escape($location_id).(($this->config->item('hide_store_account_payments_in_reports') ) ? ' and '.$this->db->dbprefix('sales').'.store_account_payment=0' : '');
        }


        $this->_create_sales_items_temp_table_query($where);
        $this->session->set_userdata('sales_report_running', FALSE);

        return TRUE;
    }

    return FALSE;

}

function _create_sales_items_temp_table_query($where)
{
    set_time_limit(0);

    $this->db->query("CREATE TEMPORARY TABLE ".$this->db->dbprefix('sales_items_temp')."
    (SELECT ".$this->db->dbprefix('sales').".deleted as deleted,".$this->db->dbprefix('sales').".deleted_by as deleted_by, sale_time, date(sale_time) as sale_date, ".$this->db->dbprefix('sales_items').".sale_id, comment,payment_type, customer_id, employee_id, 
    ".$this->db->dbprefix('items').".item_id, NULL as item_kit_id, supplier_id, quantity_purchased, item_cost_price, item_unit_price, category, 
    discount_percent, (item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100) as subtotal,
    ".$this->db->dbprefix('sales_items').".line as line, serialnumber, ".$this->db->dbprefix('sales_items').".description as description,
    (item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)+(item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100) 
    +(((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100) + (item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100))
    *(SUM(CASE WHEN cumulative = 1 THEN percent ELSE 0 END))/100) as total,
    (item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100) 
    +(((item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100) + (item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100))
    *(SUM(CASE WHEN cumulative = 1 THEN percent ELSE 0 END))/100) as tax,
    (item_unit_price*quantity_purchased-item_unit_price*quantity_purchased*discount_percent/100) - (item_cost_price*quantity_purchased) as profit
    FROM ".$this->db->dbprefix('sales_items')."
    INNER JOIN ".$this->db->dbprefix('sales')." ON  ".$this->db->dbprefix('sales_items').'.sale_id='.$this->db->dbprefix('sales').'.sale_id'."
    INNER JOIN ".$this->db->dbprefix('items')." ON  ".$this->db->dbprefix('sales_items').'.item_id='.$this->db->dbprefix('items').'.item_id'."
    LEFT OUTER JOIN ".$this->db->dbprefix('suppliers')." ON  ".$this->db->dbprefix('items').'.supplier_id='.$this->db->dbprefix('suppliers').'.person_id'."
    LEFT OUTER JOIN ".$this->db->dbprefix('sales_items_taxes')." ON  "
    .$this->db->dbprefix('sales_items').'.sale_id='.$this->db->dbprefix('sales_items_taxes').'.sale_id'." and "
    .$this->db->dbprefix('sales_items').'.item_id='.$this->db->dbprefix('sales_items_taxes').'.item_id'." and "
    .$this->db->dbprefix('sales_items').'.line='.$this->db->dbprefix('sales_items_taxes').'.line'. "
    $where
    GROUP BY sale_id, item_id, line) 
    UNION ALL
    (SELECT ".$this->db->dbprefix('sales').".deleted as deleted,".$this->db->dbprefix('sales').".deleted_by as deleted_by, sale_time, date(sale_time) as sale_date, ".$this->db->dbprefix('sales_item_kits').".sale_id, comment,payment_type, customer_id, employee_id, 
    NULL as item_id, ".$this->db->dbprefix('item_kits').".item_kit_id, '' as supplier_id, quantity_purchased, item_kit_cost_price, item_kit_unit_price, category, 
    discount_percent, (item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100) as subtotal,
    ".$this->db->dbprefix('sales_item_kits').".line as line, '' as serialnumber, ".$this->db->dbprefix('sales_item_kits').".description as description,
    (item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100)+(item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100) 
    +(((item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100) + (item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100))
    *(SUM(CASE WHEN cumulative = 1 THEN percent ELSE 0 END))/100) as total,
    (item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100) 
    +(((item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100)*(SUM(CASE WHEN cumulative != 1 THEN percent ELSE 0 END)/100) + (item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100))
    *(SUM(CASE WHEN cumulative = 1 THEN percent ELSE 0 END))/100) as tax,
    (item_kit_unit_price*quantity_purchased-item_kit_unit_price*quantity_purchased*discount_percent/100) - (item_kit_cost_price*quantity_purchased) as profit
    FROM ".$this->db->dbprefix('sales_item_kits')."
    INNER JOIN ".$this->db->dbprefix('sales')." ON  ".$this->db->dbprefix('sales_item_kits').'.sale_id='.$this->db->dbprefix('sales').'.sale_id'."
    INNER JOIN ".$this->db->dbprefix('item_kits')." ON  ".$this->db->dbprefix('sales_item_kits').'.item_kit_id='.$this->db->dbprefix('item_kits').'.item_kit_id'."
    LEFT OUTER JOIN ".$this->db->dbprefix('sales_item_kits_taxes')." ON  "
    .$this->db->dbprefix('sales_item_kits').'.sale_id='.$this->db->dbprefix('sales_item_kits_taxes').'.sale_id'." and "
    .$this->db->dbprefix('sales_item_kits').'.item_kit_id='.$this->db->dbprefix('sales_item_kits_taxes').'.item_kit_id'." and "
    .$this->db->dbprefix('sales_item_kits').'.line='.$this->db->dbprefix('sales_item_kits_taxes').'.line'. "
    $where
    GROUP BY sale_id, item_kit_id, line) ORDER BY sale_id, line");
}

致电代码

function get_recent_sales($customer_id)
{
    $return = array();

    if (!$this->create_sales_items_temp_table(array('start_date' =>date('Y-m-d', strtotime('-60 days')), 'end_date' => date('Y-m-d').' 23:59:59')))
    {
        return array();
    }

    $this->db->select('sale_id, sale_date, sum(quantity_purchased) as items_purchased, sum(total) as total', false);
    $this->db->from('sales_items_temp');
    $this->db->where('customer_id', $customer_id);
    $this->db->where('deleted', 0);
    $this->db->group_by('sale_id');
    $this->db->order_by('sale_date');
    $this->db->limit(10);


    //This sometimes has an error "phppos_sales_items_temp does not exist"
    foreach($this->db->get()->result_array() as $row)
    {
        $return[] = $row;
    }

    return $return;
}

0 个答案:

没有答案