Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
Is there a way to mix @variables with placeholders in Perl using DBI and MySQL/MyISAM?

I have a query like this:

SELECT
    foo.bar
FROM
    foo
WHERE
    foo.bang = 0
    AND (
        CASE
            WHEN ? = 2 THEN foo.baz IS NOT NULL
            WHEN ? = 1 THEN foo.baz IS NULL
            ELSE ? NOT IN (1, 2)
        END
    )
    AND (
        (? = 0)
        OR
        (foo.bang = ?)
    )

where the placeholders (?) are used as inputs for filter parameters:

my $query = $aboveQuery;
my ( $results ) = $dbh->DBI::db::selectall_arrayref(
    $query,
    { Slice => {} },
    $bazFilter,
    $bazFilter,
    $bazFilter,
    $bangFilter,
    $bangFilter,
);

This works, but it's not very reader-friendly.
MySQL supports @ variables, which would increase readability:

SET @bazFilter = ?;  -- passed in via selectall_arrayref or execute;
SET @bangFilter = ?;

SELECT
    foo.bar
FROM
    foo
WHERE
    foo.bang = 0
    AND (
        CASE
            WHEN @bazFilter = 2 THEN foo.baz IS NOT NULL
            WHEN @bazFilter = 1 THEN foo.baz IS NULL
            ELSE @bazFilter NOT IN (1, 2)
        END
    )
    AND (
        (@bangFilter = 0)
        OR
        (foo.bang = @bangFilter)
    );

I'd like to do something like this instead:

my $query = $aboveAtVariablizedQuery;
my ( $results ) = $dbh->DBI::db::selectall_arrayref(
    $query,
    { Slice => {} },
    $bazFilter,
    $bangFilter,
);

but MyISAM apparently won't do multiple statements in a single query.

My google-fu is failing me.
Is there a good way to mix-and-match the @variables with the placeholders?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Do each statement separately, using execute not one of the fetch methods.

@ variables are local to the connection, so there is no problem with contamination between threads.

If the goal is to have a single query with no repeated substitutions, then consider:

SELECT  foo.bar
    FROM  ( SELECT baz = ?, bang = ? ) AS init
    JOIN  foo
    WHERE   foo.bang = 0
        AND (
            CASE
                WHEN init.baz = 2 THEN foo.baz IS NOT NULL
                WHEN init.baz = 1 THEN foo.baz IS NULL
                ELSE init.baz NOT IN (1, 2)
            END
        )
        AND (
            (init.bang = 0)
            OR
            (foo.bang = init.bang)
        );
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda