Table 1
orderid customerName totalCost
----------------------------------
1 Jonh £200.00
2 Ringo £50
Table 2
orderlineid orderid productName productPrice Quantity
-------------------------------------------------------
1 1 Product1 £150 1
2 1 Product2 £50 1
3 2 Product3 £50 1
Table 3
orderid customerName totalCost
---------------------------------------
1 John £200
---------------------------------------
+ 1 1 Product1 £150 1
+ 2 1 Product2 £50 1
---------------------------------------
2 Ringo £50
---------------------------------------
+ 3 2 Product3 £50
Is it possible (given tables 1 and 2) to create an HTML table similar to table 3? Where for each order underneath there are the orders corresponding order lines information
Thanks
It's not possible to make a single table with real* nested relationships in SQL. Each SQL table is nothing more than a big two dimensional grid, no exceptions.
The correct way to store this type of relationship is in multiple tables, as you already have done.
You haven't said why you are trying to do this, but it sounds like it is a requirement for display rather than for storage. If so, the answer is to have whatever is producing the display query multiple tables as appropriate to get the information you need. This might be a single SQL query with a join, or it might be multiple queries. That depends on having more information about what you are doing.
*Sometimes nested relationships can be modeled within the rigid structure of a single table, but that isn't appropriate in your case.
Related
I am writing a javascript game with three different levels which can be accessed from an html form in the index.html page. I also have a list of players stored in a database and I need to:
Prevent players from accessing levels they have already completed, and
Prevent players from starting a new level until every other player has finished the current level
The way I thought to achieve this was to create two database columns named "level_1" and "level_2" which are set to 0 at the beginning of the level and should change to 1 once the level is complete, like so:
*beginning of game
player_ID level_1 level_2
1 0 0
2 0 0
3 0 0
*level one is completed
player_ID level_1 level_2
1 1 0
2 1 0
3 1 0
*level two is completed
player_ID level_1 level_2
1 1 1
2 1 1
3 1 1
When each entry in column "level_1" is changed to 1 I would disable the level 1 option in the html form and "unlock" the level 2 option. The same applies to the second and third levels.
However, I am unsure how to do this using javascript and php. I have a counter in javascript that records the player moves. When the moves go to zero the level is over and the final score is displayed:
if (moves==0){
document.getElementById('endgame_screen').style.display = "flex";
document.getElementById('score').innerHTML = points
}
I also stored the level and id form choices in the same javascript file:
var level = sessionStorage.getItem('level');
var id = sessionStorage.getItem('id');
What I have been struggling to do is opening the php file from the game javascript file and updating the database entries based on level and id. I know the best way would be to fetch the php file like:
fetch("levels.php", {method="POST"}
but then I get stuck as I am not very familiar with the SQL and PHP syntaxes and I don't know how to send the level and id data through PHP, nor how to update the database. I am also not sure how to then disable the form choices which are on another html page (my guess would be to trigger another PHP file when the form is opened that checks whether there are any zeros in the two database columns and updates the form accordingly).
Any help is appreciated!
You will have to write al least a minimal script in PHP to interact with the database.
The bare minimum would be:
<?php
$con = mysqli_connect("localhost","user","password","dbname");
mysqli_query($con, "UPDATE table SET level=1 WHERE user='johndoe'");
?>
But this is of course the bare minimum, from here you can play with your variables, you can process return values, etc. Also watch our for SQL injection.
You will have to learn a bit of MySQL to do this. Here are a few simple examples that can help:
Retrieve information from the database:
SELECT * FROM table_name;
https://www.w3schools.com/sql/sql_select.asp
Create a new record in the database:
INSERT INTO table_name VALUES (value1, value2, value3, ...);
https://www.w3schools.com/sql/sql_insert.asp
Update a record in the database:
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
https://www.w3schools.com/sql/sql_update.asp
Detele a record in the database:
DELETE FROM table_name WHERE condition;
https://www.w3schools.com/sql/sql_delete.asp
I have this two tables.
id
city
1
bogota
2
tunja
id
branch
cityid
1
sector1
1
2
sector2
2
3
sector3
2
4
sector4
1
now i need to create a way in which the branch select field updates depending on the foreign key of the city
example:
my first combo should contain city bogota and the next combo should only show me the list of branches that are part of that particular city.
how to begin?
i have ag-grid. This is the column structure -
account share qty
i'm grouping by account (rowGroup = true). I have this sample data -
account
share
qty
234
xny
4
234
ghy
3
this appears as
account
share
qty
234
0
xny
4
ghy
3
note that 0 comes for qty. this is because i havenot provided any aggregateFunction to colDef.
I want that qty cell to be empty for group level records (instead of showing a zero). is there a way to do it?
I'm not sure if this is the correct way but I was able to solve this by checking if the row is a group level row in my value formatter.
if the row is a group level row, i would return an empty string. otherwise, i would invoke the formatting function to show formatted data.
I tried to search for an answer to this but I'm not sure if there are any answers to this question because I'm not quite sure how to word it correctly... Here I have two tables
Recipe ingredient table
Recipe_id|ingredient_id
ifqvv |1
ifqvv |2
User ingredient table
User_id|ingredient_id
1 |1
1 |2
2 |1
3 |3
I need to compare these table to where if both recipe and user ingredients_id are a complete match it will return a 1, or if the ingredients are greater than 0 it'll return a 2, and 3 for no matches. For Example, a query for User 1 will return a 1, User 2 a 2, and User 3 a 3. I'm not sure if this is something I'll have to code but I was told by someone that this is possible with little information, which led me here
Assuming you also have a user table, you could achieve this by cross-joining the user with the recipe_ingredients table and then left-joining the user_ingredients table, e.g.:
SELECT u.user_id, ri.recipe_id,
COUNT(ui.ingredient_id) AS available_ingredients, -- Number of ingredients the user has that are required to cook this recipe
COUNT(ri.ingredient_id) AS required_ingredients, -- Number of ingredients that are required to cook this recipe
CASE
WHEN COUNT(ui.ingredient_id) = COUNT(ri.ingredient_id) THEN 'can_cook'
WHEN COUNT(ui.ingredient_id) > 0 THEN 'has_some_ingredients'
ELSE 'has_no_ingredients'
END AS state
FROM users u
CROSS JOIN recipe_ingredients ri
LEFT JOIN user_ingredients ui ON(ri.ingredient_id = ui.ingredient_id AND u.user_id = ui.user_id)
GROUP BY u.user_id, ri.recipe_id
ORDER BY u.user_id, ri.recipe_id
If you want to limit it to a certain user / recipe, just use a where clause:
WHERE u.user_id = 1 AND ri.recipe_id = 'ifqvv'
You can try it live here: DB Fiddle
If you don't have an users table then you can replace
FROM users u with FROM (SELECT DISTINCT user_id FROM user_ingredients) u
DB Fiddle
I have a table of exercises. I want to be able to create a workout in my react app and then submit it to the database. but I'm struggling to think how the schema would work. how can I achieve this?
I essentially want a workout to be an array of exercises. But I am aware storing arrays in databases isn't the best idea
But I can't understand how if I had a table of exercises and then a table of workouts, how I can join them together to make a table where I have a list of workouts with exercises
I want to be able to pull out a unique workout and that will give me the list of exercises from that workout along with a date and other info.
The only way I can picture it is like:
id: 1, name: 'my first workout', exercises: [array of objects]
Any ideas how this schema would look?
For many-to-many relationships, you should have a third table, which stores the relationships, and is called a junction table. For example in your case, each row would have an exerciseID and a workoutID, which means that two (exercise and workout) are related.
So for example there can be many rows in that table with exerciseID = 1 (different workoutIDs) and many rows with workoutID = 5 (different exerciseIDs), hence many-to-many.
More explanation
Each row of the junction table has three fields:
Its own id (because it's good practice in database that each table has an id, no matter what)
ExerciseID
WorkoutID
So for example say we have a junction table like this:
ID ExerciseID WorkoutID
-------------------------------
1000 12 5
1001 12 6
1002 12 9
1003 12 12
1004 12 27
1005 13 2
1006 13 6
1007 13 9
which means that:
exercise number 12 has workouts [5, 6, 9, 12, 27]
exercise number 13 has workouts [2, 6, 9]
But as for how this table should be created:
Either you have a user interface that allows your users to add, edit, and delete exercises and workouts, or you do it your self. Either way, anyone that is defining an exercise or workout gotta know how they are related! So they can also define that relation as a row in the junction table