How to deal with a large mySQL tree - php

I am working on a project which user registration multiples like this.
1st Month -> 1 user
2nd Month -> 4 users comes under the above user
3rd Month -> 16 users (i.e, 4 users comes under each 4 users above )
4th Month -> 64 users (i.e, 4 users comes under each 16 users above )
eg:
1
2 | 2
8 | 8
32| 32
and continues...
Please give me an advice, how to store this in database.
Thanks in advance.

Assuming user_id is your primary key, create a parent column that contains the user_id of the parent user. For example:
user_id parent
1 NULL
2 1
3 1
4 1
5 1
6 2
...
You will also want to create an index on the parent column so that you can quickly do a reverse lookup (i.e. find all children of a given user).

Related

after multiple delete maintain order column

in my project i have to maintain a order of every record and if i delete in between i have to maintain order in a form that order don't have any gape like after 3 next is 4 only... not 5 or 6......my display like.
if i delete multiple record that have Rule ID are 1,3 and 4...then my rule ID 0 remain 0, 2 becomes 1, and 5 becomes 3...like that i want. i am deleting record base on id.
id rileid
1 0
3 1
5 2
6 3
11 4
14 5
how to do that? please help me.

Creating a table for each individual user's timetable data

I'm coding an online timetable website which holds all your current subjects you are attending at school and contains slots to write work and it's due date. Mainly for my own personal use, but I guess I could share the URL with a few people when it's completed. Anyway, I have a login / register system set up, linked to a MySQL database. I have the following columns in a table called 'users'.
userid, username, firstname, lastname, password
Before I attempt anything too stupid, I wanted somebody to give an opinion on what I am about to attempt...
I thought I could write some PHP that creates a table for each new user when they sign up. It would contain all their subjects for each day of the week, and once they input their data it would write it to the database and they wouldn't have to edit their information unless they had to (subject change, etc...)
Would a whole new table for each user's subject data be efficient? The data would have two dimensions: The day of the week (x axis) and the periods of the school day where the subjects are situated (periods 1-6 for my school)
Anyway, thanks for reading, opinions on the best way to go around doing this would be helpful. Thank you.
EDIT: Strawberry's suggestion
userid,day,period,subjectid
1 1 1 4
1 1 2 2
1 1 3 5
1 1 4 3
1 1 5 1
1 1 6 7
2 1 1 4
2 1 2 2
2 1 3 5
2 1 4 3
2 1 5 1
2 1 6 7

How to synchronize pivot table in plain PHP

Assume we have these tables
Users table
-----------
id name
-----------
1 xxx
2 yyy
3 ccc
4 bbb
5 aaa
Location table
-------------
id name
-------------
6 Spain
7 Russia
8 Germany
9 USA
Pivot table
------------------------
id user_id location_id
------------------------
1 1 6
2 2 8
3 1 8
4 1 9
5 3 8
What I want to achieve is to sync the data in the pivot table.
So exactly I have post request with array of user ids = [1,5,4] and location_id = 8. So I would get the following result
[updated] Pivot table
-------------------------
id user_id location_id
-------------------------
1 1 6 <-- This one stays
3 1 8
4 1 9
6 5 8 <-- Added
7 4 8 <-- Added
...we deleted the row with location_id=8 and user_id=2 and user=3 because those are not in the users array
How can I do add the new ones, delete the ones that are not in the request, and leave the one that already exists with some functionality, just like Laravel has done it in sync function.
I know that the easyest way is to get all the users that has that specific location_id, delete all, and than insert once again. Is there some workaround or should I do the newbie way :D
Thank you
Not sure if this is the best way to go (actually deleting everything and then inserting the values seems easier and cleaner), but here's one way to do it, if for some reason you want to keep the pivot table ID:
Define a unique index for the table, considering both pivot fields (user_id,location_id).
Insert all data using ON DUPLICATE KEY UPDATE user_id = user_id and location_id = location_id, so that if there's duplicate values, mysql will keep them without modifications and will not throw error.
Implode the Array IDs from the Request and execute a delete from the table, where data is not in the specified values.

Duplicate Data Entry in Database table using PHP and AJAX

We have a database that has more than 70 tables with more than 10k of users. One of the tables in the database is to keep recording of user's latest actions/changes in order to keep them pending until the gameweek finishes (Football matches), let's call this table 'Substitute'.
However, every week when we advance the system (apply changes) at the end of gameweek, we check to see if there's any duplicated data stored regarding user's changes (we have noticed that a while ago), and remove them manually from the database.
The rows have primary keys (which is not duplicated) only the data is duplicated. It's like the insert query is being fired twice. (not sure about this or why)
Example: Let's say the user has a team of football players (11 on the field and 4 substitutes)
when the user chooses player and clicks substitute, that player (on the field) will be exchanged with the one who's in on the bench (Substitute). This process will happen immediately without the need of user to save (JavaScript). But, it not be saved in the team details, instead, it will be saved in the Substitute table that keep track of changes.
User wants to remove player 1 and enter player 12 (1->12). For the first step the system will record
id Team from_player to_player
0 x 1 12
And if the user do another substitution
id Team from_player to_player
0 x 1 12
1 x 2 13
And when the user substitute back the player (12->1) that record will be deleted(1->12), since the user substituted back the player and the later subs cancels the first.
id Team from_player to_player
1 x 2 13
But SOMETIMES it record that row more than once
id Team from_player to_player
0 x 1 12
1 x 1 12
2 x 1 12
3 x 2 13
Or the duplicate could be in between
id Team from_player to_player
0 x 1 2
1 x 2 13
2 x 1 2
3 x 1 2
N.B This happens only to around 10-100 users every week, even though there are more than 10k users registered in our database.
So what do you think is causing the problem?
I don't want to use INSERT IGNORE, I want to find the root of the problem and stop it.
So basically, my questions are:
1- Is it likely to be a server or client side problem?
2- Could ajax code be called/fired twice under certain circumstances?
3- Could be there an error in the sql server where it executes the same query twice?
Really appreciate your help.
UPDATE:
4- If the problem is with the client side, how i inspect it? Any suggested way?
For those who ask, what does happen when the user subs back the same player.
Let's say this is part of the original players stetting.
| Original Status | After First Subs|After Second Subs|
| | | |
On the field | p1 | P12 | P1 |
-----------------------|-----------------|-----------------|-----------------|
| | | |
Subs(at the bench) | p12 | P1 | P12 |
| | | |
Original Status p1 on the field, p12 on the bench (substitute)
Record of actions:
s1- P1->P12
Status= P12 on the field. P1 on the bench
s2- P12->P1
Status= P1 back on the field. P12 back to the bench.
Note that s2 wont be recorded, but, s1 will be deleted. it's like A * -1, and then -A * -1 again. The will cancel each other.
ajax mite be called more than once, tried inspecting using your firebug. you might need to disable your substitute method until the success flag is up.
Maybe the user fired the query more than once through the UI. Since the id is the PK, the firing the same query twice.
Another question.
When given the situation:
id Team from_player to_player
0 x 1 12
1 x 1 12
2 x 1 12
3 x 2 13
when user substitutes back 12->1, does your function delete "ALL" previous records or just 1?

Ordering standings football [soccer] positions by direct matches

I have a problem trying to apply rules about direct matches in a football[soccer] app. I have read this tread and it was very heplful on creating the standing positions table by the points criteria, difference and scored goals.
But i would like to know if is possible to order the teams position by direct matches:
look this positions table:
Pos Team Pld W D L F A GD Pts
1 FC Barcelona 5 2 3 0 8 5 3 9
2 **Inter Milan** 6 2 2 2 11 10 1 8
3 *Real Madrid* 6 2 2 2 8 8 0 8
4 AC Milan 5 0 3 2 8 12 -4 3
As you may see Inter Milan and Real Madrid are tied by points, and the Inter is heading real madrid because its goal difference. The result that i want to get is this :
Pos Team Pld W D L F A GD Pts
1 FC Barcelona 5 2 3 0 8 5 3 9
2 **Real Madrid** 6 2 2 2 8 8 0 8
3 *Inter Milan* 6 2 2 2 11 10 1 8
4 AC Milan 5 0 3 2 8 12 -4 3
Notice that in this time the real madrid is in front the inter milan because it won the two direct matches between them.
i have a table for teams and other for the results.
I would like to achive this using a query in mysql if is possible. Or maybe it would be better if i do this ordering on the server side (PHP).
Thanks any help would be appreciated.
It is impossible to efficiently do what you request in a single query that would return the results you ask for and sort the ties in points with that criteria.
The reasoning is simple: lets assume that you could get a column in your query that would provide or help with the kind of sorting you want. That is to say, it would order teams that are tied in points according to which one has more victories over the others (as this is very likely to happen to more than 2 teams). To make that calculation by hand you would need a double-entry table that shows the amount of matches won between those teams as follows:
| TeamA | TeamB | TeamC
------------------------------
TeamA | 0 | XAB | XAC
TeamB | XBA | 0 | XBC
TeamC | XCA | XCB | 0
So you would just add up each column row and sorting in descending order would provide you the needed data.
The problem is that you don't know which teams are tied before you actually get the data. So creating that column for the general case would mean you need to create the whole table of every team against every team (which is no small task); and then you need to add the logic to the query to only add up the columns of a team against those that are tied with it in points... for which you need the original result set (that you should be creating with the same query anyhow).
It may be possible to get that information in a single query, but it will surely be way too heavy on the DB. You're better off adding that logic in code afterwards getting the data you know you will need (getting the amount of games won by TeamA against TeamB or TeamC is not too complicated). You would still need to be careful about how you build that query and how many you run; after all, during the first few games of a league you will have lots of teams tied up against each other so getting the data will effectively be the same as building the whole double-entry table I used as an example before for all teams against all teams.
create temporary in a stored procedure and call to procedure...
create temporary table tab1 (position int not null auto_increment ,
team_name varchar(200),
points int,
goal_pt int,
primary key(position));
insert into tab1(team_name,
points,
goal_pt)
select team_name,
points,
goal_pt
from team
order by points desc,
goal_pt desc ;

Categories