age range for date of birth - php

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

Related

How to select all entries of a user from a database?

I am trying to make a "top purchaser" module on my store and I am a bit confused about the MySQL query.
I have a table with all transactions and I need to select the person (which could have one or many transactions) with the highest amount of money spent in the past month.
What I have:
name | money spent
------------------
john | 50
mike | 12
john | 10
jane | 504
carl | 99
jane | 12
jane | 1
What I want to see:
With a query, I need to see:
name | money spent last month
-----------------------------
jane | 517
carl | 99
john | 60
mike | 12
How do I do that?
I do not really seem to find many good solutions since my MySQL query skills are quite basic. I thought of making a table in which money is added to the user when he buys something.
That's a simple aggregated query :
SELECT t.name, SUM(t.moneyspent) money_spent_last_month
FROM mytable t
GROUP BY t.name
ORDER BY t.money_spent_last_month DESC
LIMIT 1
The query sums the total money sped by customer name. The results are ordered by descending total money spent, and only the first row is retained.
If you are looking to filter data over last month, you need a column in the table that keeps track of the transaction date, say transaction_date, and then you can just add a WHERE clause to the query, like :
SELECT t.name, SUM(t.moneyspent) money_spent_last_month
FROM mytable t
WHERE
t.transaction_date >=
DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1 DAY)
AND t.transaction_date <=
DATE_SUB(NOW(), INTERVAL 1 MONTH)
GROUP BY t.name
ORDER BY t.money_spent_last_month DESC
LIMIT 1
This method is usually more efficient than using DATE_FORMAT to format dates as string and compare the results.

Effectively Firing a multiple query

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

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)

php and mysql get data by year and date wise! table structure?

While making a sample details page, I just stuck upon a really confusing situation. What i actually want to do is like this:
+----------------------------------------------------+
+ Year | Month | Date | Total Gain +
+----------------------------------------------------+
+ 2003 | January | 26/01/2003 | +90 % +
+ 2003 | January | 27/01/2003 | +10 % +
+ 2003 | Feburary| 01/02/2003 | -29 % +
+ 2003 | Feburary| 15/02/2003 | +0.52 % +
+----------------------------------------------------+
What I actually want is that I can list the Month names and year wise like mysql_query('SELECT month FROM mytable WHERE year="2003"');
But the problem is it shows January two time. I want to display January one time and a link next to it which will carry to the next page. This will show the stats of January.
Maybe you should use a GROUP BY clause and calculates your Total gain as well.
(Since the table structure is not specified, I'll guess here)
SELECT `Year`, `Month`, SUM(gain) AS 'Total Gain +' FROM table GROUP BY month, year;
I think what youre looking for is DISTINCT query, ie
SELECT DISTINCT year, month FROM mytable

closest value and last value problem

I have a data table having fields(date,company,data_id,rank etc)..as problem is related to these fields that's why showing these fields only. suppose table is:
data_id | company | date | rank
1 | google | 23/10/2010| 1
2 | yahoo | 23/10/2010| 4
3 | msn | 23/10/2010| 8
4 | google | 27/10/2010| 3
5 | yahoo | 27/10/2010| 1
6 | msn | 27/10/2010| 6
7 | google | 29/10/2010| 1
8 | yahoo | 29/10/2010| 4
9 | msn | 29/10/2010| 3
...and so on
PROBLEM 1:
there are many users-suppose there are user1,user2,user3. All have their [my_company] in session.
Now, I have to display only those entries which are made last(can be done by any user on any date) as per company.
Example: my_company[user1-yahoo,user2-google,user3-msn]
user's [my_company] only display his company's value,nothing else..but only value entered last(on date-here 29/10/2010).
Data is added for any company by any user on any date.now as this process will continue, entries will grow.HOW CAN I FIND WHICH DATE IS LAST(specific to a company)?
PROBLEM 2:
how to find closest date to a specific date?
... where `company_name` = 'companyName' order by `date` desc limit 1
and
... between mydate - INTERVAL and mydate + INTERVAL
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
when you create session variable sort data by date so you will have order in my_company
once you have ordered list you can figure out which date belongs to which date
or
while adding data to my_company add id
you can find closest date by
SELECT date FROM table ORDER BY abs(now() - date) LIMIT 1
http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_abs

Categories