mysql query get today and tommrows date - php

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

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";

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

Calculate the difference b/w two months data

I have a mysql table orders in that I have a column order_date which is current time stamp(2016-08-17 00:00:00.000000). now I want to select or count the data's entered this month and the previous month, after this I can find the difference between these two months I am using this code and it is not working.
$sql="SELECT * FROM order WHERE order_date > DATE_SUB(NOW(), INTERVAL 1 MONTH)";
$result = $this->db->query($sql);
return $result;
this is not working an mysql error is produced.
Try
$sql="SELECT * FROM order WHERE DATE(order_date) LIKE DATE_SUB(CURDATE(), INTERVAL 1 MONTH)";
$result = $this->db->query($sql);
return $result;
i think this Link[http://sqlhints.com/2015/07/10/how-to-get-difference-between-two-dates-in-years-months-and-days-in-sql-server/] will help you
Use this. Hope it helps what you want. Thanks
$todayDate = date('Y-m-d');
$todayMonth = date("m", strtotime($todayDate ));
$previousMonth = $todayMonth - 1;
$sql = "SELECT * FROM order WHERE MONTH(order_date) BETWEEN '$todayMonth' AND '$previousMonth'";
First, the following is the correct logic to get all values from the current month and all of the previous month:
select *
from orders o
where order_date >= date_sub(date_sub(curdate(), interval day(curdate) - 1 day), interval 1 month);
Then, use conditional aggregation for comparison. Here is an easy way:
select sum(month(order_date) = month(curdate())) as cur_month,
sum(month(order_date) <> month(curdate())) as prev_month,
(sum(month(order_date) = month(curdate())) -
sum(month(order_date) <> month(curdate()))
) as diff
from orders o
where order_date >= date_sub(date_sub(curdate(), interval day(curdate) - 1 day), interval 1 month);
Note: I don't fully see the utility of comparing a partial month (this month) to a full month (last month), but that is what you seem to be asking for. If you are asking for something different, then ask another question with sample data and desired results.

MySQL Query results based on month

I really need some help. Not MySQL friendly, muddled through this last few days but now stuck...
Need to take the below query and modify it to pull out only records closed in month of "January" for instance. Is this possible from the below? Cant figure it...
<?php
$recentlyClosedDays = 7;
?>
$query1 = "
SELECT HD_TICKET.ID as ID,
HD_TICKET.TITLE as Title,
HD_STATUS.NAME AS Status,
HD_PRIORITY.NAME AS Priority,
HD_TICKET.CREATED as Created,
HD_TICKET.MODIFIED as Modified,
S.FULL_NAME as Submitter,
O.FULL_NAME as Owner,
HD_TICKET.RESOLUTION as Resolution,
(SELECT COMMENT FROM HD_TICKET_CHANGE WHERE HD_TICKET_ID=HD_TICKET.ID ORDER BY TIMESTAMP DESC LIMIT 1) as Comment,
HD_TICKET.CUSTOM_FIELD_VALUE0 as Type
FROM HD_TICKET
JOIN HD_STATUS ON (HD_STATUS.ID = HD_TICKET.HD_STATUS_ID)
JOIN HD_PRIORITY ON (HD_PRIORITY.ID = HD_TICKET.HD_PRIORITY_ID)
LEFT JOIN USER S ON (S.ID = HD_TICKET.SUBMITTER_ID)
LEFT JOIN USER O ON (O.ID = HD_TICKET.OWNER_ID)
WHERE (HD_TICKET.HD_QUEUE_ID = $mainQueueID)
AND (HD_STATUS.STATE like '%Closed%')
AND (HD_TICKET.TIME_CLOSED >= DATE_SUB(NOW(), INTERVAL $recentlyClosedDays DAY))
ORDER BY HD_TICKET.TIME_CLOSED DESC
";
Any help would be greatly apprecaited and beer will be owed :)
To select DATE, DATETIME, or TIMESTAMP values in the current month, you do this.
WHERE timestampval >= DATE(DATE_FORMAT(NOW(), '%Y-%m-01'))
AND timestampval < DATE(DATE_FORMAT(NOW(), '%Y-%m-01')) + INTERVAL 1 MONTH
For the previous month you can do this:
WHERE timestampval >= DATE(DATE_FORMAT(NOW(), '%Y-%m-01')) - INTERVAL 1 MONTH
AND timestampval < DATE(DATE_FORMAT(NOW(), '%Y-%m-01'))
For the previous year you could do this:
WHERE timestampval >= DATE(DATE_FORMAT(NOW(), '%Y-01-01')) - INTERVAL 1 YEAR
AND timestampval < DATE(DATE_FORMAT(NOW(), '%Y-01-01'))
You can summarize (aggregate) tables by month like this:
SELECT DATE(DATE_FORMAT(timestampval , '%Y-%m-01')) AS month_starting,
SUM(whatever) AS total,
COUNT(whatever) AS transactions
FROM table
GROUP BY DATE(DATE_FORMAT(timestampval , '%Y-%m-01'))
This all works because this expression:
DATE(DATE_FORMAT(sometime, '%Y-%m-01'))
takes an arbitrary sometime value and returns the first day of the month in which the timestamp occurs. Similarly,
DATE(DATE_FORMAT(sometime, '%Y-01-01'))
returns the first day of the year. You can then use date arithmetic like + INTERVAL 1 MONTH to manipulate those first days of months or years.
Here's a more complete writeup on this topic. http://www.plumislandmedia.net/mysql/sql-reporting-time-intervals/

Help with row count

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 !!!

Categories