Query with sqlalchemy with and and or












0















First of all thank you for your help.



My problem is that I have to query the server with a lot of inputs. I am updating a web page that on the past only accept one input but now, i created a checkbox so the user can have more flexibility on the query:



query = session.query(table_name).filter(and_(*conditions) , or_(*multiple_choice_filter))


Where multiple choice is a list with letters like ["A", "B"]
And conditions are a list of conditions:



table_name.c.column_1.op('~*')(filter_1))


Where on the past, there weren't anything else apart from the initial conditions but now I want to add the checkbox.



Somebody now how to combine in the query the conditions from the past with the multiple_choice_filter?



Thank you!










share|improve this question

























  • Using OR in SQLAlchemy - the link in the answer will also help you with the and_ syntax you need.

    – benvc
    Nov 21 '18 at 14:36
















0















First of all thank you for your help.



My problem is that I have to query the server with a lot of inputs. I am updating a web page that on the past only accept one input but now, i created a checkbox so the user can have more flexibility on the query:



query = session.query(table_name).filter(and_(*conditions) , or_(*multiple_choice_filter))


Where multiple choice is a list with letters like ["A", "B"]
And conditions are a list of conditions:



table_name.c.column_1.op('~*')(filter_1))


Where on the past, there weren't anything else apart from the initial conditions but now I want to add the checkbox.



Somebody now how to combine in the query the conditions from the past with the multiple_choice_filter?



Thank you!










share|improve this question

























  • Using OR in SQLAlchemy - the link in the answer will also help you with the and_ syntax you need.

    – benvc
    Nov 21 '18 at 14:36














0












0








0








First of all thank you for your help.



My problem is that I have to query the server with a lot of inputs. I am updating a web page that on the past only accept one input but now, i created a checkbox so the user can have more flexibility on the query:



query = session.query(table_name).filter(and_(*conditions) , or_(*multiple_choice_filter))


Where multiple choice is a list with letters like ["A", "B"]
And conditions are a list of conditions:



table_name.c.column_1.op('~*')(filter_1))


Where on the past, there weren't anything else apart from the initial conditions but now I want to add the checkbox.



Somebody now how to combine in the query the conditions from the past with the multiple_choice_filter?



Thank you!










share|improve this question
















First of all thank you for your help.



My problem is that I have to query the server with a lot of inputs. I am updating a web page that on the past only accept one input but now, i created a checkbox so the user can have more flexibility on the query:



query = session.query(table_name).filter(and_(*conditions) , or_(*multiple_choice_filter))


Where multiple choice is a list with letters like ["A", "B"]
And conditions are a list of conditions:



table_name.c.column_1.op('~*')(filter_1))


Where on the past, there weren't anything else apart from the initial conditions but now I want to add the checkbox.



Somebody now how to combine in the query the conditions from the past with the multiple_choice_filter?



Thank you!







python database sqlalchemy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 15:14









Ilja Everilä

24.3k33763




24.3k33763










asked Nov 19 '18 at 14:55









Borja Perez de GuzmanBorja Perez de Guzman

12




12













  • Using OR in SQLAlchemy - the link in the answer will also help you with the and_ syntax you need.

    – benvc
    Nov 21 '18 at 14:36



















  • Using OR in SQLAlchemy - the link in the answer will also help you with the and_ syntax you need.

    – benvc
    Nov 21 '18 at 14:36

















Using OR in SQLAlchemy - the link in the answer will also help you with the and_ syntax you need.

– benvc
Nov 21 '18 at 14:36





Using OR in SQLAlchemy - the link in the answer will also help you with the and_ syntax you need.

– benvc
Nov 21 '18 at 14:36












1 Answer
1






active

oldest

votes


















0














I found how to do it.The point of my question is to chain AND with OR.



First, create the filters:



conditions = 


for the and statement is very straight forward:



conditions.append(table_name.c.column_name_1.op('~*')(value_1))
conditions.append(table_name.c.column_name_2.op('~*')(value_2))
conditions.append(table_name.c.column_name_3.op('~*')(value_3))

query = session.query(table_name).filter(and_(*conditions)).order_by(table_name.c.column_name_1)


Which is translated to:



SELECT   *   FROM     table_name WHERE (column_name_1 == value_1 AND column_name_2 == value_2  AND column_name_3 == value_3)


Now, to chain conditions with OR and AND:



We use two different variables: one for the AND statement (exactly like above) and another other one like the next one:



filter_or_1.append(table_name.c.column_name_4.op('~*')(value_4))# First possible value for the OR statement
filter_or_1.append(table_name.c.column_name_4.op('~*')(value_5))# Second possible value for the OR statement
filter_or_1.append(table_name.c.column_name_4.op('~*')(value_6))# Third possible value for the OR statement


and the query will be:



query = session.query(table_name).filter(and_(*conditions) , and_(or_(*filter_or_1))).order_by(table_name.c.column_name_1)


Which is translated to:



SELECT   *   FROM     table_name WHERE (column_name_1 == value_1 AND column_name_2 == value_2  AND column_name_3 == value_3 AND(column_name_4==value_4 OR column_name_4==value_5 column_name_4==value_6 ))





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377213%2fquery-with-sqlalchemy-with-and-and-or%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I found how to do it.The point of my question is to chain AND with OR.



    First, create the filters:



    conditions = 


    for the and statement is very straight forward:



    conditions.append(table_name.c.column_name_1.op('~*')(value_1))
    conditions.append(table_name.c.column_name_2.op('~*')(value_2))
    conditions.append(table_name.c.column_name_3.op('~*')(value_3))

    query = session.query(table_name).filter(and_(*conditions)).order_by(table_name.c.column_name_1)


    Which is translated to:



    SELECT   *   FROM     table_name WHERE (column_name_1 == value_1 AND column_name_2 == value_2  AND column_name_3 == value_3)


    Now, to chain conditions with OR and AND:



    We use two different variables: one for the AND statement (exactly like above) and another other one like the next one:



    filter_or_1.append(table_name.c.column_name_4.op('~*')(value_4))# First possible value for the OR statement
    filter_or_1.append(table_name.c.column_name_4.op('~*')(value_5))# Second possible value for the OR statement
    filter_or_1.append(table_name.c.column_name_4.op('~*')(value_6))# Third possible value for the OR statement


    and the query will be:



    query = session.query(table_name).filter(and_(*conditions) , and_(or_(*filter_or_1))).order_by(table_name.c.column_name_1)


    Which is translated to:



    SELECT   *   FROM     table_name WHERE (column_name_1 == value_1 AND column_name_2 == value_2  AND column_name_3 == value_3 AND(column_name_4==value_4 OR column_name_4==value_5 column_name_4==value_6 ))





    share|improve this answer




























      0














      I found how to do it.The point of my question is to chain AND with OR.



      First, create the filters:



      conditions = 


      for the and statement is very straight forward:



      conditions.append(table_name.c.column_name_1.op('~*')(value_1))
      conditions.append(table_name.c.column_name_2.op('~*')(value_2))
      conditions.append(table_name.c.column_name_3.op('~*')(value_3))

      query = session.query(table_name).filter(and_(*conditions)).order_by(table_name.c.column_name_1)


      Which is translated to:



      SELECT   *   FROM     table_name WHERE (column_name_1 == value_1 AND column_name_2 == value_2  AND column_name_3 == value_3)


      Now, to chain conditions with OR and AND:



      We use two different variables: one for the AND statement (exactly like above) and another other one like the next one:



      filter_or_1.append(table_name.c.column_name_4.op('~*')(value_4))# First possible value for the OR statement
      filter_or_1.append(table_name.c.column_name_4.op('~*')(value_5))# Second possible value for the OR statement
      filter_or_1.append(table_name.c.column_name_4.op('~*')(value_6))# Third possible value for the OR statement


      and the query will be:



      query = session.query(table_name).filter(and_(*conditions) , and_(or_(*filter_or_1))).order_by(table_name.c.column_name_1)


      Which is translated to:



      SELECT   *   FROM     table_name WHERE (column_name_1 == value_1 AND column_name_2 == value_2  AND column_name_3 == value_3 AND(column_name_4==value_4 OR column_name_4==value_5 column_name_4==value_6 ))





      share|improve this answer


























        0












        0








        0







        I found how to do it.The point of my question is to chain AND with OR.



        First, create the filters:



        conditions = 


        for the and statement is very straight forward:



        conditions.append(table_name.c.column_name_1.op('~*')(value_1))
        conditions.append(table_name.c.column_name_2.op('~*')(value_2))
        conditions.append(table_name.c.column_name_3.op('~*')(value_3))

        query = session.query(table_name).filter(and_(*conditions)).order_by(table_name.c.column_name_1)


        Which is translated to:



        SELECT   *   FROM     table_name WHERE (column_name_1 == value_1 AND column_name_2 == value_2  AND column_name_3 == value_3)


        Now, to chain conditions with OR and AND:



        We use two different variables: one for the AND statement (exactly like above) and another other one like the next one:



        filter_or_1.append(table_name.c.column_name_4.op('~*')(value_4))# First possible value for the OR statement
        filter_or_1.append(table_name.c.column_name_4.op('~*')(value_5))# Second possible value for the OR statement
        filter_or_1.append(table_name.c.column_name_4.op('~*')(value_6))# Third possible value for the OR statement


        and the query will be:



        query = session.query(table_name).filter(and_(*conditions) , and_(or_(*filter_or_1))).order_by(table_name.c.column_name_1)


        Which is translated to:



        SELECT   *   FROM     table_name WHERE (column_name_1 == value_1 AND column_name_2 == value_2  AND column_name_3 == value_3 AND(column_name_4==value_4 OR column_name_4==value_5 column_name_4==value_6 ))





        share|improve this answer













        I found how to do it.The point of my question is to chain AND with OR.



        First, create the filters:



        conditions = 


        for the and statement is very straight forward:



        conditions.append(table_name.c.column_name_1.op('~*')(value_1))
        conditions.append(table_name.c.column_name_2.op('~*')(value_2))
        conditions.append(table_name.c.column_name_3.op('~*')(value_3))

        query = session.query(table_name).filter(and_(*conditions)).order_by(table_name.c.column_name_1)


        Which is translated to:



        SELECT   *   FROM     table_name WHERE (column_name_1 == value_1 AND column_name_2 == value_2  AND column_name_3 == value_3)


        Now, to chain conditions with OR and AND:



        We use two different variables: one for the AND statement (exactly like above) and another other one like the next one:



        filter_or_1.append(table_name.c.column_name_4.op('~*')(value_4))# First possible value for the OR statement
        filter_or_1.append(table_name.c.column_name_4.op('~*')(value_5))# Second possible value for the OR statement
        filter_or_1.append(table_name.c.column_name_4.op('~*')(value_6))# Third possible value for the OR statement


        and the query will be:



        query = session.query(table_name).filter(and_(*conditions) , and_(or_(*filter_or_1))).order_by(table_name.c.column_name_1)


        Which is translated to:



        SELECT   *   FROM     table_name WHERE (column_name_1 == value_1 AND column_name_2 == value_2  AND column_name_3 == value_3 AND(column_name_4==value_4 OR column_name_4==value_5 column_name_4==value_6 ))






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 26 '18 at 13:40









        Borja Perez de GuzmanBorja Perez de Guzman

        12




        12
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377213%2fquery-with-sqlalchemy-with-and-and-or%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini