1

I want to select table entries by id that are not in an other table. Is used the following code:

$table2 = new OfferTable();
$select = $table2->select()->from('offers', array('aId as id'))
              ->where('mem_id =?', $memId);

$table = new otherTable();
$select2 = $table->select()->where('id NOT in (?)', $select);
$result = $table->fetchAll($select);

This works fine, but is very slow when OfferTable has a lot of entries and the first select selects only a few entries.

So I tried the following:

$table2 = new OfferTable();
$select = $table2->select()->from('offers', array('aId as id'))
              ->where('mem_id =?', $memId);
$fetch = $table2->fetchAll($select);
$id_array = array();
foreach($fetch as $value){
    $id_array[] = $value->id;
}

$table = new otherTable();
$select2 = $table->select()->where('city = ?', $city)
              ->where('id NOT in (?)', $id_array);
$result = $table->fetchAll($select);

This seems to work much faster, but sometimes I get the following error message:

SQLSTATE[42000]: Syntax error or access violation: 1064 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 ')) AND (id NOT IN ('3956', '3821'))' at line 1

I don't understand why I get this error and especially why I don't get it all the time. Thanks for your help!

1
  • 1
    View the query and try to run it in phpmyadmin. Commented Aug 5, 2013 at 9:53

1 Answer 1

1

I think I solved the problem:

In the original code I used two ->where('id NOT in (?)', $id_array) statements. The error occured when one of the arrays was empty.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.