如何将多个条件应用于 db_select 上的左连接? [英] How do you apply multiple conditions to a leftjoin on a db_select?
问题描述
我正在尝试加入每个节点的 Fivestar 评级,以便按评级对它们进行排序.问题是我查询的 Fivestar 值作为每个节点的多行存储在votingapi_cache 中(投票数和平均评分),所以我想做以下事情:
I'm trying to join the Fivestar ratings for each node so I can sort them by rating. The problem is the Fivestar values I'm querying on are stored in votingapi_cache as multiple rows per node (vote count and the average rating), so I want to do the following:
...
LEFT JOIN votingapi_cache fivestar
ON fivestar.entity_id = node.nid
AND fivestar.function = 'average'
我试过下面的代码,但问题是对fivestar.function = 'average'"的检查被添加到 where 子句中,它消除了所有没有投票的节点.
I've tried the following code, but the problem is the check on "fivestar.function = 'average'" is added to the where clause which eliminates all nodes that have no votes.
$query->leftjoin('votingapi_cache', 'fivestar', 'fivestar.entity_id = n.nid');
$query->condition('fivestar.function', 'average', '=');
$query->addField('fivestar', 'value', 'average_rating');
<小时>
好的,我第一次尝试时肯定做错了什么,但这是解决方案:
Ok, the first time I tried this I must have done something wrong, but here's the solution:
$query->leftjoin('votingapi_cache', 'fivestar', "fivestar.entity_id = n.nid AND fivestar.function = 'average'");
推荐答案
好吧,我第一次尝试这个我一定做错了,但这里是解决方案:
Ok, the first time I tried this I must have done something wrong, but here's the solution:
$query->leftjoin('votingapi_cache', 'fivestar', "fivestar.entity_id = n.nid AND fivestar.function = 'average'");
这篇关于如何将多个条件应用于 db_select 上的左连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!