mysql - How do I order query results by an associated model field with conditions? -


i've spent last 2 days trying find solution problem can either provide solution or link somewhere find out need know doing me huge favour.

i've got following model relationships in cakephp

keyword hasmany click 

i want construct query (either through $this->keyword->find or using custom query) return list of similar keywords ordered number of clicks they've received in last week. unfortunately can't use countercache 'click_count' field because need count clicks occurred in last week. further complicate things i've got add like() condition keyword field too.

here i've got far:

$result = $this->keyword->find('all',array(         'conditions' => array(             'word_count >' => $keyword['keyword']['word_count'],             'upper(keyword) like' => "%".strtoupper($keyword['keyword']['keyword'])."%"         ),         'recursive' => 0,         'limit' => 10     )); 

i need add bit sorts these results number of associated click records click.created within last week. i've worked out that part should like:

array(         'conditions' => array(             'click.created >' => date("y-m-d",strtotime("1 week ago"))         ),         'fields' => array(             'count(click.keyword_id) count'         ),         'group' => 'click.keyword_id',         'order' => 'count desc'     ); 

i'm not sure bit should go.

please put me out of misery? :)

well, managed blag sql query needed page: http://www.experts-exchange.com/web_development/web_languages-standards/asp/q_20646898.html

here looks like:

select *         keywords         keyword         left join (select keyword_id,count(*) click_count clicks group keyword_id)         click         on keyword.id = click.keyword_id         keyword.word_count > ".$keyword['keyword']['word_count']."         , upper(keyword.keyword) '%".strtoupper($keyword['keyword']['keyword'])."%'         order click.click_count desc 

hopefully else find useful.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -