DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   MS SQL (http://www.daniweb.com/forums/forum127.html)
-   -   Joining two tables (http://www.daniweb.com/forums/thread158940.html)

rebeka.six Nov 22nd, 2008 10:43 am
Joining two tables
 
I need help with joining these two tables:

Table1

idx idy
---------
one 1
two 2
tree 3


Table2

idy u
-------
1 a
2 b
3 c
4 d
5 e
6 f

I want to show rows from the Table1 that have idx=one joined with all the rows from Table2.
So, I want to join these two tables so that Table1.idy=Table2.idy and Table1.idx=one
The result table should look like this.

Result table:

idx idy u
--------------
one 1 a
NULL 2 b
NULL 3 c
NULL 4 d
NULL 5 e
NULL 6 f

Could someone help me with this?
Thanks.
Rebeka

dickersonka Nov 23rd, 2008 1:57 pm
Re: Joining two tables
 
i don't see how you are expecting idx = NULL for 2-6, when you have both entries for idy in both tables

are you sure this is what you are wanting?

huangzhi Nov 24th, 2008 2:23 am
Re: Joining two tables
 
Try this command:
select        T1.idx, T2.idy, T2.u
  from        Table1 T1 right join Table2 T2 on
        T1.idy = T2.Idy and T1.idx = 'one'

rebeka.six Nov 24th, 2008 9:26 am
Re: Joining two tables
 
Hi,

Works perfectly!
Thanks,

Rebeka


All times are GMT -4. The time now is 7:24 am.

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