2

Can anyone point me to the documentation on how to use UnionAll in JDatabaseQuery? Thanks, Mike

0

Always remember to look at the Joomla API should you not be able to find anything in the documentation. UnionAll is mentioned here:

http://api.joomla.org/cms-3/classes/JDatabaseQuery.html#method_unionAll

2

And do not forget about Joomla! Docs :)

http://docs.joomla.org/Using_the_union_methods_in_database_queries

Here is an example from the Docs.

Suppose that you want to send the mail to all customers and all suppliers and that that the names and email addresses are stored in tables called, unsurprisingly, customers and suppliers, respectively.

This query will retrieve all the customer information that we need for the mailshot:

$query
    ->select('name, email')
    ->from('customers')
    ;
$mailshot = $db->setQuery($query)->loadObjectList();

while this query will do the same for all suppliers:

$query
    ->select('name, email')
    ->from('suppliers')
    ;
$mailshot = $db->setQuery($query)->loadObjectList();

Then you can combine the results into a single query like this:

$query
    ->select('name, email')
    ->from('customers')
    ->union($q2->select('name , email')->from('suppliers'))
    ;
$mailshot = $db->setQuery($query)->loadObjectList();

The result set obtained from the union query will actually be a little different from running the individual queries separately because the union query will automatically eliminate duplicates. If you don't mind that the result set may contain duplicates (which mathematically speaking means it isn't a set) then using unionAll instead of union will improve performance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.