Syntax error while testing the query in phpmyadmin [closed]

-4

Error resolved.

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,
    ifnull((SELECT a.percentual FROM $_base.avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) = $ano AND month(a.data) = ".$_parametros['mes1']." 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) <= $ano AND month(a.data) < ".$_parametros['mes1']." AND curdate() >= '$ano-".$_parametros['mes1']."-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) = $ano AND month(a.data) = ".$_parametros['mes2']." 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) <= $ano AND month(a.data) < ".$_parametros['mes2']." AND curdate() >= '$ano-".$_parametros['mes2']."-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) = $ano AND month(a.data) = ".$_parametros['mes3']." 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) <= $ano AND month(a.data) < ".$_parametros['mes3']." AND curdate() >= '$ano-".$_parametros['mes3']."-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) = $ano AND month(a.data) = ".$_parametros['mes4']." 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) <= $ano AND month(a.data) < ".$_parametros['mes4']." AND curdate() >= '$ano-".$_parametros['mes4']."-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) = $ano AND month(a.data) = ".$_parametros['mes5']." 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) <= $ano AND month(a.data) < ".$_parametros['mes5']." AND curdate() >= '$ano-".$_parametros['mes5']."-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) = $ano AND month(a.data) = ".$_parametros['mes6']." 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) <= $ano AND month(a.data) <".$_parametros['mes6']." AND curdate() >= '$ano-".$_parametros['mes6']."-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"

I'm trying to find the error but I can not: Please, if anyone can help me I'll be very grateful! Hugs.

Query updated to phpmyadmin

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,
    ifnull((SELECT a.percentual FROM avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) = 2015 AND month(a.data) = 1 ORDER BY a.data DESC LIMIT 1),
    ifnull((SELECT a.percentual FROM avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) <= 2015 AND month(a.data) <1 AND curdate() >= 2015-1-01' ORDER BY a.data DESC LIMIT 1), 0)) as '01',
    ifnull((SELECT a.percentual FROM avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) = 2015 AND month(a.data) =2 ORDER BY a.data DESC LIMIT 1),
    ifnull((SELECT a.percentual FROM avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) <= 2015 AND month(a.data) < 2 AND curdate() >= 2015-2-01' ORDER BY a.data DESC LIMIT 1), 0)) as '02',
    ifnull((SELECT a.percentual FROM avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) = 2015 AND month(a.data) = 3 ORDER BY a.data DESC LIMIT 1),
    ifnull((SELECT a.percentual FROM avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) <= 2015 AND month(a.data) < 3 AND curdate() >=2015-3-01' ORDER BY a.data DESC LIMIT 1), 0)) as '03',
    ifnull((SELECT a.percentual FROM avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) = 2015 AND month(a.data) = ".$_parametros['mes4']." ORDER BY a.data DESC LIMIT 1),
    ifnull((SELECT a.percentual FROM avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) <= 2015 AND month(a.data) < 4 AND curdate() >=2015-4-01' ORDER BY a.data DESC LIMIT 1), 0)) as '04',
    ifnull((SELECT a.percentual FROM avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) = 2015 AND month(a.data) = 5  ORDER BY a.data DESC LIMIT 1),
    ifnull((SELECT a.percentual FROM avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) <= 2015 AND month(a.data) < 5 AND curdate() >=2015-5-01' ORDER BY a.data DESC LIMIT 1), 0)) as '05',
    ifnull((SELECT a.percentual FROM avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) = 2015 AND month(a.data) = 6  ORDER BY a.data DESC LIMIT 1),
    ifnull((SELECT a.percentual FROM avaliacoes a WHERE a.status = 'A' AND a.codiniciativa = i.codiniciativa AND year(a.data) <= 2015 AND month(a.data) < 6 AND curdate() >= 2015-6-01' ORDER BY a.data DESC LIMIT 1), 0)) as '06' 
    FROM perspectivas p  
    LEFT JOIN temasestrategicos te ON p.codperspectiva = te.codperspectiva 
    LEFT JOIN objetivos o ON te.codtemaestrategico = o.codtemaestrategico 
    LEFT JOIN .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
    
asked by anonymous 31.08.2016 / 22:09

1 answer

6

phpMyAdmin does not accept PHP variables, in the example you used: $_base.avaliacoes .

Try to do: echo $sua_query; and test the result on phpMyAdmin

In addition, on the dates a single quote is missing before:

change from:

curdate() >= 2015-1-01'

To:

curdate() >= '2015-1-01'

    
31.08.2016 / 22:22