How to exclude nodes with matching field values using EntityFieldQuery This relates to a classified listings site, where nodes have an expiry date (date, field_period, start/end = value/value2) and a notifications field (text, checkboxes, 336=two weeks, 672=four weeks).

We want to query for nodes which are going to expire between two and four weeks from now -

$period = 672;
$paid_date_max = date('Y-m-d H:i', strtotime('+' . $period . ' hours'));
$paid_date_min = date('Y-m-d H:i', strtotime('+' . $period/2 . ' hours'));

$query = new EntityFieldQuery();
$query-&gt;entityCondition('entity_type', 'node')
  -&gt;entityCondition('bundle', 'entry')
  -&gt;propertyCondition('status', 1)
  -&gt;fieldCondition('field_period', 'value2', $paid_date_max, '&lt;')
  -&gt;fieldCondition('field_period', 'value2', $paid_date_min, '&gt;=')
  -&gt;fieldCondition('field_period', 'value2', 'NULL', '!=')
  -&gt;addMetaData('account', user_load(1));
$result = $query-&gt;execute();


That works fine, but includes nodes which have already been sent a reminder (in which case field_expiry_notifications will contain a value matching $period). field_expiry_notifications may have multiple values, so we need to accept only nodes where NONE of the field_period_notifications values match $period.

Q: How to exclude from the above resultset any nodes with a matching expiry_notifications value?
I believe you do need a subquery here. Apparently, you can actually do that with EntityFieldQuery. So first create your subquery, something like this:

$subquery = db_select('your_field_data_table', 't');
$subquery-&gt;addField('t', 'nid');
$subquery-&gt;condition('field_period_notifications', $period);


That gives you all nodes that have a matching value. Then add this to your EntityFieldQuery to exclude those:

-&gt;fieldCondition('field_period', 'nid', $subquery, 'NOT IN')


Or maybe you can do:

-&gt;entityCondition('entity_id', $subquery, 'NOT IN')