1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[SQL] How to query a dynamic list of strings using SQL?

Discussão em 'Outras Linguagens' iniciado por Stack, Outubro 3, 2024 às 17:13.

  1. Stack

    Stack Membro Participativo

    I have a table Customers with multiple columns one of which is state.

    I want a dynamic query : When my stateInput is empty I want all customers, if not I want customers that have the state given in my stateInput.

    stateInput is a dynamic value I receive from my fastapi request params

    stateInput = request.query_params['stateInput '] like so.

    stateInput could be '' or 'NY,CA' or 'NY' or 'NY,CA,BAL'

    How do I dynamically write an sql query for this ?

    Tried the following but these are giving errors/not working :

    Query 1 :

    select *
    from Customers
    where :)stateInput = '' or state in :)stateInput))


    Query 2 :

    stateInputList = request.query_params['stateInput '].split(',')

    select *
    from Customers
    where :)stateInput = '' or state in :)stateInputList))


    Query 3 :

    stateInputTuple = tuple(request.query_params['stateInput'].split(','))

    select *
    from Customers
    where :)stateInput = '' or state in :)stateInputTuple))

    Continue reading...

Compartilhe esta Página