dbTalk Databases Forums  

SQL question

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss SQL question in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
dwightarmyofchampions@hotmail.com
 
Posts: n/a

Default SQL question - 07-07-2007 , 01:59 PM






How do you combine two SELECT statements from two different tables
into one result (assuming bioth tables have the exact same fields)?

Like, assuming that the query SELECT * FROM Table1 gives the result

ID Name
001 Smith
002 Jones

and the query SELECT * FROM Table2 gives the result

ID Name
247 Simpson
856 Beckley

I want a SELECT ... ORDER BY ID statement that will output the
following data:

ID Name
001 Smith
002 Jones
247 Simpson
856 Beckley

How do I do this?


Reply With Quote
  #2  
Old   
ZeldorBlat
 
Posts: n/a

Default Re: SQL question - 07-07-2007 , 02:22 PM






On Jul 7, 2:59 pm, dwightarmyofchampi... (AT) hotmail (DOT) com wrote:
Quote:
How do you combine two SELECT statements from two different tables
into one result (assuming bioth tables have the exact same fields)?

Like, assuming that the query SELECT * FROM Table1 gives the result

ID Name
001 Smith
002 Jones

and the query SELECT * FROM Table2 gives the result

ID Name
247 Simpson
856 Beckley

I want a SELECT ... ORDER BY ID statement that will output the
following data:

ID Name
001 Smith
002 Jones
247 Simpson
856 Beckley

How do I do this?
select id, name
from table1
union
select id name
from table2
order by id



Reply With Quote
  #3  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: SQL question - 07-07-2007 , 05:31 PM



ZeldorBlat (zeldorblat (AT) gmail (DOT) com) writes:
Quote:
On Jul 7, 2:59 pm, dwightarmyofchampi... (AT) hotmail (DOT) com wrote:
I want a SELECT ... ORDER BY ID statement that will output the
following data:

ID Name
001 Smith
002 Jones
247 Simpson
856 Beckley

How do I do this?

select id, name
from table1
union
select id name
from table2
order by id
Or:

select id, name
from table1
union ALL
select id name
from table2
order by id

By default UNION sorts out duplicates. With UNION ALL they are retained.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #4  
Old   
--CELKO--
 
Posts: n/a

Default Re: SQL question - 07-08-2007 , 09:04 AM



Quote:
How do you combine two SELECT statements from two different tables into one result (assuming both tables have the exact same fields [sic])?
The short answer is to use a UNION or UNION ALL.

The right answer is that tables are not files, just as columns are not
fields. If two tables have the exact same structure, then they model
the exact same entity and should be in one table. Files do not behave
that way.

This is usually the result of attribute splitting -- taking the values
of an attribute and making a table for each value.



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.