Include if in query

Asked

Viewed 107 times

1

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

$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 that if? , sap like a filter;

because of this form of syntax error

1 answer

0

You may have to study a little more programming logic. The solution seems to be quite simple, just close the concatenation on the first line, before the if. Then you proceed with it after it. And do the concatenation inside the if too, 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"; how much $variavel .= "outra coisa";.

  • 1

    $Q_horario = $Q_horario . " can be exchanged for $Q_horario .= "

  • @I edited and entered this information. I usually try to simplify the code to make it "understandable" to AP, after all it is a simple question, maybe he would not know what it is .=. But this way it gets cleaner and works the same way. :)

  • It is not concatenating, it does select even with the condition.

  • @Arimelo, I did not understand very well... He will perform the SELECT anyway, the condition is only for the INNER JOIN within the if. An error occurs?

  • @bio then, select enters yes, but it is not running if, no error occurs, just skip the if. select with if and without if is tested and works 100%

Browser other questions tagged

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