Drupal - 查询计算 content_type "A" 的所有节点,其中 field_b 是 "X"

Drupal - Query to count all nodes of content_type "A" in which field_b is "X"

我需要查询一个 drupal 站点。最初我只需要计算一个内容类型的节点数:

$query = "SELECT COUNT(*) amount FROM {node} n WHERE n.type ='A'";
$result = db_query($query)->fetch();
return $result->amount;

现在我需要它,但仅适用于 field_b 等于 'X' 的节点。我该怎么做?

I tried EntityFieldQuery without sucess:

$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'A')
  ->fieldCondition('field_b', 'value', 'X');

$results = $query->execute();
return $results->amount;

有什么帮助吗?

您可以将查询设置为仅计数查询,方法是:

$count = $query->count()->execute();