Deletion of some watchdog messages with subquery What i want is to delete from watchdog the first 20 messages with type as 'blabla' what i did untill now is:

 $query = db_select('watchdog', 'w')
    -&gt;fields('w')
    -&gt;condition('type', 'blabla')
    -&gt;range(0, 250);
  $subquery = $query-&gt;execute()-&gt;fetchCol('wid');

  if ($subquery != NULL) {

    db_delete('watchdog')
      -&gt;condition('wid', $subquery, 'IN')
      -&gt;execute();
  }


it works fine but i want to do it with subquery like:

$subquery = db_select('watchdog', 'w')
    -&gt;fields('w', array('wid'))
    -&gt;condition('type', 'blabla')
    -&gt;range(0, 250);

  // Deletes the previous selected values.
  db_delete('watchdog')
    -&gt;condition($subquery, 'wid')
    -&gt;execute();


it gives me the error:

General error: 1093 You can't specify target table 'watchdog' for update in FROM clause


Any ideas how to solve this problem?
You were close; this should work for you:

$subquery = db_select('watchdog', 'w')
  -&gt;fields('w', array('wid'))
  -&gt;condition('type', 'blabla')
  -&gt;range(0, 250)
  -&gt;execute()
  -&gt;fetchCol('wid');

// Deletes the previous selected values.
db_delete('watchdog')
  -&gt;condition('wid', $subquery, 'IN')
  -&gt;execute();


You weren't executing the subquery (meaning there were no results), and you mixed up the order of arguments passed to query condition.