How i can make a select from 2 or more tables

Discussion in 'Server Operation' started by rutame, Oct 22, 2011.

  1. rutame

    rutame Member

    Hello to everyone

    I need make a select from many tables, like this:

    SELECT FROM clientes,coches,vendedores WHERE clientes.nif="$nifor coches.matricula ="$matriculaor vendedores.nif="$nif"
    But i need that the BD show only the register founded, and right now it show all registers.

    I need this for one input text for search registers in the BD.

    Thank you
  2. falko

    falko Super Moderator ISPConfig Developer

    What do you mean with that? :confused:
  3. rutame

    rutame Member

    I mean that if the select found one register, for example: cliente.nif shows only that register, because now its load in the array registers from the 3 tables:

    $buscar = $_GET['buscar'];

    $resultado = mysql_query ("SELECT * FROM vehiculos,clientes,vendedores WHERE vehiculos.matricula = '$buscar' or clientes.nif='$buscar' or vendedores.nif='$buscar' limit 1");

    $ext=substr($foto1, -4);
    $observaciones=nl2br($observaciones); //Esto es para respetar los saltos de linea.

    while ($registro = mysql_fetch_row($resultado)) {  
        $foto1 = "$ruta_img"."$registro[2]";
        if (!empty($foto1)){
        $foto1 = "<img src=$foto1 class='fotito'>";


    <a href=panel.php?modulo=vehiculo_uno&idv=<?php echo $registro[0];?>>
        <div id="registro_linea">

                <li>Fecha de alta: <?php echo $registro[1];?></li>
                <li>Número Chasis:<?php echo $registro[3];?></li>
                <li>Marca: <?php echo $registro[4];?></li>
                <li>Modelo: <?php echo $registro[5];?></li>
                <li>Matricula: <?php echo "$registro[6] | ""Fecha Matricula: $registro[7]";?></li>
                <li>Kilómetros: <?php echo $registro[8];?></li>
                <li>Precio de compra:<?php echo $registro[9];?></li>
                <li>Precio de venta: <?php echo $registro[10];?></li>
                <li>Situación: <?php echo $registro[13];?></li>
          <?php echo $foto1;?>    

             <ul> <h2>DATOS DEL CLIENTE</h2>
                    <li>Fecha de Operación: <?php echo $registro[10];?></li>
                    <li>NIF/CIF:<?php echo $registro[1];?></li>
                    <li>Nombre:<?php echo "$registro[2]"."$registro[3]";?></li>
    In this case $register[2] could to be cliente.nif and could be coches.matricula but if match cliente.nif if exist the script return vehiculo.matricula too, its weird, i dont know if i am explainig well... :)
    Last edited: Oct 22, 2011
  4. falko

    falko Super Moderator ISPConfig Developer

    SELECT coches.matricula ...
    instead of
    SELECT * ...
  5. rutame

    rutame Member

    thank you Falko, i know that is very hard to explain my trouble with that code for two reasons: one i dont know speak well english and two always is dificult to explain things like that....

    My task is make a search form in many tables and that only return the askeds values. Example i make:
    select * from customers, cars, providers where"$search" or cars.num_plate="$search" or = "$search"

    <label for="Search">Search for Number Plate, Identification Personal, or Customer Code</label>
    <input type="text" name="search" placeholder="N. Plate - Ident." />
    Returns something like (for customer match):
    Id: 123456789ABC
    Name: Pepito Perez

    And the search returns something like this for coches (cars) match:
    N. Plate: 0000ZZZ
    Marca: BMW
    Modelo: 325ci e92

    Right now the query returns something like this:

    N. Plate: 0000ZZZ
    Marca: BMW
    Modelo: 325ci e92
    Id: 123456789ABC
    Name: Pepito Perez

    Good anyway i will try your tip, thanks althoug if you can i would like so much to know how i can to do that.

    Thank you very much Falko (der komisar ;)

Share This Page