How to avoid repeated content in mysql tables? - mysql

I have two tables and they have "Many to Many" relation. The problem is in mirror table where I have 2 more fields "name" and "status". For name field values will be "Youtube, DailyMotion and Mp4Upload" and for status values will be "Subbed, Dubbed and Raw". For every link I am placing these value and the duplication occurs.
Demo:
mirror table
id name link status
1 Mp4Upload Some link 1 Subbed
2 Mp4Upload Some link 2 Subbed
3 Mp4Upload Some link 3 Subbed
4 Mp4Upload Some link 4 Raw
5 Mp4Upload Some link 5 Raw
6 YouTube Some link 6 Dubbed
7 YouTube Some link 7 Dubbed
Is there any way I can avoid repeated content by some relation? Like consider the above relation. One episode have many mirrors and many mirrors belong to one episode. We solved this using a third table pivot table episode_mirror.
Other Tables are
episode table
id name
1 First Episode
2 Second Episode
3 Third Episode
4 Fourth Episode
episode_mirror (pivot table)
id episode_id mirror_id
1 1 1
2 1 2
3 1 3
I hope this make sense to every one and thanks a lot.

Create a table for both name and status.
status_id | status
1 | subbed
2 | dubbed
3 | raw
name_id | name
1 | mp4upload
2 | youtube
3 | dailymotion
Then in your first table, use ids corresponding to those new tables
id name_id link status_id
1 1 Some link 1 1
2 1 Some link 2 1
3 1 Some link 3 1
4 1 Some link 4 3
5 1 Some link 5 3
6 2 Some link 6 2
7 2 Some link 7 2
This will save you a large amount of space and repetition from repeating the strings.

Related

Create a Matrix-View in MySQL [duplicate]

This question already has answers here:
How can I return pivot table output in MySQL?
(10 answers)
MySQL - Rows to Columns
(13 answers)
Closed last month.
if have 2 tables:
tool_id
name
status
1
Mike
1
1
Claus
3
1
Max
3
2
Mike
2
2
Claus
2
3
Mike
1
3
Claus
3
3
Max
1
3
Sam
1
4
Mike
2
and
tool_id
tool_name
1
51363
2
52514
3
51458
4
51608
I need a view that shows me these 2 tables as a matrix. The tools should be displayed in the table headers and the names and their status for the corresponding tool as a table.
If a name is not assigned to a tool, it should be given a zero.
name
51363
52514
51458
51608
Mike
1
2
1
2
Claus
3
2
3
0
Max
3
0
3
0
Sam
0
0
1
0
I have more than 400 tools and workers each in the two tables.
Is something like this possible?
Edit: This is not an duplicate of the linked posts.
I need a dynamic creation of columns.

Gaming Database Query for multiple players

I want to have a SQL result look like this (match result query):
ID Arena Winner Loser Winner_score Loser_score
-----------------------------------------------------------------
1 1 Johnny Mark 16 8
2 2 Andrew Luke 16 7
Here are my tables (simplified)
Player_tbl
ID Player
-------------
1 Johnny
2 Mark
3 Andrew
4 Luke
Match_tbl
ID Match Arena
----------------------
1 Match 1 1
2 Match 2 2
Match_results_tbl
ID Match player_id player_score
-----------------------------------------
1 Match 1 1 16
2 Match 1 2 8
1 Match 2 3 16
2 Match 2 4 7
I am wondering how to structure my query to have the desired result.
Ok, I figured out my own issue. 1st, the match_results must be put in a table with a different structure, and 2nd, the inner sql query needed to be adjusted to pull from the proper database tables.

Single Foreign Key for multiple look up tables

I'm building a simple application that would record several widget selections within a form (drop-down list and multiple select for this example) and save it within a log table in a small database.
http://i.stack.imgur.com/THpfs.png
This schema represents a selection of a Fruit from a drop down, and 1-n Fruit Type Selection from a multi-select:
Fruit
FruitId Name
1 Apple
2 Orange
3 Melon
Orange
OrangeId Name PLU1 PLU2
1 Navel 123 321
2 Blood 213 412
3 Cara 512 433
LogFruitSelection
LogId FruitId FruitSelection
1 2 2
1 2 3
2 1 1
2 1 2
2 1 3
2 1 4
The user is restricted to select only one fruit which will give them an option to select multiple types of that fruit, 1 type of fruit per submission. The selections will be held within a selection table which will reference a LogId, a FruitId, and a SelectionId which references a lookup table.
I am lost as to how to create a schema that would let me use a selection table with a single foreign key that would reference several look up tables based on another columns value.
Hope this is what you want
Fruit
FruitId Name
1 Apple
2 Orange
3 Melon
Fruit Type
TypeId Name PLU1 PLU2 FruitId
1 Navel 123 321 2
2 Blood 213 412 2
3 Cara 512 433 2
LogFruitSelection
LogId FruitId FruitSelection
1 2 2
1 2 3
2 1 1
2 1 2
2 1 3
2 1 4

MySQL query - need assistance creating this query

I need some direction on how to create this MySQL query.
I have a table that looks like this.
id group name value
1 1 user mike
2 1 setting 1
3 2 user joe
4 2 setting 2
5 3 user jill
6 3 setting 1
7 4 user mark
8 4 setting 1
9 4 other 22
I would like to format the query to group users that have identical settings (IE mike and jill would be grouped in this example, not mark because of "other")
At the end of the day I am trying to consolidate and make the table look like this. If I can figure out the query to properly group them ,I will use PHP to combine the values and save it back to the DB.
id group name value
1 1 user mike OR jill
2 1 setting 1
3 2 user joe
4 2 setting 2
7 4 user mark
8 4 setting 1
9 4 other 22
Thank you!

I need sql query for getting the Required format

i am having table Category with the columns like
id(AutoIncrement),Parent_id,Level,Name
initially for level 1 datas has Parent_id is 0. autoincrement id will be Parent_id for next levels .my table table table datas will bw like this
id Parent_id Level Name
1 0 1 Indian
2 0 1 International
3 0 1 Nri
4 1 2 BC
5 2 2 Christian
6 2 2 Muslim
7 4 3 MBC-1
8 7 4 OBC-2
9 1 2 FC
i want to show records in this format (its like a tree view)
id Parent_id Level Name
1 0 1 Indian
4 1 2 BC
7 4 3 MBC-1
8 7 4 OBC-2
9 1 2 FC
5 2 2 Christian
6 2 2 Muslim
2 0 1 International
3 0 1 Nri
4 1 2 BC
Can any one should help me to get this arrangement of datas using sql Query?
If it does not have a set number of branches you likely want to loop over a query in your app or write an SP to obtain all nodes. Some good reading here:
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/