2011-12-07 32 views
0

我有这个PHP至今导出为CSV文件:MySQL查询包括所有的“表2”中的“表1”

$link = mysql_connect($host, $user, $pass) or die("No se pudo conectar." . mysql_error()); 
mysql_select_db($db) or die ("No se pudo conectar."); 

$csv_output = "Creado el, Id de Ticket, Departamento, Tema de Ayuda, Prioridad, Nombre de Staff, Apellido de Staff, Nombre del cliente, Email del cliente, Asunto, Telefono, Extension, Estado de ticket, Direccion, Marca, Tienda, Lugar de Tienda, Codigo del PC, Fecha de compra, Factura, Documento, ID, Celular, Serie del Producto, Estado del Producto, Descripcion(PyP Defectuosa), Serie(PyP Defectuosa), Proveedor(PyP Defectuosa), Factura(PyP Defectuosa), Guia(PyP Defectuosa), Fecha(PyP Defectuosa), Garantía(PyP Defectuosa), Cantidad(PyP Defectuosa), Estado(PyP Defectuosa), Comentario(PyP Defectuosa), Usuario(PyP Defectuosa), Courier(PyP Defectuosa), N°Remito(PyP Defectuosa), Fecha de envio(PyP Defectuosa)"; 

$csv_output .= "\n"; 

$query = "(SELECT t1.created, t1.ticket_id, t3.dept_name, t1.helptopic, t1.priority_id, t2.firstname, t2.lastname, t1.name, t1.email, t1.subject, t1.phone, t1.phone_ext, t1.status, t1.address, t1.pcbrand, t1.storeLocation, t1.place, t1.pcCode, t1.purchaseDate, t1.invoice, t1.tipoid, t1.numId, t1.cell_phone, t1.nserieprod, t1.estprod, t4.ticket_id, t4.`desc` , t4.serial, t4.supplier, t4.invoice, t4.guide, t4.date, t4.warranty, t4.volume, t4.state, t4.comment, t4.user, t4.nomcourier, t4.nremito, t4.fenvio 
FROM ticket AS t1, staff AS t2, department AS t3, prod_cambio AS t4 
WHERE t2.dept_id = t3.dept_id 
AND t1.staff_id = t2.staff_id 
AND t1.ticket_id = t4.ticket_id 

order by t1.ticket_id)"; 

$result = mysql_query($query); 

if (mysql_num_rows($result) > 0) { 
while ($row = mysql_fetch_row($result)) { 
for ($j = 0; $j < 39; $j++) { 
if ($j <= 25){ 
$csv_output .= $row[$j]. " ,"; 
} 
$csv_output .= $row[$j]. " ,"; 
} 
$csv_output .= "\n"; 
} 
} 
$filename = $file."_".date("Y-m-d_H-i",time()); 
header("Content-type: application/vnd.ms-excel"); 
header("Content-disposition: csv" . date("Y-m-d") . ".csv"); 
header("Content-disposition: filename=".$filename.".csv"); 
print $csv_output; 
exit; 

I'm选择2个diferent表,并试图将它们转换于一体。 我的问题是这样的:

Created:   Ticket_id (Table 1):  Dept_name:  Priority_id:   Firstname:    Ticket_id (Table 2): 
2011-11-23 10:04:33  2     Soporte Lima    2     Juan Carlos     2 
2011-11-23 10:28:55  3     Soporte Lima    2     Antonio Arturo    3 
2011-11-23 10:42:07  4     Soporte Lima    2     Renzo      4 
2011-11-23 10:44:33  5     Soporte Lima    2     Renzo      5 
2011-11-23 10:44:33  5     Soporte Lima    2     Renzo      5 
2011-11-23 10:44:33  5     Soporte Lima    2     Renzo      5 
2011-11-23 11:00:58  6     Soporte Lima    2     Miguel      6 

如果你注意到了“表1”每次都重复TICKET_ID在“表2”匹配“表1”我怎么能打印事端像THI:

Created:   Ticket_id (Table 1):  Dept_name:  Priority_id:   Firstname:    Ticket_id (Table 2): 
2011-11-23 10:04:33  2     Soporte Lima    2     Juan Carlos     2 
2011-11-23 10:28:55  3     Soporte Lima    2     Antonio Arturo    3 
2011-11-23 10:42:07  4     Soporte Lima    2     Renzo      4 
2011-11-23 10:44:33  5     Soporte Lima    2     Renzo      5 
                                 5 
                                 5 
2011-11-23 11:00:58  6     Soporte Lima    2     Miguel      6 
重复当

在“表1”的部分空白只打印了“表2”列

我想包括所有的“表2”中的“表1”的结果,但没有重复的信息的“表“1列,而不是空格,并继续打印”表2“列。 有没有办法做到这一点?

+0

诀窍在于LEFT JOIN。例如'FROM staff AS t2 LEFT JOIN ticket AS t1 ON(t1.staff_id = t2.staff_id),department AS t3,prod_cambio AS t4'。那么你不应该在where子句中加入't1.staff_id = t2.staff_id'。您应该接收NULL而不是空格。 – Nightwolf

回答

0

更改输出:

$last_ticket_id = 0; 
while ($row = mysql_fetch_row($result)) { 
    if ($last_ticket_id == $row[1]) { $print_data = false; } else { $print_data = true; } 
    for ($j = 0; $j < 39; $j++) { 
     // not understand this but i leav 
     if ($j <= 25) { $csv_output .= $row[$j]. " ,"; } 
     // check is leav empty or not 
     if (!$print_data && in_array($j, array(0,1,2,3,4))) { 
      $csv_output .= " ,"; 
     } else { 
      $csv_output .= $row[$j]. " ,"; 
     } 
    } 
    $csv_output .= "\n"; 
    $last_ticket_id = $row[1]; 
} 
+0

谢谢!我还有一个问题。 我无法打印所有2个表的所有信息。我的sql查询只显示匹配的行。我怎么能打印所有的2表我的意思是“表1”+“表2”? – user995691

+0

字段是differents – user995691

+0

我解决了它与左连接。谢谢 – user995691

0

你有没有试过SELECT DISTINCT?从您的示例结果来看,它看起来像添加一个独特的会消除重复的行。