How do I get the first Sunday between 2 dates?
i have only 2 fields in myTable (dt_from and dt_to)
dt_from = '2016-08-6';
dt_to = '2016-08-19';
SELECT firstsunday FROM myTable BETWEEN dt_from AND dt_to;
How about getting the first Sunday just after from date itself?
SELECT DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) from myTable as A;
http://sqlfiddle.com/#!9/7fce5
If Sunday isn't between the dates:
SELECT DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) as firstsunday from myTable as A where DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) between A.date_from and A.date_to;
http://sqlfiddle.com/#!9/83d0f0/4
A little bit shorter:
SELECT firstsunday from (SELECT A.date_from, A.date_to, DATE_ADD(A.date_from, INTERVAL (6 - WEEKDAY(A.date_from)) DAY) as firstsunday from myTable as A) as B where B.firstsunday between B.date_from and B.date_to;
http://sqlfiddle.com/#!9/6adab3/1
Try this:
SELECT dt, DAYNAME(dt) as day
FROM myTable WHERE dt BETWEEN date1 AND date2
ORDER BY dt
WHERE day = "Sunday"
LIMIT 0,1
assume you only need to sue the db to get the date range so you dont want any king of query.
<?php
echo date('m/d/Y', strtotime('next Sunday', strtotime('2016-08-6')));
you can add some validation to make sure its before your end date
Related
I want to show records for previous month only, excluding this month's dates.For example, today is February 5th and I want to show records for January 1st to 31st
i have a table- tbl_order_details where I need to fetch all order records by current month and previous month respectively. the column name for date type is orderDate this is what I an doing for fetching rows for current month till date:
SELECT COUNT(1)
FROM tbl_order_details
where merchantCode= '$user_code'
AND MONTH(orderDate) = MONTH(CURRENT_DATE())
AND YEAR(orderDate) = YEAR(CURRENT_DATE())
But I cant figure out how do I show records for january that does not include any records from February
SELECT * FROM tbl_order_details
WHERE YEAR(orderDate) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(orderDate) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
For writing a PHP code, you can get the Month and Year from PHP itself by using the strtotime function depending upon the input your table takes and then formatting it in your sql query. For eg.:
<?php
$month = date("M", strtotime("previous month"));
$year = date("Y", strtotime("this year"));
$query_get = 'SELECT COUNT(1) FROM tbl_order_details where merchantCode= {$user_code} AND MONTH(orderDate) = {$month} AND YEAR(orderDate) = {$year}'
?>
And further pass $query_get to your DB query to fetch the required result. Or else, you can straight push the following query as #Rohit suggested above.
<?php
$query_get = 'SELECT * FROM tbl_order_details WHERE YEAR(orderDate) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND MONTH(orderDate) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)'
?>
Avoid using DATE (), MONTH (), DAY (), YEAR (), SUBSTR (), LEFT (), RIGHT (), LIKE when mentioning columns in WHERE or JOIN'S because you no longer use the indexes that exist in the columns mentioned. Ex: WHERE YEAR(orderDate) = ... Avoid doing this for the reasons stated above.
I suggest use as follows ...
If your "orderDate" column is of type date, do as follows:
SELECT COUNT(1)
FROM tbl_order_details
where merchantCode= '$user_code'
AND orderDate BETWEEN DATE_ADD(LAST_DAY(NOW() - INTERVAL 2 MONTH), INTERVAL 1 DAY)
AND LAST_DAY(DATE_ADD(LAST_DAY(NOW() - INTERVAL 2 MONTH), INTERVAL 1 DAY));
Will return the first day of the previous month
DATE_ADD(LAST_DAY(NOW() - INTERVAL 2 MONTH), INTERVAL 1 DAY)
Returns the last day of the previous month
LAST_DAY(DATE_ADD(LAST_DAY(NOW() - INTERVAL 2 MONTH), INTERVAL 1 DAY))
I am querying records from the last calendar month. As it is February, it should return all the records that were added on January this year.
My Query:
`SELECT * FROM table_name WHERE date >=
DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1
DAY) AND date <= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1
MONTH)), INTERVAL 0 DAY) AND campaign = '$campaign' ORDER BY date
ASC`
It returns some records but skips the first 9 days. It starts showing records from 10th of the previous month. What am I missing here?
check your date field type and make sure you have not mistaken it with varchar.
SELECT * FROM table_name WHERE
(MONTH(date) = (MONTH(NOW()) - 1) AND YEAR(date) = YEAR(NOW()))
OR
(MONTH(date) = 12 AND MONTH(NOW())=1 AND YEAR(date) = (YEAR(NOW()) - 1) AND campaign = '$campaign' ORDER BY date
ASC`
Try To Get Data in step by step like,
First, you should try below query.
SELECT Date, DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1 DAY) AS StartDate, DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1 MONTH)), INTERVAL 0 DAY) AS EndDate FROM MyTable
Second, If First Step give right date then get your data by directly writing your date rather than DATE_ADD function in where clause.
Third, If These will Give you write DATA then try to fetch data using DATE_ADD function.
Replay If you will get solution.
SELECT * FROM table_name WHERE date between DATE_SUB(DATE_SUB(CURRENT_DATE,INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY),INTERVAL 1 MONTH) AND DATE_SUB(DATE_SUB(CURRENT_DATE,INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY),INTERVAL 1 DAY) AND campaign = '$campaign' ORDER BY date ASC
I have a question with mysql.
I have a table like records and records have a field registered_date which contains the dates. I want to select last n months but not before the current date. I must select the last date that registered_date contains.
registered_date
2015-05-30
2015-05-29
2015-05-28
2015-05-27
...
and we are in july. The last date is 2015-05-30. I want to select last 3 months before the 2015-05-30.
I tried to like these:
.... where registered_date > DATE_SUB(now(), INTERVAL 6 MONTH)
and
registered_Date between now() - interval 30 day and now()
Thank you.
You need to find the biggest date and then select three months before that:
select t.*
from table t cross join
(select max(registered_date) as maxrd from table t) m
where t.registered_date >= maxrd - interval 3 month;
Did you try like this
SELECT * FROM table WHERE registered_date >= now()-interval 3 month;
I have 2 tables:
profile userid, fname,lname
schedule: userid, date, start_time, end_time
Here is my query .
select *, group_CONCAT(distinct(date),'_',start_time,'_',end_time)
as dateformat FROM profile, schedule WHERE profile.userid =
schedule.pid
This returns all dates from the table. But what I am trying to do is return only certain days like:
date >= DATE(NOW()) and date <= date(now() + 2 day)
So today tomorrow and the day after.
No matter what I try it just returns all dates in the schedule database. I need this to return 2 rows userid 5 has 1 row in profile but 5 rows in schedule and userid 6 has 1 row in profile and 2 rows in schedule the query works fine its just returning all dates and not the dates within the
"date >= DATE(NOW()) and date <= date(now() + 2 day)"
did you try GROUP BY?
like WHERE profile.userid = schedule.pid GROUP BY date
select profile.userid,schedule.userid, group_CONCAT(distinct(date),'_',start_time,'_',end_time) as dateformat FROM profile, schedule WHERE profile.userid = schedule.userid and date >= DATE(NOW()) and date <= date(now() + interval 2 day);
That did it for now!! Now I can use the dateformat variable in php to parse out my results.
kanishka panamaldeniya - thanks for your suggestion!
use PHP and MySQL and want to use SELECT statement which date_post(datetime variable) start at the last date of last month and to date the first day of next month, help me please.
Thank you in advance.
my database: 'id', 'content', 'image', 'date_post',
etc. and I try to use
$today = getdate();
$thisyear=$today['year'];
$thismon=$today['mon'];
$date_start=$thisyear.'-'.$thismon.'-01';
$date_end=$thisyear.'-'.($thismon+1).'-01';
$sql="SELECT *, DATE_FORMAT(date_post, '%d-%m-%Y') AS datepost
FROM my_table
WHERE date_post BETWEEN date('$date_start')
AND date('$date_end')
ORDER BY date_post DESC";
It makes with one query in MySQL, without any PHP:
SELECT * FROM `table_name`
WHERE DATE(`date_post`) >= DATE_FORMAT(CURDATE() - INTERVAL 1 MONTH, CONCAT('%Y-%m-', DAY(LAST_DAY(CURDATE() - INTERVAL 1 MONTH))))
AND DATE(`date_post`) <= DATE_FORMAT(CURDATE() + INTERVAL 1 MONTH, '%Y-%m-01');
Ensuring that the query will not scan the full table but will use the index of date_post (if there is one!):
SELECT *
FROM myTable
WHERE date_post < LAST_DAY(CURDATE())
+ INTERVAL 2 DAY
AND date_Post >= LAST_DAY( LAST_DAY( CURDATE()) - INTERVAL 1 MONTH )
If it is run today ( 2011-07-01 ), it will give all datetimes between 2011-06-31 00:00:00 and 2011-08-01 23:59:59.