Friday, March 23, 2012

Joining two tables

I have two tables:
1. Incomes
Income Amount
Salary 10000
Sales 55000
and 2. Expenses

Expense Amount
House Rent 3000
Tution 4000
Fooding 4000
Travelling 2000

I want to join these to tables to form:
Salary 10000 Expense Amount
Sales 55000 House Rent 5000
(null) (null) Tution 4000
(null) (null) Fooding 4000
(null) (null) Travelling 2000

How can it be done. Thanksit cannot be done unless you specify some way of joining them

in other words, what conditions would determine that

-- Salary 10000 is matched with Expense Amount
(seems to be a typo here -- Expense and Amount are column names, not column values)
-- Sales 55000 is matched with House Rent 5000

are you planning any primary keys for these tables?

rudy
http://r937.com/|||There are a couple of ways you can handle this:

1.
select income, amount, 'income' as type from incomes
union
select expense, amount, 'expense' as type from expenses
order by type

2.
select income, amount, '','' from incomes
union
select '','',expense, amount from expenses

Or another variation.

No comments:

Post a Comment