我创建了临时表 - temporary_ticket
我想将主表中的数据插入临时表ticket
在导出数据之前,我需要将数据从主表插入临时表,然后排除不需要的列
这是我的sql代码。
$sql = "SELECT id,subject,created_by,type,status,cat_id,DATE_FORMAT(create_time, '%m/%d/%Y 00:00:00')as create_time ,DATE_FORMAT(update_time, '%m/%d/%Y 00:00:00')as update_time,priority,updated_by,assigned_to,ticket_type,agent_created,cust4 FROM `$DB_TBLName` ORDER BY id ASC";
我试过
Insert* INTO temporary_ticket FROM ticket /* Drop the cloumns that are not needed */
ALTER TABLE temporary_ticket DROP COLUMN guest_name,guest_email /* Get results and drop temp table */
SELECT * FROM temporary_ticket DROP TABLE temporary_ticket
但它未来
此查询有什么问题?
这是我的完整代码
<?php
$DB_Server = "localhost"; //MySQL Server
$DB_Username = "*****"; //MySQL Username
$DB_Password = "******"; //MySQL Password
$DB_DBName = "i******"; //MySQL Database Name
$DB_TBLName = "wp3_wpsp_ticket"; //MySQL Table Name
$filename = "Support Ticket_Export"; //File Name
$sql="insert into wp3_wpsp_temporary_ticket
SELECT * FROM wp3_wpsp_ticket
commit;"
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno());
//select database
$Db = @mysql_select_db($DB_DBName, $Connect) or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno());
//execute query
$result = @mysql_query($sql,$Connect) or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());
$file_ending = "xls";
//header info for browser
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$filename.xls");
header("Pragma: no-cache");
header("Expires: 0");
$sep = "\t"; //tabbed character
//start of printing column names as names of MySQL fields
for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo mysql_field_name($result,$i) . "\t";
}
print("\n");
//end of printing column names
//start while loop to get data
while($row = mysql_fetch_row($result))
{
$schema_insert = "";
for($j=0; $j<mysql_num_fields($result);$j++)
{
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
elseif ($row[$j] != "")
$schema_insert .= "$row[$j]".$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
$schema_insert .= "\t";
print(trim($schema_insert));
print "\n";
}
?>
答案 0 :(得分:1)
在MySQL中select ... into ...不用于根据另一个表填充表,该语句用于为变量赋值或将数据导出到外部文件中。
您需要使用insert ... select ...语句:
insert into temporary_ticket
select * from ticket
但是,您在问题中描述的一系列陈述可以简单地用
代替select <list of fieldnames excluding guest_name,guest_email) from ticket
语句。无需临时表创建和所有其他复杂的东西。
答案 1 :(得分:1)
您应该使用插入选择
insert into temporary_ticket
SELECT * FROM ticket ;
并确保两个表之间的列匹配类型和编号(也是相同的序列)