count total rows date wise from last 7 days in PHP - php

I want to count the registered user from database for last 7 days includes date of today and I am mentioning the structure is given below for understanding :-
reg_users
+----+---------------------+------+
| id | added-date | name |
+----+---------------------+------+
| 1 | 2020-06-01 00:02:40 | john |
+----+---------------------+------+
| 2 | 2020-06-01 00:02:41 | sue |
+----+---------------------+------+
| 3 | 2020-06-03 00:02:42 | fran |
+----+---------------------+------+
| 4 | 2020-06-04 00:02:40 | mark |
+----+---------------------+------+
| 5 | 2020-06-05 00:02:41 | tim |
+----+---------------------+------+
now suppose How I count the total registered use date wise from last 7 days.. where I am considering today is 2020-06-07 [dd-mm-yyy] and I want get result in array like [2, 0, 1, 1, 1, 0, 0] here 2 because 2 user registered on 2020-06-01 then on 2020-06-02 no user resisted so 0.
Please help me..

I think something like this is what you're after?
select count(*) as total, DATE_FORMAT(your_date, '%y%m%d') as date_yyyymmdd from your_table group by date_yyyymmdd;
It will return you a result like
+-------+----------------+
| total | date_yyyymmdd |
+-------+----------------+
| 1 | 20200727 |
| 0 | 20200726 |
| 3 | 20200725 |
+-------+----------------+

Related

mysql. I want to get the max value of all rows with a specific date and use the corresponding fields

My table 'players_online'
id | count | date | time | day | max-players
0 | 55 | 26/05/16 | 13:00 | 5 | 300
1 | 33 | 26/05/16 | 13:10 | 5 | 300
2 | 43 | 26/05/16 | 13:20 | 5 | 300
3 | 100 | 27/05/16 | 13:00 | 6 | 300
4 | 43 | 27/05/16 | 13:10 | 6 | 300
5 | 56 | 27/05/16 | 13:20 | 6 | 300
desired output (todays highest count)
id | count | date | time | day | max-players
3 | 100 | 27/05/16 | 13:00 | 6 | 300
Also
(yesterdays highest count)
id | count | date | time | day | max-players
0 | 55 | 26/05/16 | 13:00 | 5 | 300
And
(Higest total count)
id | count | date | time | day | max-players
3 | 100 | 27/05/16 | 13:00 | 6 | 300
My knowledge of mysql has gotten rusty and I have no idea how to get it right. previously I tried the following which didn't select the right time.
SELECT MAX(count) AS maxcount, date, time FROM players_online WHERE date='".date('d-m-y')."'
I'm building a little stats page where you can view the highest player count of a minecraft server today (until now), yesterday and the highest player count of all time with their corresponding date, time, day and max-players.
Hope somebody can give me a hand.
You can just do a simple query that orders by count and is limited to 1.
SELECT * FROM players_online WHERE date = '27/05/2016' ORDER BY count DESC LIMIT 1
That should get you the highest count.

SQL SELECT row with highest value where problem_id is the same

I'm working on a project where I have to list the solutions with the highest votes per problem.
Every problem has two solutions and the users can vote on one solution per problem. This is my database at the moment.
+----------+------------+---------+
| id | id_problem | vote |
+----------+------------+---------+
| 1 | 1 | 25 |
| 2 | 1 | 10 |
| 3 | 2 | 18 |
| 4 | 2 | 2 |
| 5 | 3 | 6 |
| 6 | 3 | 7 |
| 7 | 4 | 11 |
| 8 | 4 | 4 |
| 9 | 5 | 5 |
| 10 | 5 | 2 |
+----------+------------+---------+
I would like to get this result:
(The row with the highest vote per id_problem)
+----------+------------+---------+
| id | id_problem | vote |
+----------+------------+---------+
| 1 | 1 | 25 |
| 3 | 2 | 18 |
| 6 | 3 | 7 |
| 7 | 4 | 11 |
| 9 | 5 | 5 |
+----------+------------+---------+
SELECT
id,
id_problem,
max(vote)
from
tablename
group by
id_problem
order by
id_problem ASC
The max(vote) determines greater vote, but it aggregate the result, then you need to group by id_problem and then order it asc.
You can use group by clause with max aggregation function to get the expected result, e.g.:
select id, id_problem, max(vote) as vote
from result
group by id_problem
order by id_problem
Here's SQL Fiddle.

calculating age based on date and matching against race results for sorting

I'm trying to add a new page onto an old site, a records page that'd show which players won the most money or won the race when they were a certain age
The user table looks like this
***********************************
| id | name | age | bday |
| 1 | bob | 15 | 2000-07-30 |
| 2 | john | 14 | 2001-07-30 |
| 3 | mary | 13 | 2002-07-30 |
***********************************
the race_results table looks like this
************************************************************
| id | raceid | userid | place | winnings | date |
| 1 | 1 | 1 | 1 | 1000 | 2006-04-10 |
| 2 | 1 | 2 | 5 | 50 | 2005-02-15 |
| 3 | 1 | 3 | 6 | 50 | 2010-06-12 |
| 4 | 2 | 1 | 1 | 1000 | 2009-05-29 |
| 5 | 2 | 2 | 3 | 250 | 2003-01-12 |
************************************************************
What's the most practical approach to a query that'd calculate the year range when Bob was 3 years old and match that with the race results table to see how many times he won 1st place within that particular date range?
SELECT COUNT(*)
FROM 'race_results'
INNER JOIN 'user' on user.id = race_results.userid
WHERE user.name = 'bob'
AND race_results.place = 1
AND race_results.date >= ADDDATE(user.bday, INTERVAL 3 YEAR)
AND race_results.date < ADDDATE(user.bday, INTERVAL 4 YEAR);
To get a list of all 3 yr old 1st placers and how many times they won, not just for 'bob' ...
SELECT user.name, COUNT(*)
FROM 'race_results'
INNER JOIN 'user' on user.id = race_results.userid
WHERE race_results.place = 1
AND race_results.date >= ADDDATE(user.bday, INTERVAL 3 YEAR)
AND race_results.date < ADDDATE(user.bday, INTERVAL 4 YEAR)
GROUP BY user.name;

GROUP BY with active record?

I have a table (url_analyst) with the following info:
id | user_id | url_id | date | visits
----------------------------------------------------
1 | 2 | 5 | 2015-10-25 | 15
2 | 2 | 6 | 2015-10-25 | 18
3 | 2 | 5 | 2015-10-25 | 11
4 | 3 | 8 | 2015-10-25 | 22
5 | 3 | 9 | 2015-10-27 | 30
6 | 5 | 15 | 2015-10-25 | 15
etc
What I want is a update/insert earning in a new table "earning"
earning calculate 1$/10 visits
id | user_id | total_urls | date | total_visits | earning
-------------------------------------------------------------------
1 | 2 | 3 | 2015-10-25 | 44 | 4.4$
2 | 3 | 1 | 2015-10-25 | 22 | 2.2$
3 | 3 | 1 | 2015-10-27 | 30 | 3.0$
4 | 5 | 1 | 2015-10-25 | 15 | 1.5$
How can I do this with CodeIgniter using the active record query method? Can this be done with COUNT Total URL then GROUP BY user_id AND Date , update earning?
Try this MySQL query:
SELECT
user_id, count(url_id), date, sum(visits) as
total_visits, concat(sum(visits)/10,'$') as earning
FROM
url_analyst
GROUP BY
user_id, date
Considering all group_by columns from url_analyst table,
$this->db->select("SUM(total_url)");//if required select other fields
$this->db->group_by("url_analyst.user_id");
$this->db->group_by("url_analyst.date");
If you want group_by on any column from earning, you can change the table name in group_by clause

Cumulate values for each user

I have a MySQL table with the following structure:
| id | date | type | user | approved |
======================================================
| 1 | 2015-01-30 20:32:01 | 2 | 1 | 0 |
| 2 | 2015-01-31 19:40:12 | 1 | 2 | 1 |
| 3 | 2015-02-01 11:12:08 | 2 | 1 | 0 |
| 4 | 2015-02-01 11:32:13 | 4 | 3 | 1 |
| 5 | 2015-02-01 17:25:22 | 6 | 2 | 1 |
I now would like to cumulate the times a task was approved (1) for each user. This is what the result should be:
user 1 --> 0
user 2 --> 2
user 3 --> 1
Is there a way to accomplish this by a single SQL query or would I need to do this outside of MySQL in my php script?
you can just group and summarize values:
select user, sum(approved)
from table
group by user

Categories