Forum Moderators: open

Message Too Old, No Replies

Delete Syntax in MySQL

         

Ninjabear

3:29 pm on May 3, 2007 (gmt 0)

10+ Year Member



Could someone please tell me what is wrong with my syntax here? It works as a select, but not as a delete.

I was hoping to delete everything from linked tables in one query, rather than having to delete all of the lowermost dependencies, working up to the table I want to delete, is this possible?

delete from qnaires q, questions qu, sub_questions sq,
options o, options_answers oa, respondents r
where q.qnaire_id=qu.qnaire_id
and qu.q_id=sq.q_id
and sq.sub_question_id=o.sub_question_id
and o.option_id=oa.option_id
and r.qnaire_id=q.qnaire_id
and q.qnaire_id=15;

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where q.qnaire_id=qu.qnaire_id
and qu.q_id=sq.q_id
and sq.sub_question_id=o.su' at line 3

This would suggest there was a comma wrong somewhere, but the where looks fine to me.

[edited by: Ninjabear at 3:37 pm (utc) on May 3, 2007]

phranque

11:15 pm on May 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you could try something like this:

delete from qnaires, questions, sub_questions,
options, options_answers, respondents
using qnaires q, questions qu, sub_questions sq,
options o, options_answers oa, respondents r
where q.qnaire_id=qu.qnaire_id
and qu.q_id=sq.q_id
and sq.sub_question_id=o.sub_question_id
and o.option_id=oa.option_id
and r.qnaire_id=q.qnaire_id
and q.qnaire_id=15;

Ninjabear

8:03 am on May 4, 2007 (gmt 0)

10+ Year Member



What does the 'using' keyword do?

I can't look it up on any search engine because it tends to think I mean the english word using, so I get "using sql to...".

I did got an error when I ran the query (Unknown table 'qnaires' in MULTI DELETE), aparently you need the aliases on both the select and the using.

[edited by: Ninjabear at 8:25 am (utc) on May 4, 2007]

Demaestro

3:02 pm on May 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



MySql may not allow you to call delete on more then one table at a time.

Try this:

delete from qnaires
where qnaire_id in (select qu.qnaire_id
from
questions qu, sub_questions sq,
options o, options_answers oa, respondents r and qu.q_id=sq.q_id
and sq.sub_question_id=o.sub_question_id
and o.option_id=oa.option_id
and r.qnaire_id=q.qnaire_id
and q.qnaire_id=15);

You will have to have one delete statement per table you want to delete from. Check the MySql documentation about multi table delete statements. you might be able to do it.... I don't use mySql a lot so I am not sure on this point.

Demaestro

5:12 pm on May 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you are going to do this in multiple delete statements make sure it is all done in one transaction. That way if you delete any data that you are using to draw relations that the relations will remain in tact until the entire delete statement is executed.

Otherwise you could loose relations and then you will be left with data that doesn't relate to anything but can't be looked up in a generic query.