Обсуждение: Selecting from two unrelated tables

Поиск
Список
Период
Сортировка

Selecting from two unrelated tables

От
CSN
Дата:
I have two tables:

items: id, title, added, ...
news: id, headline, datetime, ...

I'd like to select the latest 25 combined records from both tables. Is there a way to do this
using just select?

Thanks,
csn

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

Re: Selecting from two unrelated tables

От
Bricklen Anderson
Дата:
CSN wrote:
> I have two tables:
>
> items: id, title, added, ...
> news: id, headline, datetime, ...
>
> I'd like to select the latest 25 combined records from both tables. Is there a way to do this
> using just select?
>
> Thanks,
> csn
>

Maybe something like this?

select id,title_headline,dt
from (
   select id,title as title_headline,added as dt from ...
   union all
   select id,headline as title_headline,datetime as dt from ...) as a
order by dt limit 25;

Re: Selecting from two unrelated tables

От
John Sidney-Woollett
Дата:
You can use an union if the column types match

{...off the top of my head...}

select id, title, added as sortcol from table1
union
select id, headline, datetime as sortcol from table2
order by sortcol
limit 25;

John

CSN wrote:
> I have two tables:
>
> items: id, title, added, ...
> news: id, headline, datetime, ...
>
> I'd like to select the latest 25 combined records from both tables. Is there a way to do this
> using just select?
>
> Thanks,
> csn
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Have you searched our list archives?
>
>                http://archives.postgresql.org