SELECT
(REPEAT('abc-123', 10) = UNCOMPRESS(COMPRESS(REPEAT('abc-123', 10)))) comparison
70
bytes and compressed string takes 22
bytes SELECT
REPEAT('abc-123', 10) full_string,
LENGTH(REPEAT('abc-123', 10)) full_string_length,
COMPRESS(REPEAT('abc-123', 10)) compressed_string,
LENGTH(COMPRESS(REPEAT('abc-123', 10))) compressed_string_lengtha
#1356 - View 'university.student_notes' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
In this case we need to drop and recreate the VIEWDROP 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
- UNION
- DISTINCT
- HAVING
- LEFT / RIGHT JOINs
- Sub-queries
- GROUP BY
- Aggregate Functions
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-queryUPDATE 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 VIEWDROP 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 tableUPDATE `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;
student_notes
, increases author's points by one in students
table based on note's ID.
First we need to create the VIEWCREATE VIEW IF NOT EXISTS 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 tableUPDATE `student_notes`
SET points = points + 1
WHERE id = 1;
VIEW
called student_notes
that selects all notes with their authorsCREATE VIEW IF NOT EXISTS student_notes AS
SELECT
students.first_name,
students.last_name,
notes.*
FROM students
JOIN notes ON notes.student_id = students.id
After creating the VIEW
then we can use it as regular table like this example:SELECT *
FROM student_notes
full_name
that concatenates the two parameters and returns the resultDROP FUNCTION IF EXISTS full_name;
CREATE FUNCTION full_name ( first_name VARCHAR(100), last_name VARCHAR(100) )
RETURNS VARCHAR(210)
RETURN CONCAT(first_name, ' ', last_name);
After creating the function we can call it:SELECT
full_name('John', 'Swift');
DATE_FORMAT
returns formatted version of the datetime string passed as the first parameterSELECT
DATE_FORMAT('2022-03-25 18:24:14', '%Y-%m-%d %H:%i:%s')
In this example we only need to get month and day with underscore between the valuesSELECT
DATE_FORMAT('2022-03-25 18:24:14', '%m_%d')