How to create a dynamic query with Left Join?

Asked

Viewed 92 times

1

How can I leave this query dynamic , she is already working only that she only lists one semester each year and wanted her to list two, like when I select the first she lists the first and when I request the second she lists the second.

public function RetornaIndoperacionais($ano){

    if($ano == "2016"){
        $_base = "pla_est_2016";  
    }else{
      $_base = "pla_est"; 
    }


    $Query = ("SELECT i.codiniciativa,
    te.nome as nome_tema,
    te.codtemaestrategico as cod_te,
    p.observacoes as per_obs,
    te.sequencia as tem_sequencia,
    p.sigla as per_sigla,
    p.nome as nome_perspectiva,
    p.codperspectiva as cod_perspectiva,
    o.sequencia as sequencia_obj,
    o.codobjetivo as cod_obj,
    o.nome as nome_obj,concat(p.sigla, ' ', te.sequencia,
    '.', o.sequencia, '.',i.sequencia) as codigo,i.nome as nom_iniciativa,
    i.sequencia as iniciativa_sequencia,
    i.metas,i.responsavel,i.indicadores,
    (SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) = 2016 AND month(a.data) = 1 ORDER BY a.data DESC LIMIT 1),
    ifnull((SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) <= 2016 AND month(a.data) < 1 AND curdate() >= '2016-01-01' ORDER BY a.data DESC LIMIT 1), 0)) as '01',
    ifnull((SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) = 2016 AND month(a.data) = 2 ORDER BY a.data DESC LIMIT 1),
    ifnull((SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) <= 2016 AND month(a.data) < 2 AND curdate() >= '2016-02-01' ORDER BY a.data DESC LIMIT 1), 0)) as '02',
    ifnull((SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) = 2016 AND month(a.data) = 3 ORDER BY a.data DESC LIMIT 1),
    ifnull((SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) <= 2016 AND month(a.data) < 3 AND curdate() >= '2016-03-01' ORDER BY a.data DESC LIMIT 1), 0)) as '03',
    ifnull((SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) = 2016 AND month(a.data) = 4 ORDER BY a.data DESC LIMIT 1),
    ifnull((SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) <= 2016 AND month(a.data) < 4 AND curdate() >= '2016-04-01' ORDER BY a.data DESC LIMIT 1), 0)) as '04',
    ifnull((SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) = 2016 AND month(a.data) = 5 ORDER BY a.data DESC LIMIT 1),
    ifnull((SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) <= 2016 AND month(a.data) < 5 AND curdate() >= '2016-05-01' ORDER BY a.data DESC LIMIT 1), 0)) as '05',
    ifnull((SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) = 2016 AND month(a.data) = 6 ORDER BY a.data DESC LIMIT 1),
    ifnull((SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) <= 2016 AND month(a.data) < 6 AND curdate() >= '2016-06-01' ORDER BY a.data DESC LIMIT 1), 0)) as '06' 
    FROM $_base.perspectivas p 
    LEFT JOIN $_base.temasestrategicos te ON p.codperspectiva = te.codperspectiva 
    LEFT JOIN $_base.objetivos o ON te.codtemaestrategico = o.codtemaestrategico 
    LEFT JOIN $_base.iniciativas i ON o.codobjetivo = i.codobjetivo 
    WHERE p.codperspectiva = '5' AND p.status = 'A' AND te.status = 'A' AND o.status = 'A' AND i.status = 'A'ORDER BY p.sequencia ASC,te.sequencia ASC,
    o.sequencia ASC,CAST(i.sequencia AS DECIMAL) ASC" );


    //echo "</pre>"; print_r($Query); exit;
    //while ($_indoperacionais = mysql_fecth_array($Query));

    try {
          $this->search = $this->MySql->prepare($Query);
          //$this->search->bindParam(":nome", $this->indoperacionais);
          $this->search->execute();
    } catch(PDOException $e) {
          die($e->getMessage());
    }

    $_result = $this->search->fetchAll(PDO::FETCH_ASSOC);

    return $_result;

And this is the code in PHP to understand how I want the call to work.

/* codigo para gerar os paramentros  */
    $ano  = $_POST["ano"];
    $_perspectiva = $_POST["perspectiva"];

    //echo "<pre>"; print_r ($_POST);exit;
    if ($_POST["semestre"] == "primeiro") {
        $_parametros = array (
        "ano" => $ano,
        "mes1" => 1, "mes2" => 2, "mes3" => 3,
        "mes4" => 4, "mes5" => 5, "mes6" => 6,
        "nomeMes1" => "Janeiro", "nomeMes2" => "Fervereiro", "nomeMes3" => "Março",
        "nomeMes4" => "Abril", "nomeMes5" => "Maio", "nomeMes6" => "Junho",
        "perspectiva" => $_perspectiva);
    }else if ($_POST["semestre"] == "segundo") {
        $_parametros = array (
        "ano" => $ano,
        "mes1" => 7, "mes2" => 8, "mes3" => 9,
        "mes4" => 10, "mes5" => 11, "mes6" => 12,
        "nomeMes1" => "Julho", "nomeMes2" => "Agosto", "nomeMes3" => "Setembro",
        "nomeMes4" => "Outubro", "nomeMes5" => "Novembro", "nomeMes6" => "Dezembro",
        "perspectiva" => $_perspectiva);
    }else {
        $error = "ERRO";
    }

    //echo "<pre>"; print_r ($_parametros);exit;
    //Instanciando a classe
    $_model = new model_Indoperacionais();
    $_indoperacionais = $_model->RetornaIndoperacionais($ano,$_parametros);
  • Allan, can you enter the beginning of the code ? has a lost variable in the middle of your query.

  • I removed the PHP tags because the language is irrelevant to your question.

  • @gmsantos the [php] tag will be even irrelevant?

  • @gmsantos there are the codes , have to check for me ?

  • @ramaral I don’t see where PHP is relevant in the question. The problem in my view is the construction of the query, with this lot of subquery with values stoned.

  • @allanaraujo the best approach is to redo this monstrous query ai. When you have calmer revisit the question and elaborate an answer.

  • 1

    @gmsantos The way to parameterize and assign values depends on the language used, if it were in C# I would know how to answer, in PHP no.

  • @ramaral the only thing I want is to let WHERE p.codperspectiva = '5' stay in dynamic mode , type when selecting one of the 8 perspective it selects automatically in WHERE p.codperspectiva = '' -

  • What you need to do is replace that 5 with a variable or placeholder and then replace with the value to be used before "run" to query. How this can be done in PHP I don’t know. I suggest you edit the question and use a simple query as an example. All this code and the size of the query may be hindering a response.

  • @gmsantos I want to let WHERE p.codperspectiva = '5' stay in dynamic mode , type when selecting one of the 8 perspective it selects automatically in WHERE p.codperspectiva = ''

Show 5 more comments
No answers

Browser other questions tagged

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