web-dev-qa-db-ja.com

Drupal 7クエリに「count」と「having」プロパティを指定する方法は?

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
14
Jons

これは頭​​の上からずれているので注意してください...

$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();
25
Clive