比较Drupal中的2个字段

时间:2011-10-06 10:36:36

标签: mysql drupal drupal-7

我想在Drupal中做一个简单的SQL查询,但我不确定如何。我想实现这个目标:

SELECT COUNT(nid) AS i_count FROM node WHERE `created` != `changed`;

我有以下代码,但不起作用:

$query = db_select('node', 'n');
$query->addExpression("COUNT(nid)","i_count");
$query->condition("created","changed","!=");
$i_number_published = $query->execute()->fetchCol();

它不起作用的原因是它将列created与字符串值"changed"进行比较。有什么方法可以告诉它比较列而不是列字符串吗?

2 个答案:

答案 0 :(得分:7)

使用where()成员函数根据表格中的其他字段添加条件:

$query = db_select('node', 'n');
$query->addExpression("COUNT(nid)","i_count");
$query->where("created != changed");
$i_number_published = $query->execute()->fetchCol();

答案 1 :(得分:0)

$result_handler = db_query("select count(nid) from {node} where `created` != `changed`") ; 

$result_arr = db_fetch_array($result_handler) ; 

$number_of_nodes = $result_arr['count(nid)'] ;