How do you build a query to get a series of nodes and their field values filtered by field values?
I second Jeremy's suggestion of trying harder with views. However, if you absolutely must write your query directly in code, you'll want to use EntityFieldQuery.

For example, to get a count of all nodes with a text field value of 'foo':

// Get field information
$field = field_info_field('field_name');
$query = new EntityFieldQuery;
$query-&gt;entityCondition('entity_type', 'node')
  -&gt;entityCondition('bundle', 'NODE TYPE')
  -&gt;fieldCondition($field, 'value', 'foo')
  -&gt;count();

$count = $query-&gt;execute();


If you don't want a count, but results instead, you would leave off the count part, and fetch results:

foreach ($query-&gt;execute() as $entity_type =&gt; $entities) {
  foreach ($entities as $entity_id =&gt; $entity) {
  ...
  }
}


The Commerce Guys also have a good blog post detailing a simple use case.