tags:

views:

191

answers:

4

Hello!

I have a PostgreSQL database with two tables named "user" and "group". They have different columns, but I want to join them.

user: id, firstname, lastname, email, registrationdate

group: id, name, desc, createdate

I can do two separate queries: select * from "user" where ... order by "registrationdate" asc; select * from "group" where ... order by "createdate" asc;

Is it possible to join these two queries into one and order all by date? The different columns could be NULL, because they do not have the same column names.

Is this possible? What I wanna do is a search in which user and groups will be displayed mixed ordered by the date.

Thanks & Best Regards.

A: 

You can create view and then sort by date.

Valery Victorovsky
+2  A: 

This seems all kinda wrong but you can try

SELECT u.id, u.firstname || ' ' || u.lastname || ' ' || u.email, u.registrationdate AS DateVal
FROM user u
UNION ALL
SELECT g.id, g.name || ' ' || g.desc, g.createdate
FROM group g
ORDER BY 3
astander
But here you put firstname/lastname/email in one column and I can't access this data, because if someone wrote "Hans Peter" as firstname, I have a problem to separate it.
Tim
How do you then wish to **UNION** these tables. *Are you looking for a join, or a union*?
astander
I think the best is to do something like:id|firstname|lastname|email|name|desc11|---------|--------|-----|test|testDesc99|TestFirst|TestLast|-----|----|----
Tim
It still does not seem quite clear what you wish to display. Maybe if from your mentioned columns from the tables you can tell us what you whish to **show/orderby/select** we can help.
astander
It is now very good what I am doing, so I have to think about it and I have to change it. Perhaps it would be better sorting the dates in programm code instead of in a sql query.Thanks a lot for your help!
Tim
A: 

Maybe a VIEW? Something similar to this (I'm not sure if you have to give all the columns names):

CREATE VIEW
  myview
AS
  SELECT
    "user" as type,
    t1.id,
    t1.username,
    t1.firstname,
    t1.lastname,
    registrationdate as thedate,
    null,
    null,
    null
  FROM t1
UNION ALL
  SELECT
    "group" as type, 
    null,
    null,
    null,
    null,
    createdate as thedate,
    t2.id,
    t2.name,
    t2.desc
;

And then select:

SELECT * FROM myview ORDER BY thedate ASC;
Emil Vikström
You're overcomplicating things with the view. Just the query with an order by would be enough.
OMG Ponies
ERROR: Relation »t1« does not exist
Tim
A: 

Are you doing this for efficiency? If so, consider whether two separate queries would be both simpler, and possibly as fast or faster than making postgres jump through hoops. Especially, I've seen exactly zero systems in my life that were limited by the ability of the clients to do computation. The database, however, is often your performance Achilles heel.

Also, you will thank yourself later if you avoid column and table names that are postgres reserved words (user, desc). Having to quote those names in psql is a pain.

# create temporary table foo (i int);
# create temporary table bar (j int);
# insert into foo values (1);
# insert into foo values (2);
# insert into foo values (3);
# insert into bar values (3);
# insert into bar values (4);
# insert into bar values (5);
# select * from (select i, null as j from foo union select null, j from bar) baz order by coalesce(i, j);
 i | j
---+---
 1 |
 2 |
 3 |
   | 3
   | 4
   | 5
Wayne Conrad