Get data based on a specific year in PHP/MySQL - php

I currently use DATE_SUB to show results in a database. So for data dating back 2 months I will do;
SELECT * FROM table WHERE `date` >= DATE_SUB(CURRENT_DATE, INTERVAL 2 MONTH)
For data dating back 20 days, I will do
SELECT * FROM table WHERE `date` >= DATE_SUB(CURRENT_DATE, INTERVAL 20 DAY)
And for data dating back 2 years, I will do
SELECT * FROM table WHERE `date` >= DATE_SUB(CURRENT_DATE, INTERVAL 2 YEAR)
What I can't figure out is how to get data for a specific year eg if I want data for 2015 only. How do I do that?
NB. date column type is datetime

You could use year()
SELECT * FROM table WHERE year(date) = 2015

Related

Calling the data from the database which is from last 6 month specifically

I know how to call the data within the last 6 month with this SQL code of mine.
SELECT COUNT(*) FROM user WHERE
registered_date > DATE_SUB(now(), INTERVAL 6 MONTH)
The problem is listing out the COUNT (*) data specifically for the month, example:
12 = January
10 = February
3 = March
And so on so forth. I am trying to implement it into a PHP graph.
You should be able to use a combination of the mysql MONTH function and GROUP BY:
SELECT COUNT(*), MONTH(registered_date) FROM user
WHERE registered_date > DATE_SUB(now(), INTERVAL 6 MONTH) GROUP BY MONTH(registered_date)

SQL for getting products created on particular day

I want to get the product created five days before. From start of fifth day to end of fifth day. I have shown my table structure.
What will be SQL query for getting for products created exactly before five days.
I guess you are storing UNIX TIME.
To get the records of 5th day backward from current date:
SELECT
*
FROM your_table
WHERE `timestamp` >= UNIX_TIMESTAMP(CURDATE() - INTERVAL 5 DAY)
AND `timestamp` < UNIX_TIMESTAMP(CURDATE() - INTERVAL 4 DAY)
Use like this,
SELECT * FROM table_name WHERE DATE(timestamp) = '2016-06-08'

Show dates between now and 4 weeks later

How to select dates from mysql table 'Courses' between now and 4 weeks later where Open='1' and sort by Date and Time?
SELECT * FROM Courses
WHERE Open='1' AND Date BETWEEN CURDATE() AND CURDATE() + INTERVAL 4 WEEK
ORDER BY Date, Time
Not tested

PHP MySQL - Select all where expiry date = todays date + 7 days

I am using PHPMyadmin and putting values in a database using PHP. I store the expiry date of products using a timestamp as follows,
FOR EXAMPLE:
2012-11-04
I want to select all where the expiry date is equal to todays date plus 8 days (such as the one above)
I also want to select all where expiry date is equal to todays date + 2 weeks in a seperate page if any one could help me out would be very grateful!
You can do that with a query like this:
SELECT * FROM table WHERE date = DATE_ADD(CURDATE(), INTERVAL 8 DAY)
You can use DATE_SUB for dates in the past.
Select all where the expiry date is equal to todays date plus 8 days
SELECT
*
FROM
products
WHERE
products.expiry_date >= DATE(now())
AND
products.expiry_date <= DATE_ADD(DATE(now()), INTERVAL 8 DAY)
Select all where the expiry date is equal to todays date plus 2 weeks
SELECT
*
FROM
products
WHERE
products.expiry_date >= DATE(now())
AND
products.expiry_date <= DATE_ADD(DATE(now()), INTERVAL 2 WEEK)
These docs will be helpful for you:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add

Filter results by timestamp for the past 24 hours

I am trying to filter the count of Rows found, per hour, over the past 24 hours.
I am using the following SQL query and 'landtime' is a DateTime field.
SELECT HOUR(landtime) as hour, COUNT(*) as total FROM ord_log WHERE landtime > DATE_SUB(NOW(), INTERVAL 24 HOUR) GROUP BY HOUR(landtime) ORDER BY HOUR(landtime)
Sample data set > http://pastebin.com/0rYBnePG
The results looked as expected, until I looked at the data it was using.
It was pulling dates from days/weeks before todays date when I only want a count for the past 24 hours.
Any help appreciated!
SELECT * FROM table WHERE day(data) = EXTRACT(day FROM (NOW() - INTERVAL 1 day))
No need to use a function, just use simple math:
SELECT HOUR(landtime) as hour, COUNT(*) as total FROM ord_log WHERE landtime > (CURRENT_TIMESTAMP - INTERVAL 1 DAY) GROUP BY HOUR(landtime) ORDER BY HOUR(landtime)

Categories