Select 2 wheres in a query with more than 1 table - PHP, SQL

Asked

Viewed 54 times

0

I am developing a legal system for registering processes and that can be shared between the users involved.
At the moment, I need to filter processes between dates. I already managed to do this where all existing processes are listed, but now I want to filter the dates of processes of a particular client.

For example, I have a client called Marcelo and I want to list all the processes of this client. Now, I want to filter the processes of this client by date. How to do?

Below I have the code that filters by date

function GetProcessoData($data_ini,$data_fim){

    $query  = "SELECT * FROM {$this->prefix}processos p";
    $query .= " INNER JOIN {$this->prefix}clientes c ON p.pro_cliente = c.cli_id";
    $query .= " INNER JOIN {$this->prefix}user u ON p.pro_remetente = u.user_id";

    $query.= " WHERE pro_data between :data_ini AND :data_fim ";

    $query .= $this->PaginacaoLinks("pro_id", $this->prefix."processos WHERE pro_data between ".$data_ini." AND ".$data_fim);

   // passando os parametros  
    $params = array(':data_ini'=>$data_ini, ':data_fim'=>$data_fim);

    // executando a SQL
    $this->ExecuteSQL($query,$params);

    $this->GetListaAll();
}
  • 1

    André, from what I understand, just add one more condition in your Where, filtering the client table by the field that stores the name of the same, your function would have to receive a third argument, being the client’s name.

1 answer

1

Good afternoon, I would do something like:

function GetProcessoData($data_ini,$data_fim,$nome='%'){

    $query  = "SELECT * FROM {$this->prefix}processos p";
    $query .= " INNER JOIN {$this->prefix}clientes c ON p.pro_cliente = c.cli_id";
    $query .= " INNER JOIN {$this->prefix}user u ON p.pro_remetente = u.user_id";

    $query.= " WHERE c.nome like :nome and pro_data between :data_ini AND :data_fim ";

    $query .= $this->PaginacaoLinks("pro_id", $this->prefix."processos WHERE pro_data between ".$data_ini." AND ".$data_fim);

   // passando os parametros  
    $params = array(':nome' =>nome, ':data_ini'=>$data_ini, ':data_fim'=>$data_fim);

    // executando a SQL
    $this->ExecuteSQL($query,$params);

    $this->GetListaAll();
}

Because that way if you don’t pass the name filter parameter it will bring all the clients. I hope I’ve helped

Browser other questions tagged

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