简体   繁体   English

创建MySQL临时表并在PHP中从中选择

[英]Create MySQL temporary table and select from it within PHP

I need an advice with a PHP/MySQL code I am writing. 我需要有关正在编写的PHP / MySQL代码的建议。 What I need to do is to create a temp table and then select from it. 我需要做的是创建一个临时表,然后从中选择。 Problem is that the temp table only lasts during the query that created it so the second query in PHP is empty. 问题是临时表仅在创建它的查询期间持续存在,因此PHP中的第二个查询为空。

Here are both queries, I assume the solution would be to merge them into one singe query but so far nothing I tired works. 这是两个查询,我认为解决方案是将它们合并为一个单一查询,但到目前为止,我还不厌倦。

The queries below work in phpMyAdmin if executed one after another. 下面的查询在phpMyAdmin中工作,如果一个接一个地执行。

The temporary table query: 临时表查询:

$sql_temp_table = "CREATE TEMPORARY TABLE IF NOT EXISTS 
                   tbl_temp (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY) AS 
                  (SELECT 
                        SUM(number_active_ads) as active_ads, 
                        MONTH(create_date) as month, 
                        YEAR(create_date) as year, 
                        dealer_id as dealer_id 
                  FROM tbl_active_ads
                  WHERE dealer_id = '".$rs->fields['id']."' 
                  GROUP BY year, month 
                  ORDER BY id)";
$rs_temp_table  =   $db -> Execute($sql_temp_table);

The second SELECT query: 第二个SELECT查询:

$qry_chk_active_ads = "SELECT 
                            a.*, 
                            t.user_dealer_id, 
                            t.dealer_payment_for_month, 
                            t.dealer_payment_for_year
                       FROM tbl_temp as a
                       LEFT OUTER JOIN tbl_transaction as t
                       ON (a.dealer_id = t.user_dealer_id 
                           AND a.month = t.dealer_payment_for_month 
                           AND a.year = t.dealer_payment_for_year) 
                       ORDER BY id DESC 
                       LIMIT 3";
$rs_chk_active_ads  =   $db -> Execute($qry_chk_active_ads);

UPDATE: When I do this: var_dump($rs_temp_table); 更新:当我这样做时: var_dump($rs_temp_table); I get the following output from PHP: 我从PHP获得以下输出:

object(ADORecordSet_empty)#6 (6) { ["dataProvider"]=> string(5) "empty" ["databaseType"]=> bool(false) ["EOF"]=> bool(true) ["_numOfRows"]=> int(0) ["fields"]=> bool(false) ["connection"]=> bool(false) } object(ADORecordSet_empty)#6(6){[“ dataProvider”] => string(5)“ empty” [“ databaseType”] => bool(false)[“ EOF”] => bool(true)[“ _numOfRows” ] => int(0)[“ fields”] => bool(false)[“ connection”] => bool(false)}

Maybe this is explaining why the temp_table seems to be empty in PHP? 也许这解释了为什么temp_table在PHP中似乎为空?

Why not simply use... 为什么不简单地使用...

SELECT a.*
     , t.user_dealer_id
     , t.dealer_payment_for_month
     , t.dealer_payment_for_year
  FROM 
     ( SELECT SUM(number_active_ads) active_ads
            , DATE_FORMAT(create_date,'%Y-%m') yearmonth
            , dealer_id 
         FROM tbl_active_ads
        WHERE dealer_id = '".$rs->fields['id']."' 
        GROUP 
           BY DATE_FORMAT(create_date,'%Y-%m') 
     ) a
  LEFT 
  JOIN tbl_transaction t
    ON a.dealer_id = t.user_dealer_id 
   AND a.yearmonth = MONTH(t.dealer_payment_for_month)
   AND a.yearmonth = YEAR(t.dealer_payment_for_year) 
 ORDER 
    BY id DESC 
 LIMIT 3;

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

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