Structure of MYSQL database table for visitor counter [closed] - php

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have a userbase on db
______________________________________________
id | Name | so many details | Unique page link
______________________________________________
1 | abc | bla bla | username
So, user abc have a page xyz.com/username
I want to show user abc stats about visitors i.e. like
1 jan - 400
2 jan - 350
and so on for last 7 day
and also the month wise record for last 12 months
jan - 49009
feb - 73849
what would be the best MYSQL database table structure design.
_________________________________________________________________
user id | day 1 | day2| and so on for 7 day | Jan | Feb | Mar
______________________________________________________________
111111 | 400 | 300 | | 4250|24534|2435
I thought of something like this - is it OK or other optimized design is there?

I would not keep a count in a database this way.
I would store records with timestamps as a column and roll them up using a GROUP BY every time I wanted them.
If this is a reporting database, I'd recommend looking into a star schema with a time dimension.

You don't want to do it your way because you will have a long calender for each user.
It will get out of hand quickly.
You would want to have a table containing:
ID
user_id
time_stamp
info
1
324
2014/1/22
300
2
327
2014/1/20
500
3
324
2014/1/19
900
Than when you want the info.
Select * FROM table where user_id = 324
Would return
ID
user_id
time_stamp
info
1
324
2014/1/22
300
3
324
2014/1/19
900

aI think that something like this would be more easy to manage.
user_id
date
counter
For every visit check if the user is there for that date. if so increment, otherwise create record.
Then you can do as many stats as you want by using the date (day, month, interval etc.)

Related

Is there a way to count repeated strings in records from a MySQL Table? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I want to know if there is a way to count repeated strings in diferent records in a MySQL table, for instance, given a table like this one:
+----+-----------------------+
| id | data |
+----+-----------------------+
| 1 | this is here |
+----+-----------------------+
| 2 | this would be nice |
+----+-----------------------+
| 3 | this was here |
+----+-----------------------+
| 4 | this needs to be said |
+----+-----------------------+
I'd like the following result:
Count: this(4), here(2)
This is what I've been looking for, but no luck until now.
You can start with this:
SELECT id, data, SUBSTRING_INDEX(SUBSTRING_INDEX(data,' ',x.x),' ',-1) as Y
FROM test
CROSS JOIN (select 1 as x union select 2 union select 3 union select 4) x
ORDER BY id, x.x
see: DBFIDDLE
output:
id
data
Y
1
this is here
this
1
this is here
is
1
this is here
here
1
this is here
here
2
this would be nice
this
2
this would be nice
would
2
this would be nice
be
2
this would be nice
nice
3
this was here
this
3
this was here
was
3
this was here
here
3
this was here
here
4
this needs to be said
this
4
this needs to be said
needs
4
this needs to be said
to
4
this needs to be said
be
Things left to be done:
Check If you have more, of less than, 4 words. Currently when you only have 3 word the last word is repeated, and when having more than 5 words they are ignored.
Count the stuff, but that is basic SQL stuff using count(*)
BONUS: Getting the number of words in a string:
SELECT
id,
data,
length(data)-length(replace(data,' ',''))+1 as NrOfWords
from test;
output:
id
data
NrOfWords
1
this is here
3
2
this would be nice
4
3
this was here
3
4
this needs to be said
5

Cheapest way of managing relational SQL data [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to find the best method for managing the huge-relational game data.
Let me explain my data structure.
There are three main data field. User, Bets and Coupons.
+----------------------------------------------------+
| bets |
+----------------------------------------------------+
| id | status | yes | no |
+----+-----------+-----------------+-----------------+
| 1 | 0 | 1.45 | 2.52 |
+----+-----------+-----------------+-----------------+
| 2 | 1 | 3.00 | 1.08 |
+----+-----------+-----------------+-----------------+
| 3 | 2 | 2.43 | 1.42 |
+----+-----------+-----------------+-----------------+
+----------------------------------------------------+
| coupons |
+----------------------------------------------------+
| id | played_by | bets | status |
+----+-----------+-----------------+-----------------+
| 1 | 1 |1,yes;2,no;3,yes;| 0 |
+----+-----------+-----------------+-----------------+
| 2 | 2 |2,yes;3,no;1,no; | 0 |
+----+-----------+-----------------+-----------------+
| 3 | 3 |1,yes;2,no; | 0 |
+----+-----------+-----------------+-----------------+
Information: Every bet has yes/no choice. Users play bets. We register them inside of coupons. If all bets inside a coupon WIN, coupon wins and user get extra balance. Classic. Please note that there will be so many bets (avg. 5 per coupon), so many coupons played by users (thousands), and thousands of users.
So I'm trying to find best method for finalizing bets and checking coupons for win or lose process.
Method 1 I tried;
We finalized Bet ID: 2 as yes;
Check 2,yes; with "LIKE" operator in coupon, if there is, concat(append) 1 to progress field.
Check how many bets are there inside the coupon.
If count of 1s equals to numbers of bet inside this coupon, set coupon status to WON.
Method 2 I tried;
Finalize bets; YES or NO
Check related coupons with a cron task.
I liked both methods, but I want users see their progress immediately, so I am not sure about the cron method. Both methods work fine, but I have doubts what will happen when there are thousands of users.
I hope I described my issue understandable. I'm looking for comments and suggestions.
Thanks.
Instead of appending a user's bet to a value in a coupon (which is highly inefficient since you're having to use the LIKE operator), it makes more sense to just create a table of coupons that store the ID of the bet its associated with it, the ID of the user it belongs to, and the value of the coupon (YES or NO). So your Coupon table would look like the following:
Coupons
ID BetID UserID Value
1 1 10 YES
2 1 11 NO
Now if you want to acquire all of the coupons associated with Bet #1, you would just do a SELECT * FROM coupons WHERE BetID=1.
If Bet #1 wins, all you would need to do is acquire the value of the bet for the winning choice, and update all of the users who fall under the choice. For example:
# Select the winning value:
SELECT <winning value>
FROM bets
WHERE id = <id of completed bet>;
# Update the users:
UPDATE users
SET balance = balance + <winning value>
WHERE id EXISTS (SELECT userID from coupons where betID = <id of completed bet> AND value='<winning value>');

MySQL query to get one of each Item [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am working with php and MySQL, I am making a messaging app, the app has communicates with a remote database, in my database is my messaging table, the table has 9 tables of which includes the "Subject table", I need help, I want to make a query that retrieves only one subject of each type, lets say I have 10 messages with the subject "Man", 12 of the "Dog", I want it to get only one man and one dog below is a graphic representation of my Messaging Table.
| message_id | subject | username |
|:-----------|------------:|:------------:|
| 1 | cyber | Chrome |
| 2 | Hyper | Ciare |
| 4 | Cyber | Gorger |
You can use SQL's DISTINCT:
SELECT DISTINCT subject FROM table
This will fetch each subject only one time, even if it appers more than once.

How do I pull large number of multiple rows from one table?

This is an expansion of my original question located here:
How do I pull all rows from a table with one unique field and specific values for another field?
I have a table with two fields: user_id and skill_id.
I want to pull out all rows that have a skill_id of a certain number but I have a large number of skill_id's to search for (~30). I was using the self-join suggestion presented in the question linked above but with so many skills to look for, that query is proving extremely slow.
How can I look for a large number of skill_ids without bogging down the query?
EDIT:
Here's an example of what I'm looking for. Using the table below, I want to pull out all rows of users that have skill_id of 10 AND 11 AND 12, etc. (except I'd be looking for more like 30 skills at a time).
TABLE
user_id | skill_id
=====================
1 | 10
1 | 11
1 | 12
1 | 13
2 | 10
2 | 12
2 | 13
3 | 15
3 | 16
4 | 10
5 | 45
5 | 46
If I understand your question well, below query might help you. Assuming (user_id, skill_id) is UNIQUE or PK.
SELECT user_id
FROM tab
WHERE skill_id IN (30 entries)
GROUP BY user_id
HAVING SUM(skill_id IN (30 entries)) = 30;
You can test here. http://www.sqlfiddle.com/#!2/f73dfe/1/0
select user_id
from table
where skill_id IN (10,11,12...)
make suer skill_is is indexed

How would a database structure for a quiz look like? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Here's how my current quiz database structure looks like:
For the sake of testing and simplicity, I've put up only 3 questions.
This is how my table (quiz) looks like:
| id | score | q1_answered | q2_answered | q3_answered |
--------------------------------------------------------
| 1 | 210 | 0 | 1 | 0 |
| 9 | 380 | 1 | 0 | 1 |
| 5 | 210 | 1 | 1 | 1 |
--------------------------------------------------------
Each question column has numbers from 0 to 1, 0 indicating is hasn't been answered and 1 indicating it has been answered correctly.
I was wondering if any of you have another idea of creating a better structure or better, efficient.
Is there also an efficient way to add a timestamp for a user who has solved a specific question?
Say for example: If a user answers question 1, the time of answer will be recorded and will be displayed.
This structure could work but I always thought it might not be efficient if I were to add say 50 more questions.
Any help would be appreciated.
I would create two tables based on the table you have:
scores
sID score testID
1 210 1
9 380 1
// etc etc
quests
sID testID question answer anstime
1 1 1 0 1231237128961
1 1 2 1 1231237128964
1 1 3 0 1231237128968
9 1 1 1 1231237128961
9 1 2 0 1231237128968
Basically, you are normalizing your data in a way that you can easily add more questions without the need to modify the table in any way, you still have a nice clean table where you keep scores that is easy to join
You can also then do much more interesting queries, like How many people answered question 3 at a certain time, aggregate the data nicely and of course, still join it back to the scores if you want to display all the results for a particular ID.
I have also added two columns in the tables called testID. This way, you are able to track not only multiple users for one test, but multiple users across multiple test. you will be able to see whether a student is improving over the course of tests or progressively getting worse as the subject carries on.
Edit: To copy data from your structure will be a bit annoying, but this should get you started on the path at least:
insert into quests (sID, question, answer)
select sID, testID, q1_answered from yourTableName
You can insert the data into the new structure by using a select statement on your original tables like I showed you above.
In a nutshell, you define one table for the questions and one for the answers given by users:
Questions:
QuestionID, QuestionName, ...
Answers:
AnswerID, UserID, QuestionID, AnswerGiven, AnswerCorrect, DateAnswered
This way if you add a new question, you don't have to change the table structure.
I wouldn't worry about efficiency with 50 questions. Just have some fun programming. Try things out. If you have a million questions, or hundreds of thousands of questions you might start to think about efficiency. Computers are really fast.

Categories