Select data from last month sql - php

I have this code to select from my database data from current month:
SELECT * FROM daily_reports
WHERE username = '$username' and MONTH(date) = MONTH(Now())
order by date DESC
And now i want to display data from last month.
column date is type date

Try this
SELECT * FROM daily_reports
WHERE username = '$username'
and MONTH(date) = MONTH(DATE_ADD(Now(), INTERVAL -1 MONTH)) order by date DESC

This should work:-
SELECT *
FROM daily_reports
WHERE username = '$username'
and MONTH(date) = MONTH(DATE_ADD(Now(), INTERVAL -1 MONTH))
order by date DESC

SELECT
*
FROM
daily_reports
WHERE username = '$username'
AND dateColumn BETWEEN SUBDATE(CURDATE(), INTERVAL 1 MONTH) AND NOW();
ORDER BY DATE DESC

You can do many operations on dates, as shown in the fine manual.
In your case, perhaps the DATE_SUB function would be clearest:
SELECT * FROM daily_reports
WHERE username = '$username' AND MONTH(date) = MONTH(DATE_SUB(Now(), INTERVAL 1 MONTH)
ORDER BY date DESC

date BETWEEN date_format
(NOW() - INTERVAL 1 MONTH, '%Y-%m-01')
AND last_day(NOW() - INTERVAL 1 MONTH)

Related

how can i write as one query to get count in mysql

I want to get the today count of users and yesterday's users count for that i want to write only one query how can i do that..?
these are my queries I want only one query:
SELECT COUNT(*) FROM visitors group by visited_date ORDER by visited_date DESC limit 1,1 as todayCount
SELECT COUNT(*) FROM visitors group by visited_date ORDER by visited_date DESC limit 1,0 as yesterdayCount
My expected results or only 2 columns
todayCount yesterdayCount
2 4
This should do the trick:
SELECT COUNT(CASE
WHEN visited_date = CURDATE() THEN 1
END) AS todayCount ,
COUNT(CASE
WHEN visited_date = CURDATE() - INTERVAL 1 DAY THEN 1
END) AS yesterdayCount
FROM visitors
WHERE visited_date IN (CURDATE(), CURDATE() - INTERVAL 1 DAY)
GROUP BY visited_date
ORDER by visited_date
If you know the current and previous date, then you can do:
SELECT SUM(visited_date = CURDATE()) as today,
SUM(visited_date = CURDATE() - interval 1 day) as yesterday
FROM visitors
WHERE visited_date >= CURDATE() - interval 1 day;
If you don't know the two days, then you can do something similar, getting the latest date in the data:
SELECT SUM(v.visited_date = m.max_vd) as today,
SUM(v.visited_date < m.max_vd) as yesterday
FROM visitors v CROSS JOIN
(SELECT MAX(v2.visited_date) as max_vd FROM visitors v2) as m
WHERE v.visited_date >= m.max_vd - interval 1 day
Just try this simple query
select visited_date as date, COUNT(*) as count from `visitors`
group by `visited_date` order by `visited_date` asc
It will produce output as
It will work for you.
Try this:
$sqlToday = "Select COUNT(*) FROM menjava WHERE DATE(date_submitted)=CURRENT_DATE()";
$sqlYesterday = "Select COUNT(*) FROM menjava WHERE DATE(dc_created) = CURDATE() - INTERVAL 1 DAY";

Time interval query order by date

Here is my query i am retrieving birthday list from database using this query i want to retrieve data in asc order by dob(date of birth) i am using order by dob asc but its giving mysql syntax error.
QUERY
SELECT * FROM members
WHERE DATE_ADD(dob, INTERVAL YEAR(CURDATE())-YEAR(dob) YEAR)
BETWEEN CURDATE()
AND DATE_ADD(CURDATE(), INTERVAL 7 DAY
ORDER BY dob ASC
its working now here is one problem again the dob(date of birth) format is 1990-10-11 when i am using order by dob asc its short the data 1990-10-11 , 1991-10-09, but i want to short this by data not year like 1991-10-09, 1990-10-11
try this you are missing ) in your query
"SELECT * FROM members WHERE DATE_ADD(dob, INTERVAL YEAR(CURDATE())-YEAR(dob) YEAR) BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 7 DAY) order by date(dob) asc"
if you wan to sort by month than use order by Month(dob)
You can also try with selected field something like this
'SELECT field1, field2, DATE_FORMAT(dob, "%d-%M-%Y") AS userdob FROM members WHERE
DATE_ADD(dob, INTERVAL YEAR(CURDATE())-YEAR(dob) YEAR) BETWEEN CURDATE() AND
DATE_ADD(CURDATE(), INTERVAL 7 DAY) order by userdob asc'
You missed a )
...BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 7 DAY order by...

MySQL query - select entries not older than a time period

I have this query
SELECT * FROM content
WHERE topic='$rw09[id]' AND active='1' AND date < DATE_SUB(CURDATE(), INTERVAL 1 WEEK)
ORDER BY cpc DESC, id DESC
LIMIT 4
The key part of my query is date < DATE_SUB(CURDATE(), INTERVAL 1 WEEK). It returns me entries older than a week. What i want it to return me is entries NOT older than 1 week. How can i modify it to return me desired result?
Thank you.
Have you tried with
SELECT * FROM content
WHERE topic='$rw09[id]' AND active='1' AND date > DATE_SUB(CURDATE(), INTERVAL 1 WEEK)
ORDER BY cpc DESC, id DESC
LIMIT 4
?

Specify PHP code to select dates and records 3 days old

I am trying to call data from SQL table that is only 3 days old
My table has a lbs-date column in it and is date format. I have tried the following but get no result from the query at all
$result = mysql_query("SELECT *, DATE_FORMAT(datetime, '%y,%m,%d') FROM lbs_trace_etrack
WHERE lbs_date(datetime) = CURDATE() - INTERVAL 3 DAY
ORDER BY lbs_date DESC")
Is there any other way I can call only the last 3 days of information from the SQL my date format is Y/M/D
SELECT *, DATE_FORMAT(lbs_date, '%y,%m,%d')
FROM lbs_trace_etrack
WHERE lbs_date >= CURDATE() - INTERVAL 3 DAY
ORDER BY lbs_date DESC
check DATE_FORMAT. Its syntax is DATE_FORMAT(<date>,format) . Use like this :
SELECT *, DATE_FORMAT(lbs_date , '%y,%m,%d') FROM lbs_trace_etrack
WHERE lbs_date = CURDATE() - INTERVAL 3 DAY
ORDER BY lbs_date DESC

Returning Rows from MySql less than 60 Days Old

I have the following query returning all rows from my table:
$query="SELECT * FROM $tbl_name ORDER BY job_id DESC";
I'd like to limit those results by entries less than 60 days old. I record the date an entry was made to the database using:
$dt=date('d M Y');
And this is stored in a column called 'date'.
Can someone help me to modify my query?
Thanks
Dan
If date is stored like a varchar in database, your query should be:
SELECT *
FROM $tbl_name
WHERE TO_DATE(date, 'dd MON yyyy') >= DATE_SUB(CURDATE(), INTERVAL 60 DAY)
ORDER BY job_id DESC
if date is stored like a date, use:
SELECT *
FROM $tbl_name
WHERE date >= DATE_SUB(CURDATE(), INTERVAL 60 DAY)
ORDER BY job_id DESC
$query="SELECT * FROM $tbl_name WHERE DATEDIFF(CURDATE(), STR_TO_DATE(date,'%d %M %Y')) <60 ORDER BY job_id DESC"
SELECT * FROM $tbl_name FROM_UNIXTIME(date) >= DATE_SUB(CURDATE(), INTERVAL 60 DAY) ORDER BY job_id DESC
Try this (untested)
SELECT * FROM $tbl_name WHERE date >= DATE_SUB(CURDATE(), INTERVAL 60 DAY) ORDER BY job_id DESC
You can use as
select b.mobile_number, max(b.bill_number), max(b.created_on), c.name from MasterBill as b, MasterCustomer as c WHERE b.created_on < NOW() - INTERVAL 60 DAY and b.mobile_number = c.mobile_number group by mobile_number;
consider mobile number is key binding data with customer master and billing.

Categories