I have two date column in a table, one for date of question posted and another for answer. For example whenever a new question is asked its date is stored in q_date column and whenever some answer that question its date is stored in a_date column.
Now I want all the question asked within one week and all the questions which are answered within one week.
Can anyone explain me a query by using join or subquery
You can try the following query:
SELECT *
FROM yourTable
WHERE
q_date >= DATE(NOW()) - INTERVAL 7 DAY OR
a_date >= DATE(NOW()) - INTERVAL 7 DAY
This will return all record corresponding to questions or answers happening with the last 7 days.
You can use a query like this:
Select * from q_ans where DATEDIFF(WEEK,q_ans.q_date,q_ans.a_date) = 1
This will give all records for which answer date is in one week range of question date
You need to add AND condition for BETWEEN clause. You can try
WHERE q_ans.a_date BETWEEN date_sub( NOW(), INTERVAL 1 WEEK) AND NOW()
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am trying to get total view from the previous week and the week before last. I'll refer to these as WeekMinusOne and WeekMinusTwo respectively.
so for the present time, I would want views from the last FULL previous week i.e. 12/23 - 12/29 (I'd like to start my weeks on a Monday) and views from the WeekMinus two, i.e. 12.16-12.22.
While I know I can hard code this to the exact dates, I'd like it to autoupdate to reflect the new data. How can I do this?
format of the table i'm pulling from is simple.
just two columns,
date | # of unique views!
simplified look at my table
desired result
To start with, consider the following expression:
current_date - interval (weekday(current_date)) day
It gives you the first day of the current week if your weeks start on Monday. As explained in this SO answer, it your weeks start on Sunday, you need:
current_date - interval (dayofweek(current_date) - 1) day
Starting from there, we can filter the table on the 2 previous weeks and do conditional sums:
select
sum(
case when view_date >= w.dt - interval 7 day then views else 0 end
) views_from_week_minus_one,
sum(
case when view_date < w.dt - interval 7 day then views else 0 end
) views_from_week_minus_two
from
views_table v
inner join (select current_date - interval (weekday(current_date)) day dt) w
on v.view_date >= w.dt - interval 14 day and and view_date < w.dt
An important thing is that, since it uses no date function on the date column, this query will benefit from an index on views_table(view_date). Another advantage of the half-open interval strategy is that it would properly handle the time part of the date if any.
Try next solution:
SELECT SUM(views) as total_views, WEEK(date, 1) AS week_num
FROM your_table
WHERE WEEK(CURDATE(), 1) - WEEK(date, 1) < 2
GROUP BY week_num;
The query will return views summery grouped by week number
I am trying to get record which is not more than 7 days of posting .
CreatedDate > DATE_SUB(CURDATE(), INTERVAL 7 DAY)
is it correct query to get record ,
Please come to know that CreatedDate is column of date and time
Thanks
If I get the question you are not asking, the problem is that CreatedDate is a DateTime field, and you either want a Date answer or you are worried that DATE_SUB and CURDATE return a Date value.
If it is the latter, fear not, as the result will be in DateTime format.
If it is the former, you can cast the result to Date in your query.
Check this example.
This question already has answers here:
MySQL Select Date Equal to Today (having datetime as the data type)
(5 answers)
Closed 7 years ago.
I have a SQL database that has a column "A" that has timestamps in it.
How do I return all values that have a timestamp that doesnt fall under the current day. So say for example its Monday, i want to return all values that are not this Monday.
Use DATE() or CURDATE() MySQL function:-
Return all values that are current date :-
select * from table where DATE(emailsend)=CURDATE()
Return all values that are not current date :-
select * from table where DATE(emailsend) !=CURDATE()
Or use MySQL WEEKDAY() returns the index of the day in a week for a
given date (0 for Monday, 1 for Tuesday and ......6 for Sunday)
select * from table where WEEKDAY(emailsend) !=0
//Return all values where day not is monday
The simpliest way I see to do this is to ignore the time and compare only dates with date() function.
select * from table where date(A) != date(now())
Building on Rahautos answer, you can do
select * from table where WEEKDAY(emailsend) !=WEEKDAY(now());
Then it should select all the weekdays except for current weekday. Not tested, but it sounds right to me.
i have two dates in the same week i want to know which is the highest date among those two dates
for example 3/24/2014 and 3/27/2014 are the two dates in same week i want to know which is the bigger date among these two i.e 3/27/2014
what function should i use to get this result.
i tried with some of the question and answers in this link it did not work out
MySQL Query to select data from last week?
Might be you want this
SELECT max(date) FROM table_name
WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
I know this has prob been answered, but I've searched for almost an hour now and Im not finding my answer.
Here is my sql query.
SELECT *
FROM (`calendar_event`)
WHERE DATE_FORMAT(`start_time`, '%m/%d/%Y')
BETWEEN CURDATE() + INTERVAL 5 DAY AND CURDATE()
Here is the format of the start_time column 06/30/2011 8:30 AM
The query isn't having any errors, Im just not getting any results...
BETWEEN a AND b needs b to be greater than a, otherwise the interval is empty.
Try inverting the two date parameters you're building.
Try this:
WHERE date_col BETWEEN 'date1' and 'date2'