How do you apply multiple conditions to a leftjoin on a db_select?

柔情痞子 提交于 2019-12-09 18:58:45

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!