Skip to content

SQL-503 How to interpret and fix the MySQL Error 1093 #362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-- 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 t
SET t.end_date = (
SELECT subquery_program.start_date FROM (
SELECT id, start_date FROM Program WHERE id=t.id
) AS subquery_program
);

-- 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;