get the count value for this week - mysql

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')

Related

How to find which year do values tend to increase in ? in SQL

Basically I have a table like this:
Table Time:
ID.......Date
1......08/26/2016
1......08/26/2016
2......05/29/2016
3......06/22/2016
4......08/26/2015
5......05/23/2015
5......05/23/2015
6......08/26/2014
7......04/26/2014
8......08/26/2013
9......03/26/2013
The query should return like this
Year........CountNum
2016........4
2015........3
To find out which year does its value tend to increase in. I notice that I want to display the years that have more values (number of row in this case) than the previous year.
What I've done so far
SELECT Year, count(*) as CountNum
FROM Time
GROUP BY Year
ORDER BY CountNum DESC;
I don't know how to get the year from date format. I tried year(Date) function, but I got Null data.
Please help!
It should works fine.
select year(date), count(*) as countNum
from time
group by year(date)
order by countNum
Join the grouped data to itself with 1 year offset:
select
a.*
from
(
select year(`Date`) as _year, count(*) as _n
from time group by 1
) a
left join
(
select year(`Date`) as _year, count(*) as _n
from time group by 1
) b
on a._year = b._year-1
where a._n > b._n
order by 1

How can I find last month records?

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());

How to select year in sql statement

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 want to display results that are within the current month

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());

Mysql - Subquery with date and like

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