Different ways to update table based on another table column
1. Updates students table based on notes table column -
notes.id
using
Implicit JOIN
UPDATE notes, students
SET points = points+1
WHERE notes.student_id = students.id
	AND notes.note = 'My first note'
2. Updates students table based on notes table column -
notes.id
using
explicit JOIN
UPDATE notes
INNER JOIN students ON notes.student_id = students.id
SET students.points = students.points + 1
WHERE notes.id = 1
3. Updates students table based on notes table column -
notes.id
using sub-query
UPDATE students 
SET points = points + 1 
WHERE student_id = (
        SELECT student_id 
        FROM notes 
        WHERE notes.id = 1
    )
4. Updates students table based on notes table column -
notes.id
using
VIEW
First we need to create the VIEW
DROP VIEW IF EXISTS student_notes;

CREATE VIEW student_notes AS
SELECT 
    students.first_name,
    students.last_name,
    students.points,
    notes.*
FROM students
JOIN notes ON notes.student_id = students.id
Then we update the VIEW as a regular table
UPDATE `student_notes` 
SET points = points + 1 
WHERE id = 1;
5. Alternative of the above four updates is to run two completely different queries:
SELECT student_id FROM `notes` WHERE id = 5;
UPDATE students SET points = points + 1 WHERE id = 3;
by Valeri Tandilashvili
4 years ago
MySQL
UPDATE
1
Pro tip: use ```triple backticks around text``` to write in code fences