How to write the following query in orm using kohana
select * from u_question uq,users u where uq.asked_by=u.id and asked_by in (select u.id from fb_relations f,users u where f.u开发者_运维技巧ser1='515683059' and u.fb_uid=f.user2) group by asked_by
That'd be something like :
$subquery = DB::select('u.id')
->from(array('fb_relations','f'), array('users','u'))
->where('f.user1','=','515683059')
->where('u.fb_uid','=','f.user2');
$results = ORM::factory('user')
->select('uq.*')
->join(array('u_question','uq'))
->on('uq.asked_by','=','users.id')
->where('uq.asked_by','IN',$subquery)
->group_by('asked_by')
->find_all();
I can't guarantee this'll work. Notice that ORM models always select everything from the belonging table (e.g. table.*), adding a "usual" from() so it's a better practice to use "strict" JOINs instead of using from() for specifying the additional table.
And concerning subqueries, you can build them in this manner or directly with a string Database_Query object without the query builder (I wouldn't use it in this case).
You should read the docs
精彩评论