I have a table having a column which has values like 2017-08-21
See the screencast here.
Now I need to get the values of current week, current month and current year.
How can I achieve this?
Current week :
SELECT *
FROM your_table
WHERE YEARWEEK(`your_date`) = YEARWEEK(CURDATE())
Current month
SELECT *
FROM your_table
WHERE YEAR(`your_date`) = YEAR(CURDATE())
AND MONTH(`your_date`) = MONTH(CURDATE())
Current year
SELECT *
FROM your_table
WHERE YEAR(`your_date`) = YEAR(CURDATE())
To select all records in current year
Select *
FROM `table_name`
WHERE YEAR('hit_date') = YEAR(CURDATE());
To select all records in current month
Select *
FROM `table_name`
WHERE MONTH('hit_date') = MONTH(CURDATE())
AND YEAR('hit_date') = YEAR(CURDATE());
To select all records in current week
Select *
FROM `table_name`
WHERE YEARWEEK('hit_date') = YEARWEEK(CURDATE());
Please note regarding YEARWEEK you can specify your week start day depend on mode ( second parameter ) please have a look MySQL YEARWEEK() Function
Related
I am trying to get some record from table for specific month and year and I have only complete date field.
In the above image I want to select the data from date but only for (2015-09)
E.g.:
SELECT * FROM attendance WHERE date = (year-month)
You can use year and month function:
SELECT * FROM attendance WHERE year(`date`) = '2015' and month(`date`) = '9'
You can set your date range from the beginning and end of that month.
SELECT *
FROM attendance
WHERE `date` BETWEEN '2015-09-01' AND '2015-09-30'
Try to use DATE_FORMAT.
Your query is:
SELECT * FROM attendance WHERE DATE_FORMAT(date,'%Y-%m') = '2016-05'
I need a help. This code help us when we put specific dates in box.
But if we want records of last 10days then what should we need to do.
Automatic records of last 10 days
$sql = "select * from pub_website where user='$user' && STR_TO_DATE(pdate,'%Y-%m-%d') = '$fromdate' && website='$site'";
To select all records within a certain date range:
SELECT * FROM pub_website WHERE pdate BETWEEN "2015-12-01 00:00:00" AND NOW()
To do this dynamically, so to speak, for the last 10 days:
SELECT * FROM pub_website WHERE pdate BETWEEN DATE_SUB(NOW(), INTERVAL 10 DAY) AND NOW();
I have some code with me:
$sql = "SELECT * FROM palash ORDER BY id DESC LIMIT 31";
I want the LIMIT 31 to be LIMIT END OF THE MONTH
Use the LAST_DAY() function.
Takes a date or datetime value and returns the corresponding value for
the last day of the month. Returns NULL if the argument is invalid.
mysql> SELECT LAST_DAY('2003-02-05');
-> '2003-02-28'
mysql> SELECT LAST_DAY('2004-02-05');
-> '2004-02-29'
mysql> SELECT LAST_DAY('2004-01-01 01:01:01');
-> '2004-01-31'
mysql> SELECT LAST_DAY('2003-03-32');
-> NULL
In your case do this:
SELECT *
FROM palash
WHERE your_date_column <= LAST_DAY(CURDATE())
ORDER BY id DESC
Avoid solutions like OlivierH's (no offense, Olivier). Functions applied on your columns make it impossible for MySQL to use an index.
You can limit to current month with this conditions :
SELECT *
FROM palash
WHERE MONTH(my_date_column) = MONTH(CURDATE())
AND YEAR(my_date_column) = YEAR(CURDATE())
You can then adjust it to get previous dates too.
EDIT / Other way : to stay close to your original idea and to still have a full SQL solution, you can get number of days in current month with LAST_DAY :
DAY(LAST_DAY(my_date_column))
Here is one way you can do it
CREATE TABLE [dbo].[Dates](
[DateColumn] [datetime] NULL
) ON [PRIMARY]
INSERT INTO [dbo].[Dates]
([DateColumn])
VALUES
('2015-01-04 13:00:00'),
('2015-01-06 13:00:00'),
('2015-02-05 14:00:00')
GO
----Last Day of Current Month
Declare #LastDayOfCurrentMonth datetime = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))
SELECT *
FROM Dates
Where DateColumn < #LastDayOfCurrentMonth
Using PHP/MySQL
I'm trying to create a select statement that gets the data from the least day of the current week (I'm using it to show data on a certain player 'this week'). The week starts on Sunday. Sundays's data may not always exist therefore if the Sunday data isn't found then it would use the next earliest day found, Monday, Tuesday, etc.
My date column is named 'theDate' and the datatype is 'DATE'
The query would need to be something like:
SELECT *
FROM table_name
WHERE name = '$username'
AND [...theDate = earliest day of data found for the current week week]
LIMIT 1
It would return a single row of data.
This is a query I tried for getting the 'this week' data, It doesn't seem to work correctly on Sunday's it shows nothing:
SELECT *
FROM table_name
WHERE playerName = '$username'
AND YEARWEEK(theDate) = YEARWEEK(CURRENT_DATE)
ORDER BY theDate;
This is the query that I'm using to get 'this months' data and it works even if the first day of the months data is not found, it will use the earliest date of data found in the current month/year (this query works perfect for me):
SELECT *
FROM table_name
WHERE playerName = '$username'
AND theDate >= CAST( DATE_FORMAT( NOW(),'%Y-%m-01') AS DATE)
ORDER BY theDate
LIMIT 1
Without trying this, you probably need an inner query:
select *
from table_name tn
where tn.the_date =
(select min(the_date)
from table_name
where WEEKOFYEAR(the_date) = WEEKOFYEAR(CURDATE())
and YEAR(the_date) = YEAR(CURDATE()))
viz, give me the row(s) in the table with a date equal to the earliest date in the table in the current week and year.
Try this
SELECT * FROM table_name WHERE name = '$username'
AND your_data IS NOT NULL
AND WEEK(the_date,0 = WEEK(NOW(),0))
ORDER BY DATE_FORMAT(the_date,'%w') ASC
Try the following, replace YOUR_DATE with the date from the column you want (theDate):
SELECT ADDDATE(YOUR_DATE, INTERVAL 1-DAYOFWEEK(YOUR_DATE) DAY)
FirstDay from dual
Did you try:
SELECT ADDDATE(theDate , INTERVAL 1-DAYOFWEEK(theDate ) DAY) FirstDay
FROM table_name
WHERE playerName = '$username'
ORDER BY theDate DESC
LIMIT 1
Use PHP and MySQL. In my table, there is date field (datetime) recorded by NOW() sql function. Example value of data in this field is 2010-10-07 10:57:36. How can I SELECT all data which day-month-year is today. I try to use code as below:
SELECT * FROM table WHERE date=????
Try this:
SELECT * FROM table WHERE date > CURDATE();
CURDATE() will return the current date as 2011-10-07 which will be cast to 2011-10-07 00:00:00 when comparing datetimes to it.
Note that if you use DATE(date) = CURDATE() you will run a date conversion for every row in the table, which will be really bad for your perfomance if you have many rows and/or you need to run the query often. Also make sure you have an index on date, otherwise both methods will be even slower.
SELECT * FROM table where DATE(date)=CURDATE()
SELECT * FROM tableName WHERE DATE(fieldDate) = DATE(NOW());
The date_format function allows you to easily switch between various granularities:
Select everything from the same day:
select * from table
where date_format(date, '%Y-%m-%d') = date_format(now(), '%Y-%m-%d');
From the same month:
select * from table
where date_format(date, '%Y-%m') = date_format(now(), '%Y-%m');
From the same year:
select * from table
where date_format(date, '%Y') = date_format(now(), '%Y');
From the same hour:
select * from table
where date_format(date, '%Y-%m-%d %H') = date_format(now(), '%Y-%m-%d %H');
and so on.
Try this
SELECT * FROM table WHERE DATE(my_date)=DATE(now())
my_date -> column name
SET #day = '2017-12-12' ;
SELECT * FROM table WHERE dateColumn BETWEEN DATE(#day) AND DATE_ADD(DATE(#day), INTERVAL 1 DAY ) ;
use something like this it exactly works on my code(access database):
select * from Table t where t.column>=Date() and t.column< Date() + 1
use between.
select * from table date between '2010-10-06' and '2010-10-08';