Re: ORDERing two UNION queries

From: Date: Sat, 14 Apr 2007 16:30:34 +0000
Subject: Re: ORDERing two UNION queries
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-43444@lists.php.net to get a copy of this message
At 03:04 14/04/2007, you wrote:
Message-ID: <461FB44B.3010302@btinternet.com> Date: Fri, 13 Apr 2007 17:48:11 +0100 From: ioannes <ioannes@btinternet.com> Reply-To: ioannes@btinternet.com MIME-Version: 1.0 To: php-db@lists.php.net Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: ORDERing two UNION queries: Non-Subquery UNION Execution http://dev.mysql.com/doc/internals/en/select-union.html
You meant to refer to this page : http://dev.mysql.com/doc/refman/5.1/en/union.html
Does anyone know what this means? Can it be implemented in the flow of php code without explicitly creating temp tables in the database? My query is like: $query = "
       (SELECT
       Table.field, [other fields]
       FROM Table [other JOINs]
       ORDER BY Table.field
       )
       UNION
       (SELECT
       Table2.field, [other fields]
       FROM Table2 [other JOINs]
       ORDER BY Table2.field
       )
       ";
and I want the ORDER to apply to the whole UNIONed result.
So, after reading the correct documentation, you'll now know to write the query as
      (SELECT
       Table.field, [other fields]
       FROM Table [other JOINs]
       )
       UNION
       (SELECT
       Table2.field, [other fields]
       FROM Table2 [other JOINs]
       )
       ORDER BY field
Cheers - Neil

« previous php.db (#43444) next »