mysql query for selecting based on diverging conditions
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
add a comment |
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
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
add a comment |
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
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
mysql sql
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
add a comment |
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
add a comment |
4 Answers
4
active
oldest
votes
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')
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
add a comment |
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)
add a comment |
use OR
select * from table where created_at >= NOW() - interval 3 month or status='open'
add a comment |
Select * from Table where add_date(date_column, interval 3 month)> now() or status = "open";
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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')
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
add a comment |
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')
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
add a comment |
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')
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')
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
add a comment |
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
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
answered Nov 22 '18 at 7:12
fa06fa06
14.8k2917
14.8k2917
add a comment |
add a comment |
use OR
select * from table where created_at >= NOW() - interval 3 month or status='open'
add a comment |
use OR
select * from table where created_at >= NOW() - interval 3 month or status='open'
add a comment |
use OR
select * from table where created_at >= NOW() - interval 3 month or status='open'
use OR
select * from table where created_at >= NOW() - interval 3 month or status='open'
answered Nov 22 '18 at 7:13
Zaynul Abadin TuhinZaynul Abadin Tuhin
14.8k21032
14.8k21032
add a comment |
add a comment |
Select * from Table where add_date(date_column, interval 3 month)> now() or status = "open";
add a comment |
Select * from Table where add_date(date_column, interval 3 month)> now() or status = "open";
add a comment |
Select * from Table where add_date(date_column, interval 3 month)> now() or status = "open";
Select * from Table where add_date(date_column, interval 3 month)> now() or status = "open";
answered Nov 22 '18 at 7:21


petrchpetrch
32626
32626
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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