如何编写具有“计数”和“具有”属性的Drupal 7查询?


14

我无法在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

Answers:


25

这不在我的脑海中,请谨慎使用...

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

谢谢,克莱夫!另一个常见元素:按我添加的计数进行排序$query->orderBy('ncount', 'DESC');
让人讨厌的是,
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.