|
Posted by Tim Roberts on 11/11/05 06:54
"bizt" <bissatch@yahoo.co.uk> wrote:
>Ok, tried a simple example:
>
>$select = "SELECT title, desctext FROM commnews UNION SELECT title,
>article AS desctext FROM pressnews ORDER BY createdon";
>
>$result = pg_exec ($db, $select);
>
>But giving me the following error:
>
>Warning: pg_exec(): Query failed: ERROR: column "createdon" does not
>exist in /data/httpd/VirtualHosts/web-sandbox/htdocs/_testing/union.php
>on line 34
>
>The column 'createdon' does exist in both so I can only assume that my
>ORDER BY command isnt properly formated. Is this the correct way to
>ORDER a UNION SELECT?
Almost. The union-ed query does not have column names, so you have to
refer to them by number. That means the column must participate in the
query. This should work:
SELECT title, desctext, createdon FROM commnews
UNION
SELECT title, article, createdon FROM pressnews
ORDER BY 3;
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
[Back to original message]
|