Include if in query

1

I would like to include a condition in the sql query, follows.

$Q_horario =    "SELECT   ".
            "   H.horario As horario,   ".
            "   L.codigo As codlinha,   ".
            "   L.descricao As linha,   ".
            "   L.tipo As tipo,   ".
            "   IF (H.sentido='I',L.saida,L.chegada) As saida,   ".
            "   IF (H.sentido='I',L.chegada,L.saida) As chegada,   ".
            "   T. valor As tarifa   ".
            "FROM tarifas T  ". 
            "   INNER JOIN linhas L ON L.codigo = T.linha  ". 
            "   INNER JOIN horarios H ON H.linha = L.codigo  ". 
            if ($entra == 3){
            "INNER JOIN cidade C on C.origem = T.origem AND C.destino = T.destino ".
                                       "AND C.entra = 'N' ".
                                       "AND C.diasemana = 'DOM'".
                                       "AND C.horario = H.horario".
            }
            "WHERE T.origem = $origem ".  
            "   AND T.destino= $destino   ".
            "   AND H.sentido = '$sentido'".
            "   AND ((MID(H.frequencia, '$dia', 1) = 'S') AND (MID(H.frequencia1002, '$dia', 1) = 'S')) ".
            "ORDER BY T.linha, H.horario ";

Would you have any way to include this if ?, type a filter;

because of this form of syntax error

    
asked by anonymous 21.05.2018 / 16:07

1 answer

0

You may have to study a bit more about programming logic. The solution seems to be very simple, just terminate the concatenation on the first line, before if . Then you proceed with her after him. And do the concatenation within the if as well, like this:

$Q_horario =    "SELECT   ".
        "   H.horario As horario,   ".
        "   L.codigo As codlinha,   ".
        "   L.descricao As linha,   ".
        "   L.tipo As tipo,   ".
        "   IF (H.sentido='I',L.saida,L.chegada) As saida,   ".
        "   IF (H.sentido='I',L.chegada,L.saida) As chegada,   ".
        "   T. valor As tarifa   ".
        "FROM tarifas T  ". 
        "   INNER JOIN linhas L ON L.codigo = T.linha  ". 
        "   INNER JOIN horarios H ON H.linha = L.codigo  "; // encerra a concatenação...

         // faz o "IF"
        if ($entra == 3){
        $Q_horario = $Q_horario . "INNER JOIN cidade C on C.origem = T.origem AND C.destino = T.destino ".
                                   "AND C.entra = 'N' ".
                                   "AND C.diasemana = 'DOM'".
                                   "AND C.horario = H.horario";// encerra a concatenação do "if"
        }// encerra o "if"

        // faz o resto da concatenação:
        $Q_horario = $Q_horario . "WHERE T.origem = $origem ".  
        "   AND T.destino= $destino   ".
        "   AND H.sentido = '$sentido'".
        "   AND ((MID(H.frequencia, '$dia', 1) = 'S') AND (MID(H.frequencia1002, '$dia', 1) = 'S')) ".
        "ORDER BY T.linha, H.horario ";

To concatenate you can use both $variavel = $variavel . "outra coisa"; and $variavel .= "outra coisa"; .

    
21.05.2018 / 16:12