“count”및 “having”속성으로 Drupal 7 쿼리를 작성하는 방법은 무엇입니까? 작동시킬 수 없습니다.

drupal7 표준에서 다음 쿼리를 작동시킬 수 없습니다. 누구든지 이것을 도울 수 있습니까? 조금 시급하다 …

SELECT n.nid AS nid, n.title AS title, count(n.title) AS ncount
FROM node n
INNER JOIN taxonomy_index tn ON n.nid = tn.nid
WHERE (n.type = 'test')
AND (tn.tid IN( 23,37))
AND (n.title LIKE '%name%')
AND (n.status = 1)
GROUP BY n.nid
HAVING ncount = 2


답변

이것은 내 머리 꼭대기에서 떨어져 있으므로주의하십시오 …

$query = db_select('node', 'n')
  ->fields('n', array('nid', 'title'))
  ->condition('n.type', 'test')
  ->condition('tn.tid', array(23, 37))
  ->condition('n.title', '%' . db_like('name') . '%', 'LIKE')
  ->condition('n.status', 1)
  ->groupBy('n.nid');

// Add the COUNT expression
$query->addExpression('COUNT(n.title)', 'ncount');

// Add the HAVING condition
$query->havingCondition('ncount', 2);

// Add the JOIN
$query->join('taxonomy_index', 'tn', 'n.nid = tn.nid');

$results = $query->execute();

답변