Effectively Firing a multiple query - php

I was practicing drawing graphs on various statistics for the purpose of data analysis. But I am not able to figure out an efficient way to fire multiple mysql query at the back-end.
I am trying to draw Period Vs No of Visitors graph.
Please Note: Period here refers to week,month,3 months,6 months,1 year,2 years.
Period will be selected by the user from the select box.
For example: When User selects 3 week, I need to construct No of Visitors per 3 week graph.
My DataBase Contains Two Column: For each of the site hit, it records:
(1) timestamp and
(2)user ID.
If I fire query multiple times for each select option, then performance would be quite poor.So, How to do it efficiently?
UPD:
When User Select stats per 3 month:
Then I am firing mysql query as:
Select count(*) From stats_tab WHERE timestamp BETWEEN JAN AND MAR;
Select count(*) From stats_tab WHERE timestamp BETWEEN APR AND JUN;
Select count(*) From stats_tab WHERE timestamp BETWEEN JUL AND SEP;
............
Each count returned from each of the query will be the y-axis value for my graph
When User Select stats per year:
Then I am firing mysql query as:
Select count(*) From stats_tab WHERE timestamp BETWEEN 2009 AND 2010;
Select count(*) From stats_tab WHERE timestamp BETWEEN 2010 AND 2011;
Select count(*) From stats_tab WHERE timestamp BETWEEN 2011 AND 2012;
............

Don't hit database with multiple queries. Get all your values with one query appropriately applying GROUP BY and WHERE
SELECT YEAR(timestamp) year, COUNT(*) total
FROM stats_tab
WHERE timestamp BETWEEN NOW() - INTERVAL 3 YEAR AND NOW()
GROUP BY YEAR(timestamp);
SELECT MONTH(timestamp) month, COUNT(*) total
FROM stats_tab
WHERE timestamp BETWEEN NOW() - INTERVAL 6 MONTH AND NOW()
GROUP BY MONTH(timestamp);
SELECT DAY(timestamp) day, COUNT(*) total
FROM stats_tab
WHERE timestamp BETWEEN NOW() - INTERVAL 7 DAY AND NOW()
GROUP BY DAY(timestamp);
Sample output:
| YEAR | TOTAL |
----------------
| 2011 | 2 |
| 2012 | 1 |
| 2013 | 9 |
| MONTH | TOTAL |
-----------------
| 2 | 1 |
| 3 | 2 |
| 5 | 1 |
| DAY | TOTAL |
---------------
| 20 | 1 |
| 21 | 1 |
| 22 | 1 |
Here is SQLFiddle demo

Related

query for time in mysql between hours

how to get a list of rows that is between 17:30 till tomarrow 8:30.
--------------------------------------
| id | user_id | action | time |
--------------------------------------
| 1 | 25 | enter | 1512459905
| 2 | 19 | exit | 1512125105
| 3 | 31 | enter | 1514581905 |
--------------------------------------
mysql table have a time column with unix timestamp and i want get a list every day that between 17:30 till 8:30
SELECT TIME_TO_SEC(TIMEDIFF(FROM_UNIXTIME('time 1'), FROM_UNIXTIME('time 2')) AS 'time_diff_in_sec' FROM 'your_table';
This can help:
Mysql Get Time Diff
$dateStart=strtotime(date("Y-m-d 17:30:00"));
$dateEnd=strtotime(date("Y-m-d 20:30:00"));
This is how you can convert your date into unix timestamp in php.
Then in your query:
SELECT * FROM my_table WHERE time BETWEEN '$dateStart' AND '$dateEnd'
If you don't want to execute your script manually every day you can set up a cronjob to do it for you.
You can use the TIME() function to extract the time part of the datatime,
So you can do something like this:
select * FROM table t
where TIME(f.time) between '17:30:00' AND '18:30:00'

Taking a specific data with MySQL, Group by

I want to take most frequently entered datas in last 1 day.
Here is my code:
SELECT tag_id, time,
COUNT(tag_id) AS value_occurrence
FROM tag_rel
GROUP BY tag_id
HAVING time > '$yesterday'
ORDER BY value_occurrence DESC LIMIT 10
Code is working but it has a problem:
I wanna take between july 22 and 23.
id| tag_id | time |
1 | football | 22,5 july |
2 | basketball | 22,5 july |
3 | football | 22,5 july |
4 | football | 21 july |
I want to take first three rows and order them by their frequency.
ex:
1- football
2- basketball
But my code is not taking "football". It just shows "basketball". Because additionally "football" has a time value which is smaller than 22 july.
How can solve it?
The correct query for what you want would use where, not having:
SELECT tag_id, MAX(time) as maxtime,
COUNT(tag_id) AS value_occurrence
FROM tag_rel
WHERE time > '$yesterday'
GROUP BY tag_id
ORDER BY value_occurrence DESC
LIMIT 10;
Otherwise, there is an arbitrary value used for time, both in the select and the having clause.

age range for date of birth

Good day guys. i have here my code its in mysql format eventhough i know it's depreciated, so i'm only focusing on how to get total number of my data in database via age range.
my table structure for outputting
age range |Male |Female | Total
0-4 | | |
5-9 | | |
so on.. | | |
total | | |
my code
//for 0-4 age range
SELECT count(*) as total FROM data WHERE gender='male' and dob BETWEEN CURDATE() - INTERVAL 4 YEAR AND CURDATE() - INTERVAL 0 YEAR";
//for 5-9 age range
SELECT count(*) as total FROM data WHERE gender='male' and dob BETWEEN CURDATE() - INTERVAL 9 YEAR AND CURDATE() - INTERVAL 5 YEAR";
i manage to get all the total in every age range of both male and female but when i count it manually it is not exact from the total row below. Please
help me out of this problem, im just a newbie in php and mysql so i'm going to learn more . thanks a lot :)

Counting daily and hourly results in mySQL

I have two tables in mySQL, one for views and one for downloads with the following key structure:
ip | hit
---------------------------------------
127.0.0.1 | 2015-02-08 15:16:59
127.0.0.1 | 2015-02-08 15:20:22
127.0.0.1 | 2015-02-08 15:20:35
127.0.0.1 | 2015-02-08 15:21:13
10.0.1.1 | 2015-02-09 16:29:13
10.0.1.1 | 2015-02-09 16:42:12
10.0.1.1 | 2015-02-09 16:52:30
10.0.1.1 | 2015-02-10 10:52:30
10.0.1.1 | 2015-02-10 10:52:30
10.0.1.1 | 2015-02-10 12:52:30
I need to return a query that will count how many rows there are every day, ideally it would also return empty days/hours but I can fill those gaps in with PHP easily if the SQL query would be overly complex.
time | count
---------------------------------------
2015-02-08 | 4
2015-02-09 | 3
2015-02-10 | 3
I also need to get the same information on a hourly basis
time | count
---------------------------------------
2015-02-08 15:00:00 | 4
2015-02-09 16:00:00 | 3
2015-02-10 10:00:00 | 2
2015-02-10 10:00:00 | 1
So far i have the following query after cobbling together a few I found online into something that actually works, but it only returns the day of the month as being unique and I need to have the query be stretchable over multiple months so only knowing the day is not massively helpful.
select dayofmonth(hit) as Day_of_month, count(*) as records from table_name hit >= '2015-02-01 00:00:00' and hit < '2015-03-01 00:00:00' group by dayofmonth(hit)
I've seen a fair few of these similar posts around but the only ones I have found use functions not present in mySQL it seems, any help would be appreciated.
Edit
After much more fiddling I now have it returning counts for each date with the following which i can easily enough limit by wheres, however I'm still none the wiser to getting this to work on an hourly basis.
SELECT date(hit) AS The_Date, count( * ) AS count FROM table_name GROUP BY The_Date ORDER BY The_Date ASC
The easiest way to do that could be using date_format function
To get all records grouped by day
select date_format(date, '%Y-%m-%d') time, count(*) from test group by time order by time;
To get all records grouped by hour
select date_format(date, '%Y-%m-%d %H:00:00') time, count(*) from test group by date_format(date, '%Y-%m-%d %H') order by time;
Here is sqlfiddle for your example
Try something like this:
SELECT DATE(hit) AS Day, HOUR(TIME(hit)) AS Hour, COUNT(*) as Count
FROM table_name
GROUP BY Day, Hour
(edited)

Getting records between two dates for a certain amount of time

Say I have a table which looks like the following:
id | name | date
1 | test1 | 2013-05-12 00:00:01
2 | test2 | 2013-05-13 00:00:01
3 | test3 | 2013-05-14 00:00:01
4 | test4 | 2013-05-15 00:00:01
5 | test5 | 2013-05-15 00:00:02
An example of what I am looking to do would be to go back through the records for the past 3 days and then I want to count how many records there are on each individual say.
So, for the 15th it would return 2, 14th 1, etc.
I know I can do the following to get a count between 2 dates (this would be for the 15th):
SELECT COUNT(id) as recordCount FROM exampletable WHERE date >= STR_TO_DATE('130515', '%y%m%d') - INTERVAL 1 DAY AND date < STR_TO_DATE('130515', '%y%m%d')
However I am unsure how I would do it so I could get an array for the past 3 days.
I have an idea of how I could do it in PHP, having a for loop and then changing the first argument in STR_TO_DATE each time, but I am curious, is there a way I could do this using a SQL query only?
UPDATED
SELECT COUNT(*) recordCount
FROM exampletable
WHERE DATE(`date`) BETWEEN CURDATE() - INTERVAL 3 DAY AND CURDATE()
GROUP BY DATE(`date`)
NOTE This query wont use any index on date
SELECT COUNT(*) AS 'recordCount' FROM dbo.Table_1
WHERE dt > DATEADD(dd,-3,GETDATE())
GROUP BY DATEPART(dd,dt)

Categories