Help with row count - php

I need a little help with row count. i manage to add today and total members count (rows). i want to count this week and this month. can anyone point me out how to do it? thanks.
$result = mysql_query("SELECT * FROM members");
$num_rows = mysql_num_rows($result);
echo "$num_rows Members\n";
$utoday = date("j. n. Y");
$today = mysql_query("SELECT * FROM mambers WHERE date='$utoday' ");
$num_today = mysql_num_rows($today);
echo "$num_today Members\n";

If you stored the date as a type date, you can use the mysql built-in time functions.
For example, you can group by MONTH(date).

If you want to count for this week starting from the most recent Monday:
SELECT COUNT(1) WeekCount
FROM members A,
(
SELECT
(MondayDate + INTERVAL 0 SECOND) PastMonday,
((MondayDate + INTERVAL 7 DAY) + INTERVAL 0 SECOND) NextMonday
FROM
(SELECT DATE(NOW() - INTERVAL WEEKDAY(NOW()) DAY) MondayDate) AA
) B
WHERE date >= PastMonday AND date < NextMonday
;
If you want to count for this month starting from the 1st query this:
SELECT COUNT(1) MonthCount
FROM members A,
(
SELECT FirstOfThisMonth,
((FirstOfThisMonth + INTERVAL 32 DAY) - INTERVAL (DAY(FirstOfThisMonth + INTERVAL 32 DAY)-1) DAY) FirstOfNextMonth
FROM
(
SELECT (DATE(NOW() - INTERVAL (DAY(NOW())-1) DAY) + INTERVAL 0 SECOND) FirstOfThisMonth
) AA
) B
WHERE date >= FirstOfThisMonth AND date < FirstOfNextMonth
;
Give it a Try !!!

Related

MYSQL Last month Query is not returning first 9 days

I am querying records from the last calendar month. As it is February, it should return all the records that were added on January this year.
My Query:
`SELECT * FROM table_name WHERE date >=
DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1
DAY) AND date <= DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1
MONTH)), INTERVAL 0 DAY) AND campaign = '$campaign' ORDER BY date
ASC`
It returns some records but skips the first 9 days. It starts showing records from 10th of the previous month. What am I missing here?
check your date field type and make sure you have not mistaken it with varchar.
SELECT * FROM table_name WHERE
(MONTH(date) = (MONTH(NOW()) - 1) AND YEAR(date) = YEAR(NOW()))
OR
(MONTH(date) = 12 AND MONTH(NOW())=1 AND YEAR(date) = (YEAR(NOW()) - 1) AND campaign = '$campaign' ORDER BY date
ASC`
Try To Get Data in step by step like,
First, you should try below query.
SELECT Date, DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1 DAY) AS StartDate, DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 1 MONTH)), INTERVAL 0 DAY) AS EndDate FROM MyTable
Second, If First Step give right date then get your data by directly writing your date rather than DATE_ADD function in where clause.
Third, If These will Give you write DATA then try to fetch data using DATE_ADD function.
Replay If you will get solution.
SELECT * FROM table_name WHERE date between DATE_SUB(DATE_SUB(CURRENT_DATE,INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY),INTERVAL 1 MONTH) AND DATE_SUB(DATE_SUB(CURRENT_DATE,INTERVAL DAYOFMONTH(CURRENT_DATE)-1 DAY),INTERVAL 1 DAY) AND campaign = '$campaign' ORDER BY date ASC

Optimize MYSQL, PHP search for records at this exact time a number of days ago

I'm trying to get an optimized query that gives me the results at this same time on any previous day, or for a range of days. I was able to solve it with a loop on PHP repeating the query that gives me the result for an specific day but this takes a really long time.
My PHP code and the MYSQL query:
$json_data = array();
$i=$range;
while ($i>0){
$result=mysql_query("SELECT numpeople, numviews, date FROM table_stats ORDER BY ABS(date - DATE_SUB(NOW(), INTERVAL '$i' DAY)) LIMIT 1", $conn);
while($r = mysql_fetch_assoc($result)){
$json_data[]= $r;
}
$i--;
}
print json_encode($json_data);
return;
In a subselect for each day in the interval I would calculate the minimum value of your expression and join it back in the outer query to your stats table using the minimum value. The only catch is that if you have multiple records with the minimum difference, then all of them will be returned.
select numpeople, numviews, date
FROM table_stats
inner join
(select date(date) dd, min(ABS(date - DATE_SUB(NOW(), INTERVAL (datediff(curdate(), date)) DAY))) as mindiff
from table_stats
where date(date)<=curdate() - 1 and date(date)>=curdate() - interval $range day
group by date(date)) t
on t.dd=date(table_stats.date)
and t.mindiff=ABS(table_stats.date - DATE_SUB(NOW(), INTERVAL (datediff(curdate(), table_stats.date)) DAY))
In the abs() expression you can use concat(date(date), ' ', time(now())) instead of the date subtraction to get the same time on a previous day.
As an alternate version:
SELECT numpeople, numviews, `date` FROM table_stats
WHERE `date` < NOW() - INTERVAL $n DAY -- where $n is the max range
AND TIME(`date`) BETWEEN TIME(NOW()) - INTERVAL 5 MINUTE AND TIME(NOW()) + INTERVAL 5 MINUTE
ORDER BY date DESC
LIMIT 1
Finds all the records with an entry at +/- 5 minutes from the current time, up to $n days ago.

mysql query get today and tommrows date

SELECT
doctors. fullname,
dutyroster.date,
dutyroster.time
FROM
dutyroster
INNER JOIN doctors ON doctors.docid = dutyroster.docid
WHERE doctors.docid = $doc_id AND
dutyroster.date = DATE(NOW()) AND DATE(NOW())+ INTERVAL 1 DAY
ORDER BY dutyroster.`date` ASC";
this query is used to find specific doctors information from a table called dutyroster. i want to get the docs shedule information for current day and tommrow only.. but this doesnt work.
and i made a second one which is also not working since it returns current one and all the next dates also
SELECT
doctors. fullname,
dutyroster.date,
dutyroster.time
FROM
dutyroster
INNER JOIN doctors ON doctors.docid = dutyroster.docid
WHERE doctors.docid = $doc_id AND
DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= dutyroster.date
ORDER BY dutyroster.`date` ASC"
Instead of
... AND dutyroster.date = DATE(NOW()) AND DATE(NOW())+ INTERVAL 1 DAY
try
... AND (dutyroster.date = CURDATE() OR
dutyroster.date = CURDATE() + INTERVAL 1 DAY))
or in more concise way, as #MarcM suggested
... AND dutyroster.date IN (CURDATE(), CURDATE() + INTERVAL 1 day)
From your first attempt it almost looks like you are trying to program COBOL!
Also, for future reference "this doesn't work" is not a helpful comment. You should say what actually happens.
Anyway, try changing your where clause to either:
WHERE doctors.docid = $doc_id AND
(dutyroster.date = CURRENT_DATE OR dutyroster.date = CURRENT_DATE + INTERVAL 1 DAY)
or:
WHERE doctors.docid = $doc_id AND
dutyroster.date IN (CURRENT_DATE, CURRENT_DATE + INTERVAL 1 DAY))

query to select data from DB based on a particular day and count how many rows are there?

I have Db which has data of an year...so I need a query to select data from based on a date(particularly yesterday's) and count how many are in this.....
Help please!
It has a column with name "created" where all the date & time are present.
code used:
//$thirty_reg = mysql_query("SELECT column FROM user WHERE user.date BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()");
//$num_thirty=mysql_num_rows($thirty_reg);
//echo $num_thirty;
select *
from YourTable
where date_sub(curdate(), interval 1 day) < DateColumn
and DateColumn < curdate()
To fetch result set:
SELECT * FROM table WHERE data_date = DATE_SUB(NOW(), INTERVAL 1 DAY)
To fetch count of rows:
SELECT COUNT(*) FROM table WHERE data_date = DATE_SUB(NOW(), INTERVAL 1 DAY)

HOw to SELECT * (all) which date start from the last day of last month to the first day of next month

use PHP and MySQL and want to use SELECT statement which date_post(datetime variable) start at the last date of last month and to date the first day of next month, help me please.
Thank you in advance.
my database: 'id', 'content', 'image', 'date_post',
etc. and I try to use
$today = getdate();
$thisyear=$today['year'];
$thismon=$today['mon'];
$date_start=$thisyear.'-'.$thismon.'-01';
$date_end=$thisyear.'-'.($thismon+1).'-01';
$sql="SELECT *, DATE_FORMAT(date_post, '%d-%m-%Y') AS datepost
FROM my_table
WHERE date_post BETWEEN date('$date_start')
AND date('$date_end')
ORDER BY date_post DESC";
It makes with one query in MySQL, without any PHP:
SELECT * FROM `table_name`
WHERE DATE(`date_post`) >= DATE_FORMAT(CURDATE() - INTERVAL 1 MONTH, CONCAT('%Y-%m-', DAY(LAST_DAY(CURDATE() - INTERVAL 1 MONTH))))
AND DATE(`date_post`) <= DATE_FORMAT(CURDATE() + INTERVAL 1 MONTH, '%Y-%m-01');
Ensuring that the query will not scan the full table but will use the index of date_post (if there is one!):
SELECT *
FROM myTable
WHERE date_post < LAST_DAY(CURDATE())
+ INTERVAL 2 DAY
AND date_Post >= LAST_DAY( LAST_DAY( CURDATE()) - INTERVAL 1 MONTH )
If it is run today ( 2011-07-01 ), it will give all datetimes between 2011-06-31 00:00:00 and 2011-08-01 23:59:59.

Categories