简体   繁体   中英

How to do multiple INNER JOIN dynamically in php

This is the first I've made a code using multiple INNER JOIN S that their relationship is only a table.

This is what I'm doing:

$table = $_POST['tabla'];
$campos = $_POST['campos'];
$campoRelacion = $_POST['campo_relacion'];

    **//Code that i forget to put**

   *// Create an array that will contain the fields
   $ field = array ();

   //walk the array of fields and is assigned to the array
   //that was created previously
    foreach ($ fields as $ val) {
      $ field [] = "". $ val. "";
    }

   //We separate the fields in the array with commas
   $ field = join (',', $ field);*


$mainTable = $table[0];


$sql = 'SELECT '.$field.'
        FROM '.$mainTable.' ';

for ($i = 1; $i<count($table); $i++) { 
    $curTable = $table[$i];
    $joinField = $campoRelacion[$i-1];
    $sql.= 'INNER JOIN '.$curTable.' 
            ON '.$mainTable.'.'.$joinField.' = '.$curTable.'.'.$joinField.' '; 
}

The output of this would be something like this depending on the data I send:

SELECT
slip_plantillas.nombre,
cat_reaseguradoras.nombre,
slips.numero_referencia,
slips.asegurado_original,
tipo_operaciones.nombre
FROM
slips
Inner Join slip_plantillas ON slips.slip_plantillaID = slip_plantillas.slip_plantillaID
Inner Join cat_reaseguradoras ON slips.cat_reaseguradoraID = cat_reaseguradoras.cat_reaseguradoraID
Inner Join tipo_operaciones ON slips.tipo_operacionID = tipo_operaciones.tipo_operacionID

As shown in the table above, code slips remains constant in the joins.

But now I have another sql statement I want to make where slips not constant and change unions.

The query looks like this:

SELECT
slip_dos_reasegurado.prima_cien,
slip_dos_reasegurado.porcentaje_aseguradora,
cat_reaseguradoras.nombre,
slip_dos_capas.capa,
slip_dos_capas.prima_capa,
slip_dos_pagos.cantidad,
slip_dos_pagos.referencia,
estatus.nombre,
slips.tipo_negocio
FROM
slips
Inner Join slip_dos_reasegurado ON slips.slipID = slip_dos_reasegurado.slipID
Inner Join slip_dos_capas ON slip_dos_capas.slip_dos_reaseguradoID = slip_dos_reasegurado.slip_dos_reaseguradoID
Inner Join slip_dos_pagos ON slip_dos_capas.slip_dos_capaID = slip_dos_pagos.slip_dos_capaID
Inner Join cat_reaseguradoras ON cat_reaseguradoras.cat_reaseguradoraID = slip_dos_reasegurado.cat_reaseguradoraID
Inner Join estatus ON estatus.estatusID = slip_dos_pagos.estatusID

As you can see, relations are no longer a single table if they are not mixed with each other depending on the field relationship.

Maybe you need this ?

<?php
$tables = array('tbl1','tbl2','tbl3','tbl4');
$columnTable = array('column1','column2','column3','column4');
$columnRelation =     array('column_relation1','column_relation2','column_relation3','column_relation4');


$mainTable = $tables[0];


$sql = 'SELECT *
    FROM '.$mainTable.' ';

foreach($tables as $index=>$tbl){
    if($index == 0) continue; //Normal only while you know that your array indexes start from 0, in other case you would need variable for this
    $sql .= 'INNER JOIN $tbl ON '.$tbl.'.'.$columnTable[$index].' = '.$tables[$index-1].'.'.$columnRelation[$index-1].' ';
}

echo $sql;
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM