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
 General SQL Server Forums
 New to SQL Server Programming
 Inner Join won't work when I add WHERE

Author  Topic 

mike241982
Starting Member

2 Posts

Posted - 2012-07-16 : 15:47:21
Hello,

I'm pretty new to SQL and I have the following:

SELECT COL1, COL2, COL3
FROM TABLE1 INNER JOIN TABLE2
ON TABLE1.COL1 = TABLE2.COL1
WHERE TABLE3.COL1 = 'example'

First 3 lines work, but when I add the WHERE statement at the end, it doesn't work.

Basically I need to do the following:

1) Get from table3 everything from col1 that says 'example'
2) From that result, I need to get the col1, col2, col3 from table 1 IF table1.col1 = table2.col1

Thanks!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-07-16 : 15:59:40
You have to add another join in order to use TABLE3 in your query.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

mike241982
Starting Member

2 Posts

Posted - 2012-07-16 : 16:05:03
How would I add another INNER JOIN?

SELECT COL1, COL2, COL3
FROM TABLE1 INNER JOIN TABLE2 AND TABLE3
ON TABLE1.COL1 = TABLE2.COL1
WHERE TABLE3.COL1 = 'example'

or

SELECT COL1, COL2, COL3
FROM TABLE1 INNER JOIN TABLE2
ON TABLE1.COL1 = TABLE2.COL1
FROM TABLE2 INNER JOIN TABLE3
WHERE TABLE3.COL1 = 'example'

Neither seems to be working for me but adding another inner join makes sense.

If you can provide an example that'd be great!

Thanks
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-07-16 : 16:14:29
http://www.sqlservercentral.com/articles/Best+Practices/61537/
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-07-16 : 16:33:35
SELECT COL1, COL2, COL3
FROM TABLE1
JOIN TABLE2 ON TABLE1.COL1 = TABLE2.COL1
JOIN TABLE3 ON ...--Need your join condition here, how does TABLE3 relate to TABLE1 or TABLE2?
WHERE TABLE3.COL1 = 'example'

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -