mysql query for selecting based on diverging conditions












0















I have a table with the following columnns



id status created_at
1 close ..
2 close ..
3 open ..


Now, i am trying to fetch all records that are newer than 3 months like this



select * from table where created_at >= NOW() - interval 3 month 


But i also want records that are open and older than 3 months, how to add that condition in the same sql?










share|improve this question























  • Please provide a relevant and minimal sample data showcasing your requirements, and expected output. Please go through this link once: Why should I provide an MCVE for what seems to me to be a very simple SQL query?

    – Madhur Bhaiya
    Nov 22 '18 at 7:23
















0















I have a table with the following columnns



id status created_at
1 close ..
2 close ..
3 open ..


Now, i am trying to fetch all records that are newer than 3 months like this



select * from table where created_at >= NOW() - interval 3 month 


But i also want records that are open and older than 3 months, how to add that condition in the same sql?










share|improve this question























  • Please provide a relevant and minimal sample data showcasing your requirements, and expected output. Please go through this link once: Why should I provide an MCVE for what seems to me to be a very simple SQL query?

    – Madhur Bhaiya
    Nov 22 '18 at 7:23














0












0








0








I have a table with the following columnns



id status created_at
1 close ..
2 close ..
3 open ..


Now, i am trying to fetch all records that are newer than 3 months like this



select * from table where created_at >= NOW() - interval 3 month 


But i also want records that are open and older than 3 months, how to add that condition in the same sql?










share|improve this question














I have a table with the following columnns



id status created_at
1 close ..
2 close ..
3 open ..


Now, i am trying to fetch all records that are newer than 3 months like this



select * from table where created_at >= NOW() - interval 3 month 


But i also want records that are open and older than 3 months, how to add that condition in the same sql?







mysql sql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 7:11









BhargavBhargav

167120




167120













  • Please provide a relevant and minimal sample data showcasing your requirements, and expected output. Please go through this link once: Why should I provide an MCVE for what seems to me to be a very simple SQL query?

    – Madhur Bhaiya
    Nov 22 '18 at 7:23



















  • Please provide a relevant and minimal sample data showcasing your requirements, and expected output. Please go through this link once: Why should I provide an MCVE for what seems to me to be a very simple SQL query?

    – Madhur Bhaiya
    Nov 22 '18 at 7:23

















Please provide a relevant and minimal sample data showcasing your requirements, and expected output. Please go through this link once: Why should I provide an MCVE for what seems to me to be a very simple SQL query?

– Madhur Bhaiya
Nov 22 '18 at 7:23





Please provide a relevant and minimal sample data showcasing your requirements, and expected output. Please go through this link once: Why should I provide an MCVE for what seems to me to be a very simple SQL query?

– Madhur Bhaiya
Nov 22 '18 at 7:23












4 Answers
4






active

oldest

votes


















1














Just add that condition to your where clause. Note that you don't need to include the "older than 3 months" part to the condition as anything newer than 3 months is picked up by the first part of the where clause anyway so the OR status = 'open' will only find additional records older than 3 months.



select * 
from table
where columnX = 'some ID' AND
(created_at >= NOW() - interval 3 month OR status = 'open')





share|improve this answer


























  • Hi sorry, my bad i didnt ask the full question, the challenge here was i have multiple necessary where conditions before this date comparison too, like columnnX= some id and these 2 conditions, how to use them in unison?

    – Bhargav
    Nov 22 '18 at 7:18











  • @Bhargav see my edit, I think that's what you mean?

    – Nick
    Nov 22 '18 at 7:19



















0














You can try using OR



select * from table where created_at >= NOW() - interval 3 month 
or (status='open' and created_at < NOW() - interval 3 month)





share|improve this answer































    0














    use OR



    select * from table where created_at >= NOW() - interval 3 month or status='open'





    share|improve this answer































      0














      Select * from Table where add_date(date_column, interval 3 month)> now() or status = "open";





      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%2f53425590%2fmysql-query-for-selecting-based-on-diverging-conditions%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        Just add that condition to your where clause. Note that you don't need to include the "older than 3 months" part to the condition as anything newer than 3 months is picked up by the first part of the where clause anyway so the OR status = 'open' will only find additional records older than 3 months.



        select * 
        from table
        where columnX = 'some ID' AND
        (created_at >= NOW() - interval 3 month OR status = 'open')





        share|improve this answer


























        • Hi sorry, my bad i didnt ask the full question, the challenge here was i have multiple necessary where conditions before this date comparison too, like columnnX= some id and these 2 conditions, how to use them in unison?

          – Bhargav
          Nov 22 '18 at 7:18











        • @Bhargav see my edit, I think that's what you mean?

          – Nick
          Nov 22 '18 at 7:19
















        1














        Just add that condition to your where clause. Note that you don't need to include the "older than 3 months" part to the condition as anything newer than 3 months is picked up by the first part of the where clause anyway so the OR status = 'open' will only find additional records older than 3 months.



        select * 
        from table
        where columnX = 'some ID' AND
        (created_at >= NOW() - interval 3 month OR status = 'open')





        share|improve this answer


























        • Hi sorry, my bad i didnt ask the full question, the challenge here was i have multiple necessary where conditions before this date comparison too, like columnnX= some id and these 2 conditions, how to use them in unison?

          – Bhargav
          Nov 22 '18 at 7:18











        • @Bhargav see my edit, I think that's what you mean?

          – Nick
          Nov 22 '18 at 7:19














        1












        1








        1







        Just add that condition to your where clause. Note that you don't need to include the "older than 3 months" part to the condition as anything newer than 3 months is picked up by the first part of the where clause anyway so the OR status = 'open' will only find additional records older than 3 months.



        select * 
        from table
        where columnX = 'some ID' AND
        (created_at >= NOW() - interval 3 month OR status = 'open')





        share|improve this answer















        Just add that condition to your where clause. Note that you don't need to include the "older than 3 months" part to the condition as anything newer than 3 months is picked up by the first part of the where clause anyway so the OR status = 'open' will only find additional records older than 3 months.



        select * 
        from table
        where columnX = 'some ID' AND
        (created_at >= NOW() - interval 3 month OR status = 'open')






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 7:19

























        answered Nov 22 '18 at 7:12









        NickNick

        32.8k121942




        32.8k121942













        • Hi sorry, my bad i didnt ask the full question, the challenge here was i have multiple necessary where conditions before this date comparison too, like columnnX= some id and these 2 conditions, how to use them in unison?

          – Bhargav
          Nov 22 '18 at 7:18











        • @Bhargav see my edit, I think that's what you mean?

          – Nick
          Nov 22 '18 at 7:19



















        • Hi sorry, my bad i didnt ask the full question, the challenge here was i have multiple necessary where conditions before this date comparison too, like columnnX= some id and these 2 conditions, how to use them in unison?

          – Bhargav
          Nov 22 '18 at 7:18











        • @Bhargav see my edit, I think that's what you mean?

          – Nick
          Nov 22 '18 at 7:19

















        Hi sorry, my bad i didnt ask the full question, the challenge here was i have multiple necessary where conditions before this date comparison too, like columnnX= some id and these 2 conditions, how to use them in unison?

        – Bhargav
        Nov 22 '18 at 7:18





        Hi sorry, my bad i didnt ask the full question, the challenge here was i have multiple necessary where conditions before this date comparison too, like columnnX= some id and these 2 conditions, how to use them in unison?

        – Bhargav
        Nov 22 '18 at 7:18













        @Bhargav see my edit, I think that's what you mean?

        – Nick
        Nov 22 '18 at 7:19





        @Bhargav see my edit, I think that's what you mean?

        – Nick
        Nov 22 '18 at 7:19













        0














        You can try using OR



        select * from table where created_at >= NOW() - interval 3 month 
        or (status='open' and created_at < NOW() - interval 3 month)





        share|improve this answer




























          0














          You can try using OR



          select * from table where created_at >= NOW() - interval 3 month 
          or (status='open' and created_at < NOW() - interval 3 month)





          share|improve this answer


























            0












            0








            0







            You can try using OR



            select * from table where created_at >= NOW() - interval 3 month 
            or (status='open' and created_at < NOW() - interval 3 month)





            share|improve this answer













            You can try using OR



            select * from table where created_at >= NOW() - interval 3 month 
            or (status='open' and created_at < NOW() - interval 3 month)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 '18 at 7:12









            fa06fa06

            14.8k2917




            14.8k2917























                0














                use OR



                select * from table where created_at >= NOW() - interval 3 month or status='open'





                share|improve this answer




























                  0














                  use OR



                  select * from table where created_at >= NOW() - interval 3 month or status='open'





                  share|improve this answer


























                    0












                    0








                    0







                    use OR



                    select * from table where created_at >= NOW() - interval 3 month or status='open'





                    share|improve this answer













                    use OR



                    select * from table where created_at >= NOW() - interval 3 month or status='open'






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 22 '18 at 7:13









                    Zaynul Abadin TuhinZaynul Abadin Tuhin

                    14.8k21032




                    14.8k21032























                        0














                        Select * from Table where add_date(date_column, interval 3 month)> now() or status = "open";





                        share|improve this answer




























                          0














                          Select * from Table where add_date(date_column, interval 3 month)> now() or status = "open";





                          share|improve this answer


























                            0












                            0








                            0







                            Select * from Table where add_date(date_column, interval 3 month)> now() or status = "open";





                            share|improve this answer













                            Select * from Table where add_date(date_column, interval 3 month)> now() or status = "open";






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 22 '18 at 7:21









                            petrchpetrch

                            32626




                            32626






























                                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%2f53425590%2fmysql-query-for-selecting-based-on-diverging-conditions%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







                                Popular posts from this blog

                                MongoDB - Not Authorized To Execute Command

                                Npm cannot find a required file even through it is in the searched directory

                                in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith