joomla/joomla-platform — pull request #916 by gpongelli (2012-02-23 21:50:10+00:00)
Title: Added subquery capability for FROM and INSERT elements and their tests.

With this pull request it's possible to create queries as following examples.

Using subqueries inside FROM element

$q = new JDatabaseQuery ;
$subq = new JDatabaseQuery;
$subq->select('col2')->from('table')->where('a=1');
$q->select('col')->from($subq, 'alias');

This will be translated to

SELECT col
FROM (
SELECT col2
FROM table
WHERE a=1 ) AS `alias`

Now an INSERT INTO element example

$q = new JDatabaseQuery;
$subq = new JDatabaseQuery;
$subq->select('col2')->where('a=1');
$q->insert('table')->columns('col')->values($subq);

This will be translated to

INSERT INTO table
(col)
(
SELECT col2
WHERE a=1)

For each element is maintained backward compatibility about use without subqueries.