比较 Drupal 中的 2 个字段

发布于 2024-12-08 14:18:12 字数 484 浏览 1 评论 0原文

我想在 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 进行比较“。有什么方法可以告诉它比较列而不是列字符串?

I want to do a simple SQL query in Drupal, but I'm not sure how. I would like to achieve this:

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

I have the following code, which doesn't work:

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

The reason why it doesn't work is that it compares the column created with the string value "changed". Is there any way I can tell it to compare columns instead of column-string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兮子 2024-12-15 14:18:12

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

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

Use the where() member function to add conditions based on other fields in the table:

$query = db_select('node', 'n');
$query->addExpression("COUNT(nid)","i_count");
$query->where("created != changed");
$i_number_published = $query->execute()->fetchCol();
回忆凄美了谁 2024-12-15 14:18:12
$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)'] ; 
$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)'] ; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文