当前位置: 首页>>技术解读>>正文


连接并查找NULL值

webfans 技术解读 , , 去评论

问题描述

我正在使用标记模块让用户将他们的问题标记为已解决,类似于它在此网站上的工作方式。因此,我想让用户过滤帖子以仅显示未解决的问题。此外,一些帖子可能根本不是问题。因此,它们不能具有”solved”标志,但在仅通过未解决的问题进行过滤时,它们也不应出现在结果中。因此,我需要将节点表与另外两个连接起来:flag_contentfield_data_field_question(后一个表是告诉帖子是否是一个问题)。

这是我正在尝试的当前代码:

$query->join('flag_content', 'f', 'f.content_id = n.nid AND f.content_type = "node" AND f.fid = 5');
$query->join('field_data_field_question', 'q', 'q.entity_id = n.nid AND q.field_question_value = 1');
$query->condition('f.fid', 'NULL', 'IS');
$query->condition('q.field_question_value', 'NULL', 'IS NOT');

但是,这会导致以下错误:

PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''NULL') AND (q.field_question_value IS NOT 'NULL')
LIMIT 21 OFFSET 0' at line 4: SELECT n.nid AS nid
FROM
{node} n
INNER JOIN {field_data_field_category} t ON t.entity_id = n.nid
INNER JOIN {flag_content} f ON f.content_id = n.nid AND f.content_type = "node" AND f.fid = 5 
INNER JOIN {field_data_field_question} q ON q.entity_id = n.nid AND q.field_question_value = 1
WHERE (t.field_category_tid = :db_condition_placeholder_0) AND (f.fid IS :db_condition_placeholder_1) AND (q.field_question_value IS NOT :db_condition_placeholder_2)
LIMIT 21 OFFSET 0; Array 
( [:db_condition_placeholder_0] => 464 [:db_condition_placeholder_1] => NULL [:db_condition_placeholder_2] => NULL
)
in queryExecuteRender_recentActivity() (line 57 of someFile.php).

这不是将NULL值传递给查询的正确方法吗?

最佳解决方案

数据库API实际上有一种添加此类表达式的首选方法,即SelectQuery::isNull()SelectQuery::isNotNull()函数。您可以像这样使用它们:

$query->isNull('f.fid');

$query->isNotNull('q.field_question_value');

参考资料

本文由朵颐IT整理自网络, 文章地址: https://duoyit.com/article/3014.html,转载请务必附带本地址声明。