PHP/MYSQL group and count by distinct dates and users - php

I'm trying to wrap my head around how to get a distinct count of days a user logged in, when the db has each login session stored with a time and date stamp (DATETIME column). EG:
USERID | TIME | BOUGHT
--------------------------
4 | 2012-07-16 04:44:52 | 3
4 | 2012-07-16 04:45:52 | 2
5 | 2012-07-16 04:54:52 | 5
4 | 2012-07-18 04:44:52 | 3
4 | 2012-07-18 04:45:52 | 2
4 | 2012-07-21 04:54:52 | 5
I want to search for how many times user 4 bought from the site - and the query should return 3 (because user 4 visited the site a total of 3 days). Do I have to use a PHP/MYSQL combination or can I do it in SQL?

SELECT USERID, COUNT(DISTINCT DATE(TIME)) FROM my_table GROUP BY USERID
See it on sqlfiddle.

Related

How to count all indentical values in Column which belongs to Different Values in another column

I have a mysql database table which has 4 columns: ID, Date, SessionID, UserID(irrelevant here). Let's call them A, B, C,
What I can't figure out is how to count how many sessions has been booked to specific date.
For example something like this
A | B | C
---|------------|------
1 | 2016-02-12 | 1 |
2 | 2016-02-12 | 1 |
3 | 2016-02-13 | 1 |
4 | 2016-02-12 | 5 |
5 | 2016-02-12 | 5 |
6 | 2016-02-13 | 2 |
7 | 2016-02-18 | 2 |
8 | 2016-02-19 | 3 |
So I want that my php code would output
that for date 2016-02-12 I have 2 entries with value 1 and 2 entries with value 5.
For date 2016-02-13 I have one entrie with value 1 and one with value 2.
This should work as you need
SELECT B AS date,COUNT(C) AS sessions FROM table GROUP BY B,C
It's a simple GROUP BY.
SELECT Date, SessionID, COUNT(*)
FROM yourTable
GROUP BY Date, SessionID

Bunching a group of notifications

I have a notifications table with me:
+----+------+--------+--------+------------+
| ID | User | Object | Action | TimeStamp |
+----+------+--------+--------+------------+
| 1 | 1 | 3 | Like | 2014-05-01 |
| 2 | 2 | 3 | Like | 2014-05-01 |
| 3 | 3 | 3 | Like | 2014-05-01 |
| 4 | 3 | 5 | Share | 2014-05-01 |
+----+------+--------+--------+------------+
If you can see, the Users 1, 2, 3 have liked the same object 3. In the Notifications window, if we just give a simple SELECT query, it shows like this:
User 1 has liked Object 3. 2 mins ago
User 2 has liked Object 3. 2 mins ago
User 3 has liked Object 3. 2 mins ago
User 3 has shared Object 5. 2 mins ago
But since the action has been done on the same object, I would like to bunch or group the notifications like this:
Users 1, 2, 3 has liked Object 3. 2 mins ago
User 3 has shared Object 5. 2 mins ago
I have the following questions:
How do I group / bunch the notifications?
Is my notifications table schema the right one?
select object, action, group_concat(`user`) as users
from notifications
group by object, action

How to get standard deviation of grouped rows?

I want to calculate the standard deviation between page views on my site. I'd like to do this using pure MySQL - without querying the whole table to the webserver - and return a single number to the PHP code for further use. Each page view is stored as a visitor_id - page_id - visit_count trio as per the following schema:
+============+=========+=============+
| visitor_id | page_id | visit_count |
+============+=========+=============+
| 1 | 2 | 7 |
+------------+---------+-------------+
| 2 | 2 | 4 |
+------------+---------+-------------+
| 1 | 1 | 17 |
+------------+---------+-------------+
| 3 | 2 | 12 |
+------------+---------+-------------+
| 1 | 3 | 639478 |
+------------+---------+-------------+
| 2 | 1 | 6 |
+------------+---------+-------------+
page_id refers to a PRIMARY_KEY in the pages table, visitor_id refers to a PRIMARY_KEY in the visitors table. The above table's primary key is the visitor_id - page_id pair, since the same page seen by the same visitor is recorded by increasing the visit_count of the corresponding row, instead of creating a new one.
Before calculating standard deviation, the entries should be grouped together by page_id, their visit_count summed (visitor_id can be ignored here), so, effectively, I want to calculate the deviation of the following:
+=========+=============+
| page_id | visit_count |
+=========+=============+
| 2 | 23 |
+---------+-------------+
| 1 | 23 |
+---------+-------------+
| 3 | 639478 |
+---------+-------------+
I'm aware of the possible PHP solutions, but I'm interested in a MySQL one.
If you want the standard deviation for each page (i.e., the visitors are the population):
select page_id, sum(visit_count) as visit_count, std(visit_count) as visit_std
from table1
group by page_id;
If you want the standard deviation over the pages:
select std(visit_count) as page_std
from (select page_id, sum(visit_count) as visit_count
from table1
group by page_id
) t;
You could create a new table that stores timestamp + current views so you can view a history of changes in views. You'd be able to check the last two timestamped entries and how much the difference is between the two as well as a whole bunch of other stuff you haven't even thought of yet. Like graphs. Or pie charts showing activity increases per week day. Mmmm pie.

Fixture generator in PHP or MySQL

I am trying to generate fixtures for a sports website. I have a table called Members, with relevant columns being member_id and league_id. The league_id will be passed from a form on the previous page as the variable $leagueid.
I'm pulling out all the member ID's relating to that league ID using...
$result = mysql_query("SELECT member_id FROM Members WHERE league_id = '$leagueid'")
I now need to generate fixtures for all these member ID's and then insert that data into the MySQL table 'Fixtures'. Each row of data in that table needs to include:-
player1 - member_id of the first player
player2 - member_id of the second player
week - integer showing which week the match will be played
league_id
However, there are some special conditions that also need to be applied.
Every 3rd week needs to remain free (i.e. weeks 3,6,9,12 etc). No matches can be scheduled on these weeks
There needs to be an option (which will be selected and passed from a form on the previous page as a checkbox variable called $double) which will double up the matches. This means that after generating one complete round of fixtures, you need to take the generated list, swap ID's for players 1 and 2, and duplicate them all. So 1 round of fixtures could look like this....
Week 1
1 vs 2
3 vs 4
Week 2
1 vs 3
2 vs 4
Week 3
1 vs 4
2 vs 3
Then you would swap the ID's and add on another set...
Week 4
2 vs 1
4 vs 3
Week 5
3 vs 1
4 vs 2
Week 6
4 vs 1
3 vs 2
What I'm looking for is some code that will generate all these fixtures while keeping in mind all the special conditions that I've listed.
I know this is all possible in PHP but I also think I can do it using one SQL query instead, which might be a lot cleaner. Can anyone help me out?? Thanks!!
P.S. I know I'm using mysql and not mysqli. I am currently trying to transfer over to mysqli but I'm having some problems which I have posted on a separate question that I have yet to get a correct answer to.
SELECT * FROM ints;
+---+
| i |
+---+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+---+
SELECT * FROM ints WHERE MOD(i,3) > 0;
+---+
| i |
+---+
| 1 |
| 2 |
| 4 |
| 5 |
| 7 |
| 8 |
+---+
The second part is simply
INSERT INTO my_table SELECT week_id+3, column_2, column_1 FROM my_table;

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