getSubQuery()
Returns the raw SQL string of a subquery instance. Use this method when you need full control over the subquery representation – for example when embedding it in a custom SQL expression or manual join.
Signature:
public function getSubQuery(): string
Example: Manual WHERE IN with custom SQL
// Build a subquery to fetch all active user IDs
$sub = $db->subQuery();
$sub->where('status', 'active');
$sub->get('test_users', null, 'id');
// Embed subquery manually into raw SQL
$sql = "SELECT * FROM test_orders WHERE user_id IN (" . $sub->getSubQuery() . ")";
$result = $db->rawQuery($sql);
Equivalent SQL:
SELECT * FROM test_orders
WHERE user_id IN (SELECT id FROM test_users WHERE status = 'active')
When to use getSubQuery()
- You need to embed a subquery manually inside raw SQL
- You want to debug or inspect the generated SQL
- You don’t want the subquery to be cast to string automatically
- You plan to reuse the same subquery in multiple places