Use result of an SQL query as attribute to another query.(MYSQL)

Asked

Viewed 188 times

0

I have the following structure:

tabela (Grupo):
-cd_grupo
-nm_grupo
-ds_filtros -> Aqui armazeno parte do comando SQL utilizado como filtro , para posteriormente atualizar.

I store the following information.

cd_grupo  |   nm_ grupo  |   ds_filtro
   1      |    grupo1    | AND (uf = 'PR') AND (genero = 'F')
   2      |    grupo2    | AND (nasc >= '1990-02-20')

there is another link table, where bind employees according to the group filter.

cd_grupo  |   cd_funcionario
   1      |        14
   1      |        19
   1      |        35

my doubt is necessary to update the link table when changes the status of the employee and when it is inserted working new.

This way down I know it doesn’t work but the idea would be this:

SELECT cd_funcionario FROM funcionario WHERE (SELECT ds_filtros FROM grupo)

That would bring the filters "AND (Uf = 'PR') AND (genero = 'F')" and add after the clause WHERE

Have some alternative to store filters or if this way you have to search for all filters in the group table and apply them in another SQL command be it REPLACE, UPDATE or INSERT ?

I am using PHP with Mysql.

  • in case the idea is to store the command, not results?

  • from what I understood and only you save the result in a variable and then put this variable after the WHERE

  • 'I store the controls used in the respective filter

1 answer

0

I have an example that I’ve done a do, so yours will stay but or so in my bank has inserir a descrição da imagem aqui then he shows inserir a descrição da imagem aqui

 <?php
    $host ="localhost";
    $usu="root";
    $senha="";
    $bd="ts";
    $link = mysqli_connect($host, $usu, $senha, $bd);
        $sql="SELECT `numero` FROM `ts` WHERE id=1";
        $execut=mysqli_query($link,$sql);
        while($row= MYSQLI_FETCH_ARRAY($execut)){
                $filtro=$row['numero'];
        }
        $sql="SELECT `numero` FROM `ts` WHERE $filtro";
        $execut=mysqli_query($link,$sql);
        while($row= MYSQLI_FETCH_ARRAY($execut)){
                $filtro=$row['numero'];
        }
        echo $filtro;
    ?>
  • had thought this way, would using direct TRIGGERS in MYSQL have some way ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.