1

I have a SELECT statement, and I want to DELETE what I am selecting. When I change the SELECT to a DELETE, I get errors. How do I use the DELETE to delete what I selected?

This is my query:

SELECT course.course_id FROM course LEFT JOIN section ON section.course_id = course.course_id WHERE section.course_id IS NULL; 
2
  • 2
    What is the select query that you're using? Deleting should be in the format of DELETE table WHERE .... Ensure that you're not doing something like DELETE * FROM table WHERE ... Commented Feb 9, 2018 at 0:30
  • This is my query: SELECT course.course_id FROM course LEFT JOIN section ON section.course_id = course.course_id WHERE section.course_id IS NULL; Commented Feb 9, 2018 at 2:39

3 Answers 3

4

The DELETE keyword is used to delete an entire record. If I understand you correctly you are taking a SELECT {column} and trying to delete that column. If you want to clear the value in that specific cell use an UPDATE query.

update

I tried recreating your model course table

course_id course_name 1 First Course 2 Second Course 3 Third Course 

section table

section_id course_id section_name 1 1 First Section 2 1 Second Section 3 3 Third Section 

the delete query I used was

DELETE FROM course WHERE course.course_id NOT IN( SELECT section.course_id FROM section ); 
Sign up to request clarification or add additional context in comments.

9 Comments

I guess I'm selecting a specific column element but I need to delete that whole row.
Exactly, glad to be of assistance!
Thank you! But when I do this, it selects the correct row, but how do I delete it now?SELECT * FROM course WHERE course_id = (SELECT course.course_id FROM course LEFT JOIN section ON section.course_id = course.course_id WHERE section.course_id IS NULL);
I tried this but it said "You can't specify target table 'course' for update in FROM clause": DELETE FROM course WHERE course_id = (SELECT course.course_id FROM course LEFT JOIN section ON section.course_id = course.course_id WHERE section.course_id IS NULL);
Replace the initial 'SELECT *' with 'DELETE'
|
0

You need to use the delete keyword. The syntax for this is usually along the lines of:

delete from table_name where ... 

The ... in my example should match exactly what you have in your select statement.

You can find more info on this in my MySQL docs.

Comments

0

The DELETE statement has a WHERE clause. Use the WHERE clause of the SELECT statement as the WHERE clause for the DELETE statement.

for instance:

SELECT name FROM table WHERE id = 1; 

will have the following as its delete

DELETE FROM table WHERE id = 1; 

in case your select has no WHERE clause;

SELECT name FROM table; 

the statement becomes:

DELETE FROM table; 

The WHERE clause of the SELECT statement is the same as the WHERE clause for the DELETE statement.

NB: The second example will require that safe mode is disabled.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.