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

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

Related

Select count from a table with a Where on another table

I have two tables
oc2_visits (fields: id_ad)
oc2_ads (fields: published)
I need to count the visits by grouping the id_ad, and that works, but I need also that the first 9 results have been published not more that 25 days ago.
This is the query I have at the moment:
SELECT count(v.id_ad) AS visits,
v.id_ad,
a.published
FROM oc2_visits AS v,
oc2_ads AS a
WHERE DATE(a.published) >= DATE_SUB(NOW(), INTERVAL 25 DAY)
GROUP BY v.id_ad
ORDER BY visits DESC LIMIT 0,9
but when I try to enter the query in phpmyadmin, it crashes.
What am I doing wrong?
I think you forgot to parse DATE_SUB(NOW(), INTERVAL 25 DAY) into DATE while comparing with a DATE. Hope it'll work.
SELECT count(v.id_ad) AS visits,
v.id_ad,
a.published
FROM oc2_visits AS v,
oc2_ads AS a
WHERE DATE(a.published) >= DATE(DATE_SUB(NOW(), INTERVAL 25 DAY))
GROUP BY v.id_ad
ORDER BY visits DESC LIMIT 0,9
Presumably the root of your problem is the cartesian product between two tables. Simple rule: Never use comma in the from clause. Always use explicit join syntax.
I imagine the query you want looks something like this:
SELECT count(v.id_ad) AS visits, v.id_ad, a.published
FROM oc2_visits v join
oc2_ads a
ON v.id_ad = a.id_ad
WHERE DATE(a.published) >= DATE_SUB(NOW(), INTERVAL 25 DAY)
GROUP BY v.id_ad
ORDER BY visits DESC
LIMIT 0, 9;

Adjust mysql query to filter out today from the date

Hi all i currently have this sql:
SELECT a.*
FROM (SELECT a.*
FROM articles a
WHERE date >= UNIX_TIMESTAMP(DATE(NOW() - INTERVAL 7 DAY)) AND a.active = 1
ORDER BY views ASC
) a
ORDER BY views ASC
It lists all articles posted in the last week, what I want to do is adjust it so it ignored today, is that easy to do?
Certainly. You just need to add AND date < UNIX_TIMESTAMP(CURDATE())
For simplicity, you can use the BETWEEN operator:
WHERE `date` BETWEEN UNIX_TIMESTAMP(DATE(NOW() - INTERVAL 7 DAY))
AND UNIX_TIMESTAMP(DATE(NOW() - INTERVAL 1 DAY))
I believe this allows the engine to make better use of indexes than individual >= and <= calls, but I'm not certain on that.
Shouldn't
SELECT `a`.*
FROM `articles` AS a
WHERE `date` >= UNIX_TIMESTAMP(NOW(TODAY() - INTERVAL 7 DAY)) AND `date` <= UNIXTIMESTAMP(DATE(NOW() - INTERVAL 1 DAY)) `a`.`active` = 1
ORDER BY `views` ASC
Suffice for this task?

MySQL Query Date Errors

I have this query that indexes my images and orders them by popularity but I cant make the user to choose the interval cause there's something wrong with the query:
switch($Data['data']){
case 'daily':$QueryDate='=CURDATE()';break;
case 'weekly':$QueryDate=' BETWEEN SUBDATE(CURDATE(), INTERVAL 7 DAYS) AND NOW()';break;
case 'monthly':$QueryDate='>CURDATE() - INTERVAL 31 DAYS';break;
default: Core::redirect('image/browse/daily/1');break;
}
$IMGDB = new Database('images');
$query = "SELECT *, (derived.`likes` * 2 + derived.`views`) as `popularity` from
(SELECT *,
(SELECT COUNT(*) FROM `likes` WHERE `like`=I.id AND `date`".$QueryDate.") AS `likes`,
(SELECT SUM(`views`) FROM `views` WHERE `id`=I.id AND `date`".$QueryDate.") AS `views`
FROM images AS I
) AS derived
where 1 ORDER BY `popularity` DESC ";
Only the daily case works.
Here is the error:
SQL Error (1064): You have an error in your SQL syntax;..... to use near 'DAYS) AND NOW()) AS likes, (SELECT SUM(views) FROM views WHERE id= I.id A
The correct syntax for specifying an interval of days uses the DAY keyword. You've used DAYS in:
BETWEEN SUBDATE(CURDATE(), INTERVAL 7 DAYS) AND NOW()
and:
> CURDATE() - INTERVAL 31 DAYS

Select sql from database only from yesterday

How can I select my sql database only from yesterday? Here is my code:
SELECT *, COUNT(visitors.usr_id) as usr_count FROM user, visitors WHERE visitors.usr_id = $usr_id GROUP BY $usr_id ORDER BY usr_count LIMIT 1
My database name is timein and the way it inputs the date and time that the INSERT was done. Here is the code:
Database column : timein
Database insert looks like : 2012-9-6 9:11:35
Basically I want to be able to SELECT and COUNT only from yesterday. How can I SELECT COUNT from my sql database from yesterday only?
Your query does not make sense. You are doing a cross join between user and visitors and then filtering only on visitors. I suspect you want something like:
SELECT *, COUNT(visitors.usr_id) as usr_count
FROM user join
visitors
on visitors.usr_id = user.usr_id
WHERE user.usr_id = $usr_id
GROUP BY $usr_id
ORDER BY usr_count
LIMIT 1;
The where clause to get yesterday's data would be:
WHERE date(timein) = date(NOW() - INTERVAL 1 DAY)
Or, if you have an index on timein:
WHERE timein >= date(NOW() - INTERVAL 1 DAY) and timein < date(NOW())
Try this one add a condition for yesterday like timein <= NOW() - INTERVAL 1 DAY , timein >= NOW() - INTERVAL 2 DAY;
SELECT *, COUNT(visitors.usr_id) as usr_count
FROM user, visitors
WHERE visitors.usr_id = $usr_id
AND timein <= NOW() - INTERVAL 1 DAY
AND timein >= NOW() - INTERVAL 2 DAY
GROUP BY $usr_id
ORDER BY usr_count
LIMIT 1
Or you can use BETWEEN
SELECT *, COUNT(visitors.usr_id) as usr_count
FROM user, visitors
WHERE visitors.usr_id = $usr_id
AND timein BETWEEN NOW() - INTERVAL 1 DAY AND NOW() - INTERVAL 2 DAY;
GROUP BY $usr_id
ORDER BY usr_count
LIMIT 1

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

Categories