MySQL DATE_SUB function not working as expected - mysql

I have following query, to fetch the events .
SELECT
*
FROM
events_table
WHERE event_date > DATE_SUB(NOW(), INTERVAL 1 DAY)
ORDER BY event_status ASC,
event_date ASC
All I want to do is return all rows whose event_date is today
Where I m going wrong?
Thanks for help

How about this in your where clause:
WHERE DATE(event_date) = DATE(NOW())
And another option that wouldn't prevent using an index on event_date:
WHERE event_date BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 1 DAY)

Use CURDATE():
SELECT
*
FROM
events_table
WHERE event_date > CURDATE()
ORDER BY event_status ASC,
event_date ASC
This is assuming that event_date can't be in the future... otherwise you'll want to also use the DATE() function:
WHERE DATE(event_date) = CURDATE()

Related

MySQL COUNT on Tomorrow's Date based on CURDATE

I have a MYSQL COUNT Status that is working great by counting records greater than CURDATE. How do I specify this query to ONLY count tomorrow based on CURDATE for the column specified?
SELECT COUNT(ID) as total_count
FROM HTG_ScheduleRequest
WHERE (ScheduleDateCurrent > CURDATE())
AND JobStatus = '3'
GROUP BY SSR
The "easy" way is:
WHERE date(ScheduleDateCurrent) = date_add(CURDATE(), interval 1 day) AND JobStatus = '3'
The better way is:
WHERE JobStatus = '3' AND
ScheduleDateCurrent >= date_add(CURDATE(), interval 1 day) AND
ScheduleDateCurrent < date_add(CURDATE(), interval 2 day)
The reason this is better is because it can take advantage of an index on JobStatus, ScheduleDateCurrent, if one is available.
The following query would do the trick
SELECT COUNT(ID) as total_count
FROM HTG_ScheduleRequest
WHERE (ScheduleDateCurrent BETWEEN DATE_ADD(DATE(CURDATE()), INTERVAL 1 DAY)
AND DATE_ADD(DATE(CURDATE()), INTERVAL 2 DAY))
AND JobStatus = '3'
GROUP BY SSR
To count only the rows having tomorrow's date (and non-NULL ID), you could do this:
SELECT COUNT(ID) as total_count
FROM HTG_ScheduleRequest
WHERE (ScheduleDateCurrent = ADDDATE(CURDATE(),1))
AND JobStatus = '3'
GROUP BY SSR

Select between dates issues

I have this SQL:
$sql="SELECT *
FROM table
WHERE expiresdate >= Date(Now())
AND expiresdate <= Date_add(Date(Now()), INTERVAL 10 day)
ORDER BY expiresdate ASC";
it should basically show all rows in the database that are going to expire within 10 days time however, lets say the expiredate was 2013-03-06 - this row will not display on any day after the expiredate
does anyone have any ideas?
This should be what you need:
SELECT
*
FROM
`table`
WHERE
expiresdate <= CURDATE() + INTERVAL 10 DAY
ORDER BY
expiresdate ASC

MySQL - Select all UNLESS query

I have a query that I use the works, but now I need to modify it so that it excludes rows if they have a certain data in a field.
Here's the current code:
SELECT oc_ieentry,oc_sysitem,oc_item,oc_itemdesc,oc_purchasedate,oc_url
FROM catalog
WHERE oc_purchasedate >= date_sub(current_date, interval 21 day)
ORDER BY oc_item ASC
What I need to do is add a statement in there that if oc_ieentry LIKE 1, then those rows should not be shown.
Try this:
SELECT
oc_ieentry,oc_sysitem,oc_item,oc_itemdesc,oc_purchasedate,oc_url
FROM catalog
WHERE (oc_purchasedate >= date_sub(current_date, interval 21 day))
AND (oc_ieentry NOT LIKE 1)
ORDER BY oc_item ASC
You could add another condition in the WHERE clause, such as:
SELECT oc_ieentry,oc_sysitem,oc_item,oc_itemdesc,oc_purchasedate,oc_url
FROM catalog
WHERE
oc_purchasedate >= date_sub(current_date, interval 21 day)
AND oc_ieentry != 1
ORDER BY oc_item ASC
Add an extra condition to your WHERE?
...
WHERE oc_purchasedate >= date_sub(current_date, interval 21 day) AND oc_ieentry <> 1
...
You can use the NOT keyword in the WHERE clause:
SELECT * from products WHERE prod_price NOT IN (49, 100, 999);
Does it help you?

SELECT records MySQL: between date + 1

Say I want to SELECT all records between two dates plus one record before and one record after that date? All records are ordered by date.
You could use a union combined with the limit statement. Something like what's below (untested, don't have access to mysql).
(select column from table where datefield > startdate and datefield < stopdate)
union
(select column from table where datefield < startdate order by datefield desc limit 1)
union
(select column from table where datefield > stopdate order by datefield limit 1)
This will give you the next row regardless of where it falls date-wise.
Thanks for syntax fix, ponies.
(select * from t where date < start_date order by date desc limit 1)
union (select * FROM t WHERE date between start_date and end_date)
union (select * from t where date > end_date order by date asc limit 1)
You can use functions to add or subtract values, like this:
select * from table where field1 < ADDDATE( CURTIME() , INTERVAL 1 DAY)
Check this link where there are some examples.
SELECT *
FROM table
WHERE date BETWEEN DATE_ADD(current_date(), INTERAL -1 DAY)
AND DATE_ADD(current_date(), INTERVAL 1 DAY);

Query to get all rows from previous month

I need to select all rows in my database that were created last month.
For example, if the current month is January, then I want to return all rows that were created in December, if the month is February, then I want to return all rows that were created in January. I have a date_created column in my database that lists the date created in this format: 2007-06-05 14:50:17.
SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
Here's another alternative. Assuming you have an indexed DATE or DATETIME type field, this should use the index as the formatted dates will be type converted before the index is used. You should then see a range query rather than an index query when viewed with EXPLAIN.
SELECT
*
FROM
table
WHERE
date_created >= DATE_FORMAT( CURRENT_DATE - INTERVAL 1 MONTH, '%Y/%m/01' )
AND
date_created < DATE_FORMAT( CURRENT_DATE, '%Y/%m/01' )
If there are no future dates ...
SELECT *
FROM table_name
WHERE date_created > (NOW() - INTERVAL 1 MONTH);
Tested.
Alternatively to hobodave's answer
SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
You could achieve the same with EXTRACT, using YEAR_MONTH as unit, thus you wouldn't need the AND, like so:
SELECT * FROM table
WHERE EXTRACT(YEAR_MONTH FROM date_created) = EXTRACT(YEAR_MONTH FROM CURDATE() - INTERVAL
1 MONTH)
SELECT *
FROM yourtable
where DATE_FORMAT(date_created, '%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 1 month),'%Y-%m')
This should return all the records from the previous calendar month, as opposed to the records for the last 30 or 31 days.
Even though the answer for this question has been selected already, however, I believe the simplest query will be
SELECT *
FROM table
WHERE
date_created BETWEEN (CURRENT_DATE() - INTERVAL 1 MONTH) AND CURRENT_DATE();
WHERE created_date >= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1 DAY)
AND created_date <= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1 MONTH)), INTERVAL 0 DAY)
This worked for me (Selects all records created from last month, regardless of the day you run the query this month)
Alternative with single condition
SELECT * FROM table
WHERE YEAR(date_created) * 12 + MONTH(date_created)
= YEAR(CURRENT_DATE) * 12 + MONTH(CURRENT_DATE) - 1
select fields FROM table
WHERE date_created LIKE concat(LEFT(DATE_SUB(NOW(), interval 1 month),7),'%');
this one will be able to take advantage of an index if your date_created is indexed, because it doesn't apply any transformation function to the field value.
Here is the query to get the records of the last month:
SELECT *
FROM `tablename`
WHERE `datefiled`
BETWEEN DATE_SUB( DATE( NOW( ) ) , INTERVAL 1
MONTH )
AND
LAST_DAY( DATE_SUB( DATE( NOW( ) ) , INTERVAL 1
MONTH ) )
Regards
- saqib
if you want to get orders from last month, you can try using
WHERE MONTH(order_date) = MONTH(CURRENT_DATE()) -1
One more way to do this in:
MYSQL
select * from <table_name> where date_created >= DATE_ADD(NOW(), INTERVAL -30 DAY);
SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)