Note that there are some explanatory texts on larger screens.

plurals
  1. POIn MySQL, how can I do a DELETE based on the result from a table where I did an INNER JOIN with the same table?
    primarykey
    data
    text
    <p>OK, so I do suck at MySQL, but here is basically what I want to do:</p> <pre><code>delete from course_plan_relationships where course_plan_relationships.id not in ( select course_plan_relationships.id from daily_plans inner join course_plan_relationships on daily_plans.id=course_plan_relationships.daily_plan_id ); </code></pre> <p>To give you an idea of what's happening, I'll show you the subquery and its result:</p> <pre><code>mysql&gt; select course_plan_relationships.id from daily_plans inner join course_plan_relationships on daily_plans.id=course_plan_relationships.daily_plan_id; +----+ | id | +----+ | 1 | | 13 | +----+ </code></pre> <p>So basically, I want to delete all items in course_plan_relationships, where its id field is not in that table I generated there in the subquery. </p> <p>The error I get is:</p> <blockquote> <p>ERROR 1093 (HY000): You can't specify target table 'course_plan_relationships' for update in FROM clause</p> </blockquote> <p>What I've basically gotten is that for some reason MySQL won't let you DELETE or UPDATE based on a sub-query involving the same table. </p> <p>That's fine, and here's a supposed workaround: <a href="http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/" rel="nofollow">http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/</a></p> <p>But its for UPDATE and doesn't use the "in" syntax. </p> <p>I'm really new to this stuff so any help would be super appreciated. I haven't had any luck using the "AS blahothertablename" kind of syntax (keep getting syntax errors), and I also can't figure out how to store the initial subquery as a temporary result (again, syntax errors). </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload