I want to display results that are within the current month
example
Select * FROM table_name WHERE date="THIS MONTH"...
What can I replace THIS MONTH with to get the results for the current month
Use a date function to extract the month and compare to the number for the month you are trying to match against:-
Select * FROM table_name WHERE MONTH(date)= [0 - 11];
You may need to match against the right year too:-
Select * FROM table_name WHERE MONTH(date) = month(current_timestamp) and year(date) = year(current_timestamp);
Try this query:-
Select * FROM table_name WHERE DATE_FORMAT(date_field,"%m")= DATE_FORMAT(NOW(),"%m");
Try this query
select * from tableName
where month(dateField) = month(now())
and year(dateField)= year(now());
Related
Which is better for performance when looking for timestamps in current month and year?
SELECT *
FROM mytable
WHERE YEAR(CURRENT_TIMESTAMP) = YEAR(mytable.timestamp)
AND MONTH(CURRENT_TIMESTAMP) = MONTH(mytable.timestamp)
OR
SELECT *
FROM mytable
WHERE DATE_FORMAT(CURRENT_TIMESTAMP,'%m-%Y') = DATE_FORMAT(mytable.timestamp,'%m-%Y')
For better perfomance and able to use index in mytable.timestamp, truncate the current date to month.
SELECT DATE_FORMAT(CURRENT_TIMESTAMP, '%Y-%m-01')
This create a constant value and you can index search for it.
And then you can get all the record from this month
SELECT *
FROM mytable
WHERE mytable.timestamp >= DATE_FORMAT(CURRENT_TIMESTAMP, '%Y-%m-01')
how can I found last month of records while from current month.
ex.
12-11-2015
12-12-2015
12-1-2016
12-2-2016
I wrote my query like this:
select * from tbl where MONTH(date) < MONTH(CURDATE());
It shows only one record 12-1-2016. But I want all record except last record 12-2-2016.
I think problem with different years.
Try this:
SELECT *
FROM tbl
WHERE (MONTH(date) < MONTH(CURDATE()) AND YEAR(date) = YEAR(CURDATE()))
OR
YEAR(date) < YEAR(CURDATE())
The predicates used in the above query essentially say: if both dates are on the same year, then you can compare month, otherwise you have to only compare the year value.
select * from tbl where MONTH(date) < MONTH(CURDATE()) OR YEAR(date) < YEAR(CURDATE());
My code:
$results = $GLOBALS['wpdb']->get_results( 'SELECT * FROM myTable WHERE date = 2014 ORDER BY id DESC', object );
The problem is date is stored in this format: 2014-01-01
So how do I select just the year ( I don't care about month and day for the time being ).
Thanks
Use the year() function:
WHERE year(date) = 2014
or use explicit comparisons:
WHERE (date >= '2014-01-01' and date < '2015-01-01')
The latter is better because it can make use of an index on the date column.
Try this Query :
SELECT * FROM myTable WHERE year(`date`)='2014' ORDER BY id DESC
Try this:
SELECT * FROM myTable WHERE date >= '2014-01-01 00:00:00' ORDER BY id DESC
To select all rows where the year of a date column (called date_col) is equal to 2014 use the year function:
SELECT * FROM `tbl` WHERE Year(`date_col`) = '2014';
You can select year for get posts with query_posts(); parameter is year. Example: query_posts("year=2014"); This is not full question for you, only alternative..
i have to use below query for get the count value on today.
select *
from orders
where status='P'
AND DATE_FORMAT(FROM_UNIXTIME(date),'%Y-%m-%d')=CURDATE();
it is successfully displayed count value.but get the count value for this month means have to use this below query.
select * from xcart_orders where status='C' AND DATE_FORMAT(FROM_UNIXTIME(date),'%m')=DATE_FORMAT(CURDATE(),'%m');
it is successfully displayed correct count value.
How is get the count value for this week..please help me.how is write the query for this week
You need to remove:
DATE_FORMAT(FROM_UNIXTIME(date),'%Y-%m-%d')= (MONTH(date)
your query should be:
select *
from orders
where status='P' AND
(MONTH(FROM_UNIXTIME(date)) = MONTH(CURDATE()) AND
(YEAR(date) = YEAR(CURDATE());
Just modifying your query a little, this should work:
select * from orders
where status='P'
AND MONTH(date) = MONTH(CURDATE())
AND YEAR(date) = YEAR(CURDATE());
You could rewrite this as:
select * from orders
where status='P'
AND EXTRACT(YEAR_MONTH FROM date) = EXTRACT(YEAR_MONTH FROM NOW())
The following works for me, try it and let me know
SELECT * FROM TABLE_NAME WHERE date_format(FROM_UNIXTIME(date_field), '%u')=date_format(now(), '%u')
I have a table with a column date (type mysql date : yyyy-mm-dd)
I have done this request:
Select Month(MAX(date)) AS last_month, YEAR(MAX(date)) AS last_year FROM mytable
to get the last year and last month.
I want for this last year and last month select all data in one request.
How can I use something like:
Select * from mytable where date like 'last_year-last_month-%'
this in sub-query ??
Is the table being update continuously?
If so, then you can go without the subqueries since they will slow the query down by quite a lot.
This selects everything from the current month.
SELECT * FROM mytable
WHERE MONTH(date) = MONTH(NOW())
AND YEAR(date) = YEAR(NOW())
ORDER BY date ASC
And this one selects everything from the current and last year.
SELECT * FROM mytable
WHERE YEAR(date) > YEAR(NOW())-1
ORDER BY date ASC
This should do the trick (maybe not the best performing solution though):
SELECT * FROM mytable
WHERE MONTH(date) = (SELECT MONTH(MAX(date) FROM mytable))
AND YEAR(date) = (SELECT YEAR(MAX(date) FROM mytable));
EDIT: Corrected brackets.
Thanks for alls,
here is the good request :
SELECT * FROM mytable
WHERE MONTH(date) = (select Month(MAX(date)) FROM mytable)
AND YEAR(date) = (select YEAR(MAX(date)) FROM mytable)
ORDER BY date ASC