Main menu

FuelPHP - Nested queries using query builder

Published by dnovikov on Thu, 05/30/2013 - 10:16

Once I've needed to do a "WHERE IN (SELECT * ...) filter in my query. The intention was to use FuelPHP's query builder. After some googling, which gave no results, I've started some dumb experiements in code. Here is what I achieved, the solution seems to work perfect:

 

<?php

Core\DB::select('u.email', 'u.created', 'u.id')
    ->from(array('user', 'u'))
    ->where('u.id', 'not in',
        \DB::select('user_id')
        ->from('email_sales_log'));

?>

 

The thing was to put another select statement in the original one. You can achieve the same result by using DB::expr, but the nested solution is more elegant, and gives an opportunity to bind parameters to the nested query.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.