1

我终于准备好将查询插入代码中,但现在运行整个查询时出现错误。我相信这与 drop table 功能有关。我最初将它们内联,然后读到我应该删除它并在查询的开头添加,如下所示:

    $query = $this->db->query("DROP TABLE IF EXISTS resultx;");
    $query = $this->db->query("DROP TABLE IF EXISTS resulty;");

$query = $this->db->query("
 CREATE TEMPORARY TABLE resultx AS
 select *, CONCAT(Credit,'_',OrderStat) as consol from (..........

我正在创建两个临时表,然后在最后一个查询中加入它们。我不确定如何将第二个 DROP 临时表放回完整查询中,或者这是否是正确的方法。我得到的错误是:

A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for the right syntax to use 
near 'CREATE TEMPORARY TABLE resulty AS select packetDeet,Sales,SaleDate, UserID,Lead' at line 15

询问:

CREATE TEMPORARY TABLE resultx AS 
    select 
        *,
        CONCAT(Credit,'_',OrderStat) as consol 
    FROM 
        ( select 
              packetDetailsId, GROUP_CONCAT(Credit) AS Credit,
              GROUP_CONCAT(AccountNum) AS AccountNum,
              GROUP_CONCAT(OrderStat) AS OrderStat 
          FROM 
             ( SELECT 
                   pd_extrafields.packetDetailsId, 
                   CASE WHEN 
                       pd_extrafields.ex_title LIKE ('%Credit%') 
                       THEN pd_extrafields.ex_value 
                       ELSE NULL 
                   END as Credit, 
                  CASE WHEN 
                       pd_extrafields.ex_title LIKE ('%Account%') 
                       THEN pd_extrafields.ex_value 
                       ELSE NULL 
                  END as AccountNum, 
                 CASE WHEN 
                       pd_extrafields.ex_title LIKE ('%Existing%') 
                       THEN pd_extrafields.ex_value 
                       ELSE NULL 
                 END as OrderStat 
               FROM pd_extrafields 
             ) AS myalias 
          GROUP BY packetDetailsId 
        )as TempTab; 
CREATE TEMPORARY TABLE resulty AS select packetDeet,Sales,SaleDate, .........

请让我知道这是否有意义,或者我需要用更多信息更新问题。

4

1 回答 1

2

如果您尝试在一次调用中执行两个查询,$this->db->query()则问题可能是您的数据库库不允许多个查询。

要查看这是否是问题所在,您应该将它们分成两个单独的查询。

于 2013-03-13T21:14:01.590 回答