diff --git a/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-DELETE-statement.sql b/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-DELETE-statement.sql new file mode 100644 index 00000000..648689c2 --- /dev/null +++ b/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-DELETE-statement.sql @@ -0,0 +1,36 @@ + +-- This statement generates the Error 1093 +DELETE FROM Program +WHERE start_date > +(SELECT MIN(start_date) FROM Program); + +-- This statement fixes the Error 1093 in the preceding example using a derived table +DELETE FROM Program +WHERE start_date > ( + SELECT subquery_program.start_date + FROM ( + SELECT MIN(start_date) AS start_date FROM Program + ) AS subquery_program +); + + +-- This set of statements fixes the Error 1093 in the example using a temporary table +- Create a temporary table +CREATE TEMPORARY TABLE temp_min_start_date AS +SELECT MIN(start_date) AS min_date +FROM Program; + +-- Delete from the main table using the temporary table +DELETE FROM Program +WHERE start_date > +(SELECT min_date FROM temp_min_start_date); + +-- Drop the temporary table +DROP TEMPORARY TABLE temp_min_start_date; + +-- This statement fixes the Error 1093 using a join with a derived table +DELETE p +FROM Program AS p +JOIN (SELECT MIN(start_date) AS min_start_date FROM Program) +AS min_program_date +ON p.start_date > min_program_date.min_start_date; diff --git a/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-UPDATE-statement.sql b/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-UPDATE-statement.sql new file mode 100644 index 00000000..6c544df0 --- /dev/null +++ b/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-UPDATE-statement.sql @@ -0,0 +1,36 @@ +-- This statement generates the Error 1093 +UPDATE Program t +SET t.end_date = (SELECT start_date FROM Program WHERE id=t.id); + +-- This statement fixes the Error 1093 in the preceding example using a derived table +UPDATE Program +SET end_date = ( + SELECT max_start_date + FROM ( + SELECT MAX(start_date) AS max_start_date + FROM Program + ) AS derived_table +); + +-- This set of statements fixes the Error 1093 in the same example using a temporary table +-- Create a temporary table +CREATE TEMPORARY TABLE +IF NOT EXISTS temp_table +AS SELECT id, start_date +FROM Program; + +-- Update the main table using the temporary table +UPDATE Program t +JOIN temp_table temp +ON t.id = temp.id +SET t.end_date = temp.start_date; + +-- Drop the temporary table +DROP TEMPORARY TABLE +IF EXISTS temp_table; + +-- This statement fixes the Error 1093 using a self-join +UPDATE Program p1 +JOIN Program p2 +ON p1.id = p2.id +SET p1.end_date = p2.start_date;