问题
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'
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'");
回答1:
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'");
回答2:
Here some multiple join conditions with an IN condition inside the join with implode PHP array to get correct results.
$query->leftJoin('element_item','ei','ei.id = ei.id2 AND (ei.param = '.$entity_param.' AND ei.id IN ('.implode(',',$records).'))');
来源:https://stackoverflow.com/questions/8695993/how-do-you-apply-multiple-conditions-to-a-leftjoin-on-a-db-select