Welcome Guest, Not a member yet? Register   Sign In
improve Query Builder to make more complex queries
#4

(10-16-2016, 08:20 PM)kilishan Wrote: For sub-queries, I believe Query Grouping already handles that, even in CI. That aside, I don't understand how this example would work. You're comparing a field with a partially generated query. How would it know what you're expecting to compare it to?

Grouping queries looks ugly. I want to suggest using an anonymous function.

PHP Code:
$builder->where(function(BaseBuilder $builder) {
 
  return $builder->where('a''a')
 
      ->orWhereNot(function(BaseBuilder $builder) {
 
           return $builder->where('b''b')->where('c''c');
 
      });
})->
orWhereNot('d''d');

// SELECT * FROM `test` WHERE ( `a` = 'a' OR NOT ( `b` = 'b' AND `c` = 'c' ) ) OR `d` != 'd' 

Subqueries?

PHP Code:
$builder->where('key', function(BaseBuilder $builder) {
 
   return $builder->where('b''b')->select('key')->from('test_2');
});

// SELECT * FROM `test` WHERE key = (SELECT `key` FROM `test_2` WHERE `b` = 'b')

$builder->whereIn('key', function(BaseBuilder $builder) {
 
   return $builder->where('b''b')->select('key')->from('test_2');
});
// SELECT * FROM `test` WHERE `key` IN (SELECT `key` FROM `test_2` WHERE `b` = 'b') 

it's easy

Attached Files
.php   BaseBuilder.php (Size: 71.71 KB / Downloads: 49)
Reply


Messages In This Thread
RE: improve Query Builder to make more complex queries - by iRedds - 05-10-2019, 12:54 AM



Theme © iAndrew 2016 - Forum software by © MyBB