Query with fields from different tables

0

I am setting up a CRUD and I want to join fields from different tables, this would be:

  • Customers table: customer, first and last name
  • Invoice table: ID and quantity

The code of the SELECT would be:

$id  = $_GET[ "IdCliente" ];
$nom = $_GET[ "Nombre" ];
$ape = $_GET[ "Apellido" ];
$idfactura = $_GET[ "IdFactura" ];



$consulta = "SELECT Idcliente, nombre, apellido, idfactura.factura 
    WHERE Idcliente=:idcli, Nombre=:nomcli, Apellido=:apecli, Idfactura=:idfact"

Is this code well written?

    
asked by Santi Corso 16.10.2017 в 23:19
source

1 answer

0

First, the field CustomerID should be a primary key or in any case a single type field. It is the only data that you will need to consult data from the clients table. I suppose that in the table invoice you have an FK that will relate it to that of clients .

Try the query:

$consulta = "SELECT 
    clientes.Idcliente, 
    clientes.nombre, 
    clientes.apellido, 
    factura.idfactura
        FROM clientes, factura
        WHERE clientes.Idcliente=:idcli 
            AND factura.Idcliente=:idcli";

I recommend that you create a simple database with some records, and try to make queries and other operations before trying to integrate them into an application.

    
answered by 17.10.2017 / 00:32
source