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
Related
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.
Note : I cannot change Table structure
These are the three tables- user,user_field_type and user_field_value which needs to be joined and produce the result shown below
user
User_code User_id
-----------------
test11 000_1
test12 000_2
test13 000_3
test14 000_5
user_field_type
field_name field_id
----------------------
Name 100_1
Age 100_2
Class 100_3
Roll 100_4
User_field_value
user_id field_id field_value
-------------------------------------
000_1 100_1 Tom
000_1 100_2 6
000_1 100_3 2
000_1 100_4 1
000_2 100_1 Dick
000_2 100_2 6
000_2 100_3 2
000_2 100_4 2
000_3 100_1 Harry
000_3 100_2 6
000_3 100_3 2
000_3 100_4 3
Result Needed:
user_id user_code Name Age Class Roll
------------------------------------------------
000_1 test11 Tom 6 2 1
000_2 test12 Dick 6 2 2
000_3 test13 Harry 6 2 3
I tried Group concat which doesn't produce desired results and i'm afraid of hard coding columns as it will require changes everytime we add a new user field type
I have two tables, the first is the following
"Categories"
id name parent_id
1 Food NULL
2 Vegetable 1
3 Fruit 1
4 Tomatoes 2
5 onion 2
6 bananas 3
7 grapes 3
the second table is the following
"amounts"
id amount category_id
1 100 4
2 50 6
3 25 5
4 100 7
5 50 4
6 25 7
Currently categories table are three levels and I want to return all
the Categories along with total columns:
Means that sum of the category of level 2 is the total of level 3 and
the total of level 2 should was the sum of level 1. I would really
appreciate some help Beforehand thank you very much.
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.
Consider the database (made up for example):
Table 'owner'
id name
1 john
2 andrew
3 peter
Table 'duck'
id name ownerID
1 duck1 1
2 duck2 1
3 duck3 1
4 duck4 2
5 duck5 2
6 duck6 3
7 duck7 3
8 duck8 1
9 duck9 3
10 duck10 3
11 duck11 1
12 duck12 2
Table 'food'
id name type duckID
1 beef meat 4
2 grass veg 8
3 lemon fruit 5
4 apple fruit 3
5 pizza snack 7
I wish to write some SQL that for each OWNER, COUNT the number of ducks which eat some kind of food. For example for owner John, there would be 2 ducks (ducks 3 and 8).
So far I am writing this code:
select owner.name, count(duck.ownerID)
from duck, food, owner
where duck.id == food.duckID
and duck.ownerID == owner.id;
I am getting the result:
Peter | 5
Any suggestions are appreciated.
This is done with an group by clause:
select owner.name, food.name, count(duck.id)
from duck, food, owner
where duck.id == food.duckID
and duck.ownerID == owner.id
group by owner.name, food.name;
This query gives you one row for each combination of owner name and food name with the number of the owner's ducks eating this food.