0

我正在制作一个临时表,它是来自其他两个表的数据的摘要,并将其输出到电子表格。

这些表是“cruises”和“itinerary”,并且 Cruises 表列出了一些 Cruises ,并且通过 id 与该表相关的是 itinerary 表,其中包含该游轮上每个停靠港的一行。

邮轮表:

  • id, title, 出发, 代码, 持续时间

行程表:

  • id、cruise_id、日期、港口、订单

我想把这些数据放在一起,像这样

  • Cruise_id:游轮的id
  • 描述字段包含游轮访问的所有港口,用逗号分隔,甚至可能在游轮当天订购

这是到目前为止的代码,我还没有从行程表中选择任何东西

<?php require_once('includes/session.php'); ?>
<?php require_once('includes/connection.php'); ?>
<?php require_once('includes/functions.php'); ?>
<?php confirm_logged_in(); ?>

<?php 

$maketemp = "CREATE TEMPORARY TABLE temp(`itineraryId` int NOT NULL, `live` varchar(1), `shipCode` varchar(15), `description` text, `length` varchar(10), PRIMARY KEY(itineraryId))"; 

mysql_query( $maketemp, $connection ) or die ( "Sql error : " . mysql_error ( ) );

$inserttemp = "INSERT INTO temp SELECT id AS itineraryId, live, ship AS shipCode, description AS description, duration AS length FROM cruises WHERE live ='Y'";

mysql_query( $inserttemp, $connection ) or die ( "Sql error : " . mysql_error ( ) );

$select = "SELECT itineraryId, shipCode, description, length FROM temp";

$export = mysql_query ( $select, $connection ) or die ( "Sql error : " . mysql_error( ) );

$fields = mysql_num_fields ( $export );

for ( $i = 0; $i < $fields; $i++ )
{
    $header .= mysql_field_name( $export , $i ) . "\t";
}

while( $row = mysql_fetch_row( $export ) )
{
    $line = '';
    foreach( $row as $value )
    {                                            
        if ( ( !isset( $value ) ) || ( $value == "" ) )
        {
            $value = "\t";
        }
        else
        {
            $value = strip_tags( $value );
        $value = str_replace( array('&nbsp;'),'',$value );
        $value = str_replace( array('Ocean Countess'), 'OC', $value );
        $value = str_replace( array('Marco Polo'), 'MP', $value );
        $value = str_replace( array('Nights', 'nights', 'Night', 'night'), '', $value);
        $value = str_replace( '"' , '""' , $value );
        $value = '"' . $value . '"' . "\t";
    }
    $line .= $value;
}
$data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );

if ( $data == "" )
{
$data = "\n(0) Records Found!\n";                        
}

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=itinerary.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?>
4

1 回答 1

2

您正在 MySQL 中寻找GROUP_CONCAT函数。

这将允许您按 Cruise_id 分组,并按天排序。

于 2012-04-27T12:10:41.920 回答