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
Related
I have two data tables
'wp_balance' table with column Balance, and userid
Another table:
'wp_users' table with column userid and referredby
userid is common in both table where referredby is a different userid. I need to update referredby members Balance in wp_balance table where wp_balance.userid=wp_users.userid.
$sql = "UPDATE wp_balance SET balance = $sum FROM wp_balance JOIN wp_users
ON wp_balance.userid = wp_users.userid;"
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 a database with two tables. When a user posts an article, it will be inserted into both tables, (2 queries in one file)
I use post_id as foreign key, both tables post_id auto increment. Will foreign keys be messed up? For example if users A and B query the database at the same time.
Table 1
post_id user...
1 A
2 B
Table 2
post_id content...
1 A
2 B
First off you can't have auto increment on both tables.
Usually, what you do is insert in table 1, get the ID of the just inserted row.
Then you use this ID, to insert in table 2 which references table 1.
See: mysqli::$insert_id at
http://www.php.net/manual/en/mysqli.insert-id.php
Example:
$query = "INSERT INTO table1(user,whatever) VALUES ('A','something')";
$mysqli->query($query);
printf ("New Record has id %d.\n", $mysqli->insert_id);
$query = "INSERT INTO table2(post_id,content) VALUES ($mysqli->insert_id,'This is content')";
$mysqli->query($query);
You could also do this using a stored procedure based on: stackoverflow.com/a/1723325/1688441
DELIMITER //
CREATE PROCEDURE new_post_with_content(
user_id CHAR(5), content_text CHAR(100)
BEGIN
START TRANSACTION;
INSERT INTO table1 (user)
VALUES(user_id);
INSERT INTO table2 (post_id, content)
VALUES(LAST_INSERT_ID(), content_text);
COMMIT;
END//
DELIMITER ;
And you call it like so:
CALL new_engineer_with_task('A','This is the content');
Why not use table1 as user table and second with posts?
users
user_id(autoinc) username
1 A
2 B
3 C
posts
post_id(autoinc) user_id posts_text
1 2 text
2 1 other text
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;
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.