Forum Moderators: open
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]
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;
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]
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.
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.