mardi 28 mars 2017

INSERT INTO table_1 SELECT FROM table_2 WHERE condition ORDER BY random value from table 3 (not table_2)

I have 3 tables:

Table_1 is flux_zilnic: Table_2 is tableta: tableta And table_3 is birouri: birouri

I have the PHP code:

function search_in_birouri()
    {
        $connection = db_connect();
        $query = mysqli_query($connection, "SELECT * FROM birouri WHERE disponibilitate = 'LIBER' AND locatie_actuala = 'Orhideea' AND pauza = '' AND closed_program = '' AND feedback = '' ORDER BY id ASC") or die(mysqli_error($connection));
        $row = mysqli_fetch_assoc($query);

        $username = $row['username'];
        $nr_birou = $row['nr_birou'];
        $disponibilitate = $row['disponibilitate'];
        $locatie_actuala = $row['locatie_actuala'];
        $pauza = $row['pauza'];
        $closed_program = $row['closed_program'];
        $feedback = $row['feedback'];
        $inregistrare_clienti = $row['inregistrare_clienti'];

        return array('username' => $username, 'nr_birou' => $nr_birou, 'disponibilitate' => $disponibilitate, 'locatie_actuala' => $locatie_actuala, 'pauza' => $pauza, 'closed_program' => $closed_program, 'feedback' => $feedback, 'inregistrare_clienti' => $inregistrare_clienti);
        unset($connection);
    }

function search_in_tableta()
    {
        $connection = db_connect();
        $query = mysqli_query($connection, "SELECT * FROM tableta WHERE locatie = 'Orhideea' ORDER BY `tableta`.`id` ASC") or die(mysqli_error($connection));
        $row = mysqli_fetch_assoc($query);

        $clientID = $row['clientID'];
        $nume_client = $row['nume_client'];
        $data_inregistrare = $row['data_inregistrare'];
        $ora_inregistrare = $row['ora_inregistrare'];
        $locatie = $row['locatie'];
        $status = $row['status'];

        return array('clientID' => $clientID, 'nume_client' => $nume_client, 'data_inregistrare' => $data_inregistrare, 'ora_inregistrare' => $ora_inregistrare, 'locatie' => $locatie, 'status' => $status);
        unset($connection);
    }

function start_working()
    {
        $username = cauta_in_birouri();
        $nr_birou = cauta_in_birouri();
        $disponibilitate = cauta_in_birouri();
        $paused = cauta_in_birouri();
        $closed_program = cauta_in_birouri();
        $feedback = cauta_in_birouri();
        $inregistrare_clienti = cauta_in_birouri();
        $clientID = cauta_urmatorul_client();
        $nume_client = cauta_urmatorul_client();
        $connection = db_connect();
        $data_curenta = date_create('');
        $ora_alocare = date_format($data_curenta, 'H:i');
        $username = $username['username'];
        $nr_birou = $nr_birou['nr_birou'];
        $clientID = $clientID['clientID'];
        $nume_client = $nume_client['nume_client'];
        $disponibilitate = $disponibilitate['disponibilitate'];
        $paused = $paused['pauza'];
        $closed_program = $closed_program['closed_program'];
        $feedback = $feedback['feedback'];
        $inregistrare_clienti = $inregistrare_clienti['inregistrare_clienti'];
        //
        if($nume_client == NULL)
        {
            //Salveaza in log
            $linie_text = "NU DETECTEZ NICIUN CLIENT ÎNREGISTRAT ÎN ORHIDEEA";
            $new_line = "==> ";
            $handle = fopen('loguri/actiuni_comandate.txt','a+');
            fwrite($handle, $new_line . $linie_text . " (" . $ora_alocare . ")" . "<br>" . "\r\n");
            fclose($handle);
        } else if($nume_client != NULL && $disponibilitate == 'LIBER' && $paused == NULL && $closed_program == NULL && $feedback == NULL && $inregistrare_clienti == NULL)
        {
            $query = mysqli_query($connection,"
                INSERT INTO `flux_zilnic` (motiv,nume_client,clientID,data_inregistrare,ora_inregistrare,status_alocare,ora_alocare,status_preluare,ora_preluare,status_rezolvare,ora_rezolvare,operator,birou,fisa_service,locatie_actuala,fise_cu_probleme,observatii,status)
                SELECT  '',nume_client,clientID,data_inregistrare,ora_inregistrare,'ALOCAT','$ora_alocare','','','','','$username','$nr_birou','',locatie,'','',status
                FROM `tableta` WHERE locatie = 'Orhideea' ORDER BY id ASC LIMIT 1") or die("NU AM ALOCAT URMĂTORUL CLIENT DEOARECE: <br>" . mysqli_error($connection));

            $queryTwo = mysqli_query($connection,"UPDATE `birouri` SET `disponibilitate` = 'OCUPAT' WHERE username = '$username'") or die("ÎN ORHIDEEA NU AM SCHIMBAT BIROUL ÎN STATUS OCUPAT, DEOARECE: <br>" . mysqli_error($connection));

            $queryThree = mysqli_query($connection,"DELETE FROM `tableta` WHERE locatie = 'Orhideea' ORDER BY id ASC LIMIT 1") or die("ÎN ORHIDEEA NU AM ȘTERS CLIENTUL DE PE TABLETĂ DIN CAUZA UNEI ERORI: <br>" . mysqli_error($connection));

            if($query && $queryTwo && $queryThree) 
            {
                //Salveaza in log
                $linie_text = "AM ALOCAT CLIENTUL " . $nume_client . " CĂTRE " . $username . " ÎN ORHIDEEA";
                $new_line = "==> ";
                $handle = fopen('loguri/actiuni_comandate.txt','a+');
                fwrite($handle, $new_line . $linie_text . " (" . $ora_alocare . ")" . "<br>" . "\r\n");
                fclose($handle);
            }
        } else
        {
            //Salveaza in log
            $linie_text = "NU ESTE DISPONIBIL NICIUN BIROU ÎN ORHIDEEA. NU POT SĂ ALOC CLIENTUL " . $nume_client;
            $new_line = "==> ";
            $handle = fopen('loguri/actiuni_comandate.txt','a+');
            fwrite($handle, $new_line . $linie_text . " (" . $ora_alocare . ")" . "<br>" . "\r\n");
            fclose($handle);
        }
        unset($connection);
    }

According to $query from start_working() I am taking values from table 2 and table 3 and put them together in table 1.

As you can see in search_in_birouri(), this function returns me values from table 3 ORDER BY id ASC.

This means that next thing will happen: 1. Function start_working() - $query will copy all values from table_2 and only nr_birou and username from table_3 and put them together in table_1. 2. Function start_working() - $queryTwo will update table_3 ... 3. Function start_working() - $queryThree will delete one row from table_2

Everything work just fine and no problems.

Except I want to select a random $nr_birou (and the username which belongs to it) from table_3 and put it in table_1 combined with values from table_2.

How can I do that? How to copy values from table_2 with random nr_birou and username from table_3 into table_1?

I really, really tried to explain the best I could.




Aucun commentaire:

Enregistrer un commentaire