mysql-php,创建临时表时出错

时间:2013-03-13 19:47:20

标签: php mysql create-table temp-tables

我终于准备好了我的查询插入代码,但现在我在运行整个查询时遇到错误。我相信它与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, .........

请告诉我这是否有意义,或者我需要更新问题并提供更多信息。

1 个答案:

答案 0 :(得分:2)

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

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