I have 4 tables in a mysql database that i need to add/edit/manage.
The difficulty, is that each one, is dependent on the one before it.
The way i have this setup on the user end, is you select an option from table 1.
You are then presented with the options in table 2, that have the first option's ID in their row.
Once you select option in table 2, you are taken to table 3, which generates its list where the rows contain the ID of your selection in table 2, and so on and so forth.
The problem is how to edit/manage this.
Obviously if you delete an option in table 1, all its subsequent child options will be invalid.
I just need an idea of how to manage a setup like this.
Without many details it's hard to comment on your exact situation, but here's a way to organize your MySQL database structure:
TABLE table1
INT id
... other data ...
TABLE table2
INT id
INT table1_id FOREIGN_KEY table1
... other data ...
TABLE table3
INT id
INT table2_id FOREIGN_KEY table2
... other data ...
TABLE table4
INT id
INT table3_id FOREIGN_KEY table3
... other data ...
And your url structure for your site could be:
/table1/add
/table2/add?table1_id=T1_ID
/table3/add?table2_id=T2_ID
/table4/add?table3_id=T3_ID
/table1/(edit,delete)/T1_ID
/table2/(edit,delete)/T2_ID
/table3/(edit,delete)/T3_ID
/table4/(edit,delete)/T4_ID
For adding to table2,3,4:
INSERT INTO table2 (data, table1_id) VALUES(data, T1_ID);
For deleting from table1:
DELETE FROM table1 WHERE id = T1_ID;
$t2_ids = SELECT id FROM table2 WHERE table1_id = T1_ID;
DELETE FROM table2 WHERE table1_id = T1_ID;
$t3_ids = SELECT id FROM table3 WHERE table2_id IN ($t2_ids);
DELETE FROM table3 WHERE table2_id = $t2_ids;
$t4_ids = SELECT id FROM table4 WHERE table3_id IN ($t3_ids);
DELETE FROM table4 WHERE table3_id = $t3_ids;
And so and so forth if you're deleting from the sub tables.
Additionally, if your data in each table isn't very different, you can use a single table to maintain the parent/child relationships
TABLE table
INT id
INT parent_id
... other data ...
Your URL structure won't change much, but your deleting pseudo-code can be optimized to use subselects on the table itself.
Multi-tiered data is most-often stored in with a parent:child relation field in a table.
id | name | parent_id
----------------------
1 | Mom | NULL
2 | Son | 1
Write a recursive function/query to traverse the relationships and any updates/inserts/deletes should be relatively simple.
Related
I have a Quiz module in which I have two tables quiz_question and quiz_options. quiz_question is saving Questions
and quiz_option saving options for particular questions.
table structure for quiz_option:
id | question_id | text | is_correct
table structure for quiz_question
id | title | desctiption |
Where question_id is foreign key to id of quiz_question
I want to write a query to update quiz_question and all its corresponding quiz_options is a single query.
Multiple table update you can use foreign key connect two and more table with foreign key concept update parent table primary key to child table foreign key see link
http://www.hostingadvice.com/how-to/mysql-foreign-key-example/.
table quiz_question PK (question_id)
table quiz_option FK (question_id)
Get the id of the question and Update them one by one as you cannot update two tables using a single query.
$ID_VALUE = mysqli_real_escape_string($conn, $_GET['id']);
UPDATE quiz_option SET fields_name = 'value' WHERE question_id = $ID_VALUE;
UPDATE quiz_question SET fields_name = 'value' WHERE id = $ID_VALUE;
Here is the query:
INSERT INTO quiz_question(title,description) VALUES ('test','test question');
INSERT INTO quiz_option(question_id,text,is_correct) VALUES (LAST_INSERT_ID(),'test','0'),(LAST_INSERT_ID(),'test2','0'),(LAST_INSERT_ID(),'test3','0'),(LAST_INSERT_ID(),'test4','1')
try this
UPDATE table1 a
INNER JOIN table2 b
ON a.ID = b.ID
SET a.value = b.value
I am trying to SELECT rows from a table where it has got a foreign key on second table and the key from the second table is used in a third table as foreign key. How can I retrieve the rows from the first table where on the third table doesn't have the key from the second table which this particular key's row has got the first tables' row's primary key as foreign key and also on the third table it hasn't got the foreign key of the fourth table.
I tried using inner join but it only works with SELECTING rows where it has got a specify value rather than not having this specify value.
Please help me
Faculty table
FaculID | FaculName | FaculLocation |
Course table
CourseID | CourseName | CourseDescription
FacultyCourse table
fcID | CourseID | FaculID
Registeration table
RegID | fcID | stuID
Student table
stuID | stuName | stuAge | stuAddress
so basically what i want to do now is get all bid where the e table hasn't got uid and did(which has got a bid foreign key).
Try to use LEFT OUTER JOIN:
SELECT a.*
FROM a
LEFT OUTER JOIN b on a.keya = b.keybjoina
LEFT OUTER JOIN c on b.keybjoina = c.keycjoinb
WHERE c.keycjoinb IS NULL
Try:
(change table and column names as appropriate)
updated based on example layout
select *
from faculty
where faculid not in (select faculty.FaculID
from faculty
join facultycourse
on faculty.FaculID = FacultyCourse.FaculID
join registration
on registration.fcid = FacultyCourse.fcid
where registration.stuid = 'XYZ');
I have 2 tables with the same columns.
for example
table 1
id
name
height
weight
table 2
id
name
height
weight
The data is table 2 is complete. But in table 1, only some data exists, and the rest of the columns are empty. for example:
table 1 table 2
id 4 4
name (empty) salman
height 5'11" 5'9"
weight (empyy) 65kg
I want a single script, that will allow me to update the table 2 with values from table 1, but only where it exists. In places where the table 1 is empty, I want to retain the data that already exists in table 2.
I've tried various ways, but all required multiple queries and are long and hectic. I want to know if there is a simpler way to get this done? Preferably in a single query?
Thank you
You can try by joining the 2 tables and then using the CASE keyword to conditionally update the fields:
UPDATE table2 t2 INNER JOIN table1 t1 USING (id)
SET
t2.name = CASE WHEN (t1.name IS NULL) THEN t2.name ELSE t1.name END
t2.height= CASE WHEN (t1.height IS NULL) THEN t2.height ELSE t1.height END
t2.weight = CASE WHEN (t1.weight IS NULL) THEN t2.weight ELSE t1.weight END
http://dev.mysql.com/doc/refman/5.5/en/case-statement.html
http://dev.mysql.com/doc/refman/5.5/en/update.html
I am trying to fire a query which should give me result from multiple table.... I am doing JOIN in zend framework 1.10.0 ... but the problem is that i am having tables which are connected as a branch.
for example
Table 1 (T1 PK)
Table 2 (T2 PK, T1 FK)
Table 3 (T3 PK, T1 FK)
Table 4 (T4 PK, T2 FK)
Table 5 (T5 PK, T1 FK)
Now, i am able to join Table1, with Table2, Table3 & Table5, but the problem is what should i do with Table4, bcoz i want data from that table also... How can I make a query which can do Branch JOINS ... Working on this from 2 days ... Plz help me frnds ... Thanks In Advance
I haven't tested it but it should be ok.
When joining tables you must must specify the conditional expression of the join. So you just have to reference the right table when joining T4
$dbTable = new Aplication_Model_DbTable_T1();
$select = $dbTable->select()
->setIntegrityCheck(false)
->from(array("t1" => "table1"), array(t1 columns to fetch...))
->join(array("t2" => "table2"), "t2.t1 = t1.id",array(t2 cols to fetch))
->join(array("t3" => "table3"), "t3.t1 = t1.id",array(t3 cols to fetch))
->join(array("t4" => "table4"), "t4.t2 = t2.id",array(t4 cols to fetch))
->join(array("t4" => "table4"), "t5.t1 = t1.id",array(t4 cols to fetch))
->where(...);
Ok, I have this first table which has, among other things:
table 1: id | depID (every id has one depID)
Then, I have a second table where I have table 2: userID | depID (where an userID is associated with multiple depIDs in separate rows. Also, I have table 3 with userID | rankID (where an userID is associated with one rankID).
I need to get all id and depID from table 1, and then to check, which userIDs of table 2 shares the same depID (table1.depID = table2.depID), and then, to check which of those userIDs from table 2 has rankID = $rID
Thanks guys.
I think this SQL should get you what you want, but I'm not 100% clear from the wording of the question:
SELECT table2.userID
FROM table1
JOIN table2
ON table1.depID = table2.depID
JOIN table3
ON table2.userID = table3.userID
AND table3.rankID = $rID;