Database Union statement

Union statement

Yesterday, I must design a customer support system. After searching on the internet, getting references from some e-books and I re-memorized SQL statements which could be suitable.

Basic, Union statement is same one as in mathematics. In SQL, it's used to unite two select-views.

Let you see the listing code below. I also illustrated the process for better understanding.

use ebook_sql
go
select * from Orders
go
select * from Returns
go
select 'order' as 'Type', orders.OrderDate as 'Date', OrderAmount as 'Amount' from Orders where orders.CustomerID=2
go
select 'return' as 'Type',Returns.ReturnDate as 'Date', ReturnAmount as 'Amount' from Returns where returns.CustomerID=2
go
select 'order' as 'Type', orders.OrderDate as 'Date' from Orders where orders.CustomerID=2
union
select 'return' as 'Type',Returns.ReturnDate as 'Date' from Returns where returns.CustomerID=2

order by Date desc



Share on Google Plus

About Chien

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment