Pause or manipulate auto increment colum in table - mysql

Hi I have a page where a coach would select a team. The selected players gets assigned to an array $player_names. My table, to which I will upload the values in the array, has 3 colums.
selection_id(auto, increment), Fixture_id, and player_names
Now what happens is I upload the player_names to the database but. For each player that gets uploaded the selection_id colums increments. This is wrong because I want all the selected players to hold the same selection_id.
Any advice on how to tackle this problem will be greatly appreciated.
Practical example:
All the players in the image above should have the same selection_id. Only when the next team is selected for a different fixture should it increment by 1
Selection_id
selection_id is based on the team that gets selected on a previous page. Lets say a coach picks his 5 players for a fixture. Now the 5 players gets uploaded to the db. In the team table the selection_id gets assigned to the 5 players selected. What I am trying to do is have the same selection id with each team (5 players) selected for a specific fixture

You have need two tables to manage same selection_id for selected players. selection_id should not auto incremented.
In first table player_id will be auto incremented.
+-------------+-------------+
| player_id | player_name |
+-------------+-------------+
| 1 | Tendai |
| 2 | Andriaan |
| 3 | Janus |
| 4 | Eben |
| 5 | Burger |
+-------------+-------------+
+-------------+--------------+
| player_id | selection_id |
+-------------+--------------+
| 1 | 117 |
| 2 | 117 |
| 3 | 117 |
| 4 | 118 |
| 5 | 118 |
+-------------+--------------+
Using join query you can get selected player for same selection_id

Related

Return Query Results to Textbox on Userform

let me start by giving you some table structures. I have a 3 relevant tables:
OutreachEventsLog: stores information like Date/Time/Location for community events
| EventID | Date | Location |...
| 1 |2/19/2019 | Earth |
| 2 |2/18/2019 | Earth |
Staff: A list of staff employed in program
|StaffID | First Name | Last Name | Full Name |
| 1 | John | Smith | John Smith |
| 2 | Mary | Sue | Mary Sue |
| 3 | Betty | Jane | Betty Jane |
OutreachEventtoStaff: a junction table that relates staff members to the outreach events they attended.
| Event Id | Staff ID |
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
| 2 | 3 |
I've created a couple of forms to capture event information. The two relevant ones are called OutreachSummary and EventtoStaff. The EventtoStaff form is launched by clicking a button entitled "Add Staff" embedded in the OutreachSummary form. This process is working properly and updating all associated tables correctly.
Here's what I want to do: I want to embed a text box on the OutreachSummary form that lists all the staff members currently assigned to that event, with entries separated by a comma. Example: "John Smith, Mary Sue, Betty Jane..." where Event ID = 2
I've managed to write some code that returns a comma delimited list like the one above to a query. I then tried grab a specific row from this query based on EventID and assign it to the text box with no luck. I keep getting a ?Name error. Is there an easy way to do this? Am I even on the right track?
Thanks for the input!

Implement and query MYSQL foreign key which refers to two tables

I have two tables Hotels and Activities
Hotels
id | name |
1 | hilton |
2 | taj samudra |
3 | galadari |
Activities
id | name |
1 | air balloon |
2 | whale watching |
3 | jungle safari |
also I have a another table Tour
id | starting_location_id | starting_location_type
1 | 1 | hotel
2 | 1 | activity
user can start the Tour from hotel or activity.
What I want is to pass the id and need to get the data like this using a join
id | name
1 | hitlon
2 | air baloon
If I future explain, I want a foreign key which can refer to multiple tables.
Note :- This is not implemented yet. I want to implement this kind of scenario. I want to pass the id in the Tour table and need to get the associated data for that record.
Is this possible to in MYSQL?
You can try below -
DEMO
select t1.id,coalesce(h.name,a.name) as name
from tour t1
left join hotels h on t1.starting_location_id=h.id and starting_location_type='hotel'
left join Activities a on t1.starting_location_id=a.id and starting_location_type='activity'
OUTPUT:
id name
2 air balloon
1 hilton

Application specific MySQL table Structure

I have a question about my DB table structure. I want to know if i'm on the right track or if I have missed a good alternative. Here is the case:
To make it easy to read, I haven't pasted the full contents as my question is only about the structure.
2 tables:
1: id (AI), task
2: id, name, task
Table 1 presents dynamic check-boxes which can be altered by an admin panel so the contents would be like this
1 task1
2 task2
5 task5
(3 & 4 are missing cause the administrator deleted those records).
In table number two are the people who should do the tasks from table 1. And the goal is that the tasks wich are not checked will be displayed.
So the contents of table 2 would be:
1 Name1 1,5
2 Name2 1,2
3 Name3 1,2,5
The numbers in table 2 represent the checked boxes from table 1. So with a query i can compare the numbers from table 2 with the id's from table 1 and display the missing ids as "todo".
In my opinion this looks very overdone, and there must be an easier way to create dynamic options which can be compared and stored as a todo.
Suggestions are welcome!
I suggest you to use basic structure for many-to-many relationship:
tasks users user_tasks
+----+-----------+ +----+-------+ +---------+---------+
| id | name | | id | name | | user_id | task_id |
+----+-----------+ +----+-------+ +---------+---------+
| 1 | Buy milk | | 1 | John | | 1 | 2 |
| 2 | Get drunk | | 2 | Tim | | 3 | 2 |
| 3 | Have fun | | 3 | Steve | | 2 | 4 |
| 4 | Go home | +----+-------+ | 3 | 4 |
+----+-----------+ +---------+---------+
And you can fetch unassigned tasks using following query:
SELECT
tasks.*
FROM
tasks
LEFT JOIN
user_tasks
ON (tasks.id = user_tasks.task_id)
WHERE
user_tasks.user_id IS NULL
You also can fetch users who have no assigned tasks:
SELECT
users.*
FROM
users
LEFT JOIN
user_tasks
ON (users.id = user_tasks.user_id)
WHERE
user_tasks.user_id IS NULL
Hope this will help you.

MySQL table inside table

I'm wondering what is the right way to store the following data inside an MySQL DB.
Let's say an object for example a video. and I want to store the the rating which was give to it by other user, so on option is create a column and store the ID of the users and their rating in a complex way, like that:
| ID | video_name | rating (usrID,rating) |
| 5 | cool video | (158,4),(5875,1),(585,5) |
I guess that it is not the most efficient way to do so.
Is there a way to store a table inside of a row? what is the most efficient way to do it so I can use SQL query on the rating without processing the data via a PHP code?
Create a second table, ratings:
+----------+---------+--------+
| video_id | user_id | rating |
+----------+---------+--------+
| 5 | 158 | 4 |
| 5 | 5875 | 1 |
| 5 | 585 | 5 |
+----------+---------+--------+
You can then group and/or join this table in queries, as desired; for example:
SELECT videos.*, AVG(ratings.rating)
FROM videos JOIN ratings ON videos.id = ratings.video_id
GROUP BY videos.id
Normalize it
Have 2 tables.
I.e.
ID Video_name
5 Cool Video
The second being
vid_id userid rating
5 158 4
5 5875 1
5 585 5
The normal way is an entity relationship diagram.
Then you have
ratings * --- 1 video
That is, you have a table "video"
ID | Name
5 | cool video
And a table "ratings"
ID | value | USERID | video_ID
1 | 4 | 158 | 5
2 | 1 | 5875 | 5
3 | 5 | 585 | 5

Data Entry Tracking (Database Design)

I have developed a website (PHP) that allow staffs to add records on to our system.
Staffs will be adding thousands of records into our database.
I need a way to keep track of what record have been done and the process/status of record.
Here a number of Teams I could think of:
Data Entry Team
Proof Reading Team
Admin Team
When staff (Data Entry Team) completed a record - he/she will then click on the Complete button. Then somehow it should asssign to 'Proof Reading Team' automatically.
A record need to be checked twice from a Proof Reading Team. If StaffB finish proof reading then another member from Proof Reading Team need to check it again.
When Proof reading is done, Admin Team will then assign "Record Completed"
In a few months later record might need to be updated (spelling mistake, price change, etc) - Admin might to assign record to Data entry team.
Is this good data entry management solution? How do I put this into Database Design perspective?
Here what I tried:
mysql> select * from records;
+----+------------+----------------------+
| id | name | address |
+----+------------+----------------------+
| 1 | Bill Gates | Text 1 Text Text 1 |
| 2 | Jobs Steve | Text 2 Text 2 Text 2 |
+----+------------+----------------------+
mysql> select * from staffs;
+----+-----------+-----------+---------------+
| id | username | password | group |
+----+-----------+-----------+---------------+
| 1 | admin1 | admin1 | admin |
| 2 | DEntryA | DEntryA | data_entry |
| 3 | DEntryB | DEntryB | data_entry |
| 4 | PReadingA | PReadingA | proof_reading |
| 5 | PReadingB | PReadingB | proof_reading |
+----+-----------+-----------+---------------+
mysql> select * from data_entry;
+----+------------+-----------+------------------------+
| id | records_id | staffs_id | record_status |
+----+------------+-----------+------------------------+
| 1 | 2 | 3 | data_entry_processiing |
| 2 | 2 | 3 | data_entry_completed |
| 3 | 2 | 4 | proof_read_processing |
| 4 | 2 | 4 | proof_read_completed |
| 5 | 2 | 5 | proof_read_processing |
| 6 | 2 | 5 | proof_read_completed |
+----+------------+-----------+------------------------+
Is there alternative better solution of database design?
i think design it's well done. but may be you want to separate group into groups table, and record_status into status table. If you're storing a lot of records you would store a lot of useless information, at least create an enum type for record_status field and group field
table: groups
id - name 1 - admin 2 - data_entry 3 - proof_reading
...
table: status
id - name 1 - data_entry_processing ...
and if you want the users to be in different groups at a time, you could create users_group table
table: user_groups
group_id - user_id 1 - 1 2 - 1 1 - 4 3 -
4 4 - 4 ....
Hope this helps