Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Correct Syntax For 3 Table Join?

Author  Topic 

Palermo
Starting Member

25 Posts

Posted - 2013-10-24 : 19:14:24
I have tried :

SELECT column_name1, column_name2, column_name3
FROM tbl_name1, tbl_name2, tbl_name3
WHERE tbl_name1.column_name = tbl_name2.column_name
AND tbl_name2.column_name = tbl_name3.column_name

Although the query runs there is no data. The column is table 3 has numerical data (int) whereas the columns in the other two tables are nchar(20). is this why I get no data or is there something wrong with my syntax?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2013-10-24 : 23:24:02
SELECT column_name1, column_name2, column_name3
FROM tbl_name1
join tbl_name2 on tbl_name1.column_name = tbl_name2.column_name
join tbl_name3 on tbl_name2.column_name = convert(nchar(20), tbl_name3.column_name)

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Palermo
Starting Member

25 Posts

Posted - 2013-10-25 : 13:20:10
I wasn't getting any data as I wasn't joining tables on their shared foreign and primary keys.

SELECT table1.column, table2.column, table3.column
FROM table1, table2, table3
WHERE table1.column = table3.column [on their shared primary key]
AND table3.column = table2.column [on their shared foreign key]
Go to Top of Page

markjosol
Starting Member

2 Posts

Posted - 2013-10-26 : 05:06:43



is table 3 has numerical data (int) whereas the columns in the other two tables are nchar(20). is this why I get no data or is there something wrong with my syntax?




________________________________________________
http://www.fifautc.com/

markjosol josol
Go to Top of Page
   

- Advertisement -