MYSQL查询包括所有“表2”在“表1”中

发布于 2024-12-20 04:13:22 字数 4361 浏览 1 评论 0原文

我有这个 php 将日期导出到 csv 文件中:

$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;

我选择 2 个不同的表并尝试将它们转换为一个。 我的问题是这样的:

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

如果您注意到每次“表 2”中的 Ticket_id 与“表 1”匹配时“表 1”都会重复,我如何打印类似这样的内容:

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”列

我想在“表 1”结果中包含所有“表 2”,但不重复“表”1 列的信息,而是放置空格并继续打印“表 2”列。 有办法做到吗?

i have this php to export date into a csv file:

$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 selecting 2 diferent tables and trying to convert them in one.
My problem is this:

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

if you notice the "table 1" is repeating every time the Ticket_id in "Table 2" match the "table 1" how can i print somethin like 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

blank space in "table 1" part when repeating and only print the "table 2" columns

i want to include all "table 2" in "table 1" result but with no repeating the info of the "table "1 columns instead put blank space and continue printing the "table 2" columns.
is there a way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉风有信 2024-12-27 04:13:22

改变输出:

$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];
}

Change output:

$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];
}
情绪 2024-12-27 04:13:22

你试过选择不同吗?从您的示例结果来看,看起来只需添加不同的行即可消除重复的行。

have you tried SELECT DISTINCT? judging by your example result it looks like just adding a distinct would eliminate duplicate rows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文