Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Need help with easy query

Author  Topic 

vgurgov
Starting Member

12 Posts

Posted - 2006-08-17 : 18:13:22
Hi,

I have 2 tables:

create table resources (
res_id int identity primary key,
res_name varchar(50) not null unique,
res_url varchar(200)
<more fields>...);

create table resource_names (
res_id int not null,
res_name varchar(50) not null,
primary key (res_id, res_name),
foreign key (res_name) references resources (res_id));

The reason why I have the 'resource_names' table is because there are some resources which may have alternative names and I don't want to keep them in the main 'resources' table.

My question is, how can I pull up all resources from both tables so that the returning results will look like this:

res_id res_name res_url
====== ======== =======
1 A1 http://res1_url.com
1 A2 http://res1_url.com
1 A3 http://res1_url.com
2 B1 http://res2_url.com
3 C1 http://res3_url.com

I need only the first resource with specific res_id to be from 'resources' table (res_id 1 in example above), the rest (with with same res_id) should be from 'resource_names', and all other fields from 'resources' should show on all records.

Thanks in advance,
VG

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-08-17 : 18:31:32
[code]
select
a.res_id,
a.res_name,
a.res_url
from
resources a
union all
select
b.res_id,
c.res_name,
b.res_url
from
resources b
join
resource_names c
on b.res_id = c.res_id
order by
1,2

[/code]

CODO ERGO SUM
Go to Top of Page

vgurgov
Starting Member

12 Posts

Posted - 2006-08-17 : 21:54:45
Thanks Michael! Worked like a charm :)
Go to Top of Page
   

- Advertisement -