DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   MS SQL (http://www.daniweb.com/forums/forum127.html)
-   -   Extract columns from two independent table in SQL Server 2005? (http://www.daniweb.com/forums/thread118554.html)

bhavna_816 Apr 11th, 2008 2:54 am
Extract columns from two independent table in SQL Server 2005?
 
I want to get two columns data from two tables which are not dependent to each other i.e. no key common in both they are not related to each other.

I can't use JOIN because it needs where clause which uses a common field from both tables.
I just want one column from one table and one column from another and display it.
how can do it?
some unions or any other syntax for that?

Thanks in advance,

plazmo Apr 11th, 2008 12:12 pm
Re: Extract columns from two independent table in SQL Server 2005?
 
select * from table1, table2

this combines both tables

sonia sardana Apr 11th, 2008 5:06 pm
Re: Extract columns from two independent table in SQL Server 2005?
 
create table name1(name varchar)
insert into name1 values('A')
insert into name1 values('B')

create table name2(name varchar)
insert into name2 values('B')
insert into name2 values('C')

select name from name1
UNION
select name from name2

UNION will eliminate duplicates,If u want to retain duplicates Use UNION ALL.

Is that worked for u?If face any probs reply.

sonia sardana Apr 12th, 2008 2:55 am
Re: Extract columns from two independent table in SQL Server 2005?
 
Can u Paste the tables & specify which column to retrieve, so that I can help u out.

aardvax01 Apr 14th, 2008 12:51 am
Re: Extract columns from two independent table in SQL Server 2005?
 
Hi Bhavna

I think that you can do it yourself
There is something called Non -Equi Joins Which needs the where clause which you want
I think you can use Logical Operators to accomplish what you want

There is one Example of Employee and Salary Tables ( a famous one)

Hope this will help you

Ok lemme give an Example -- Suppose there are two tables with name as Emp and Salary



SELECT E.Last_Name, E.Sal, S.Category
FROM Emp E, Salary S
WHERE E.Sal Between S.Highest and S.Lowest


And this example is taking oracle in consideration ..
there might be some other way of using joins in SQL Server
I will have a look and will edit this post once more

aardvax01 Apr 18th, 2008 7:12 am
Re: Extract columns from two independent table in SQL Server 2005?
 
 SELECT * FROM Employees 


I posted this message to learn the [code tags]


All times are GMT -4. The time now is 10:44 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC