Query is printed on a single td

0

I have a table which reflects a purchase made by a client or buyer, which has an identifier that would be the number of the order, it reflects that the customer requested, but it shows me in several tds the data of the order use a group by but it did not work to put them in a single td This is the structure of the table from which I take the data: detail Here they are the data as shown in the table

And so they are shown in the td I want to organize them to show that all the products of an order are printed in a single td that is to say the order 15 to print in a single td all the products that were generated in that order

<div class="panel panel-info">
                               <div class="panel-heading text-center"><i class="fa fa-refresh fa-2x"></i><h3>Datos del  pedido</h3></div>
                              <div class="table-responsive">
                                  <table class="table table-bordered">
                                      <thead class="">
                                          <tr>
                                              <th class="text-center">#</th>
                                              <th class="text-center">Codigo del producto</th>
                                              <th class="text-center">Cliente</th>
                                              
                                              <th class="text-center">Cantidad de productos</th>
                                              
                                          </tr>
                                      </thead>
                                      <tbody>
                                          <?php
                                            $pedidoU=  ejecutarSQL::consultar("select * from detalle, producto,cliente");
                                            $upp=1;
                                            while($peU=mysql_fetch_array($pedidoU)){
                                                echo '
                                                    
                                                        <tr>
                                                            <td>'.$peU['NumPedido'].'<input type="hidden" name="num-pedido" value="'.$peU['NumPedido'].'"></td>
                                                            <td>'.$peU['CodigoProd'].'-   '.$peU['NombreProd'].'</td>
                                                            <td>';
                                                                $conUs= ejecutarSQL::consultar("select * from cliente where NIT='".$peU['NIT']."'");
                                                                while($UsP=mysql_fetch_array($conUs)){
                                                                    echo $UsP['Nombre'];
                                                                }
                                                    echo   '<td>';
                                                                $conUs= ejecutarSQL::consultar("select Nombre,NumPedido,CodigoProd,CantidadProductos from detalle,cliente group by Nombre ");
                                                                while($UsP=mysql_fetch_array($conUs)){
                                                                    echo  $UsP['CantidadProductos'] ;
                                                                }'</td>
                                                            
                                                        </tr>
                                                      </form>
                                                    </div>
                                                    ';
                                                $upp=$upp+1;
                                            }
                                          ?>
                                      </tbody>
                                  </table>
                              </div>
                            </div>
                        </div>
        </div>
    
asked by Anderson Rey 11.09.2017 в 03:19
source

1 answer

1

I sense that there are design errors in your data, for example, you make at least three queries to obtain data that, with an appropriate design, could be obtained in a single query.

I show you this design, with some data tests, in case it can help you.

It's a design let's say normal of the kind of application it looks like, you try to do.

There are 4 tables:

  • clientes
  • productos
  • pedidos
  • detalles_pedidos

The tables are related as follows:

  • Table pedidos with table clientes using the key cliente_id
  • Table pedidos with table productos using the key producto_id
  • Table detalles_pedidos with table pedidos using the key pedido_id

It's something done quickly, more relationships and refinements are possible ...

The following query will bring you grouped data by pedido_id and a column named productos with data concatenated by comma , :

SELECT 
    cl.cliente_nom,  
    pe.pedido_id, 
    GROUP_CONCAT(pr.producto_nom SEPARATOR ',')productos, 
    pe.fecha
FROM detalles_pedidos_20170911 dp 
    INNER JOIN pedidos_20170911 pe ON dp.pedido_id=pe.pedido_id 
    INNER JOIN productos_20170911 pr ON dp.producto_id=pr.producto_id 
    INNER JOIN clientes_20170911 cl ON pe.cliente_id=cl.cliente_id
GROUP BY pe.pedido_id;

To this query you can apply other types of filters, or bring more data from any of the 4 tables involved in it.

Código: SEE DEMO IN REXTESTER

CREATE TABLE IF NOT EXISTS clientes_20170911 (
    cliente_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    cliente_nom VARCHAR(70)
);


CREATE TABLE IF NOT EXISTS productos_20170911 (
    producto_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    producto_nom VARCHAR(50),
    INDEX (producto_nom)
);

CREATE TABLE IF NOT EXISTS pedidos_20170911 (
    pedido_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    cliente_id INT,
    fecha DATETIME,
    INDEX (cliente_id,fecha),
    FOREIGN KEY (cliente_id) REFERENCES clientes_20170911(cliente_id)
    ON DELETE CASCADE


);


CREATE TABLE IF NOT EXISTS detalles_pedidos_20170911 (
    pedido_id INT, 
    producto_id INT,
    cantidad INT,
    INDEX (pedido_id,producto_id),
    FOREIGN KEY (pedido_id) REFERENCES pedidos_20170911(pedido_id)
    ON DELETE CASCADE
);



INSERT INTO clientes_20170911 (cliente_nom)
    VALUES ('Pedro'),('Santiago'),('Juan')
;


INSERT INTO productos_20170911 (producto_nom)
    VALUES 
        ('Zapatos Chinos no falsicados'),
        ('Mac Book Air 2013'),
        ('Producto 3'),
        ('Producto 4'),
        ('Producto 5')

;


INSERT INTO pedidos_20170911 (cliente_id, fecha)
    VALUES 
        (1,'2017-09-06'),
        (2,'2017-09-06'),
        (3,'2017-09-07'),
        (1,'2017-09-08')
;


INSERT INTO detalles_pedidos_20170911 (pedido_id, producto_id, cantidad)
    VALUES 
        (1,1,5),
        (1,3,2),
        (1,5,1),
        (2,2,1),
        (2,4,1),
        (3,1,1),    
        (3,2,2),
        (3,3,2),
        (4,2,1),
        (4,3,2),
        (4,4,3),
        (4,5,4)
;


SELECT 
    cl.cliente_nom,  
    pe.pedido_id, 
    GROUP_CONCAT(pr.producto_nom SEPARATOR ',')productos, 
    pe.fecha
FROM detalles_pedidos_20170911 dp 
    INNER JOIN pedidos_20170911 pe ON dp.pedido_id=pe.pedido_id 
    INNER JOIN productos_20170911 pr ON dp.producto_id=pr.producto_id 
    INNER JOIN clientes_20170911 cl ON pe.cliente_id=cl.cliente_id
GROUP BY pe.pedido_id;

Resultado

cliente_nom pedido_id   productos                                                    fecha
Pedro       1           Producto 5,Zapatos Chinos no falsicados,Producto 3           06.09.2017 00:00:00
Santiago    2           Mac Book Air 2013,Producto 4                                 06.09.2017 00:00:00
Juan        3           Producto 3,Zapatos Chinos no falsicados,Mac Book Air 2013    07.09.2017 00:00:00
Pedro       4           Producto 5,Producto 3,Producto 4,Mac Book Air 2013           08.09.2017 00:00:00
    
answered by 11.09.2017 / 06:35
source