如何在db_select上将多个条件应用于一个leftjoin? [英] 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上将多个条件应用于一个leftjoin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!