简体   繁体   English

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

[英]mysql-php, error on create temporary table

I've finally gotten my queries ready to insert into code but now I'm getting an error when running the whole query. 我终于准备好将查询插入到代码中,但是现在在运行整个查询时遇到错误。 I believe it has to do with the drop table function. 我相信这与放置表功能有关。 I originally had them inline and then read that I should remove it and add at the beginning of the query like so: 我最初将它们内联,然后阅读我应将其删除并在查询开始时添加,如下所示:

    $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 (..........

I am creating two temp tables and then joining them in the last query. 我正在创建两个临时表,然后将它们加入上一个查询中。 I am not sure how to put that second DROP temp table back into the full query or if that's even the right way to go. 我不确定如何将第二个DROP临时表放回完整查询中,或者这是否是正确的方法。 The error that I'm getting is: 我得到的错误是:

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

Query: 查询:

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, .........

Please let me know if this makes sense or I need to update question with more information. 请让我知道这是否有意义,或者我需要使用更多信息来更新问题。

If you are trying to execute both queries in one call to $this->db->query() the problem is probably that your database library does not permit multiple queries. 如果试图在一次对$this->db->query()的调用中执行两个查询,则问题可能出在您的数据库库不允许多次查询。

To see if that is the problem, you should split them up in two separate queries. 要查看是否存在问题,应将它们分为两个单独的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM