SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help formatting my SQL..
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Slipstream19
Starting Member

Canada
5 Posts

Posted - 11/29/2013 :  10:06:21  Show Profile  Reply with Quote
Hi everyone,

I know what I need to get for my SQL but with this, I am not returning many results. It touches 4 tables and a bit beyond me, yet I need to get it done. Here is my current SQL.

select
t1.QUOTE as Quote,
t3.NUM as C_NUM,
t3.NAME,
t1.field1,
t1.date1,
t1.date2,
t1.field2,
t4.NAME
from table1 t1, table2 t2, table3 t3 , table t4
where
t1.randField='GOOD' and
t1.field2 = 'Y' and
t1.CREATED>=to_date('2013-01-01', 'YYYY-MM-DD') and
(t1.FLG is null or t1.FLG='N') and

t1.ID=t2.ROW_ID and
t2.FK_ID1=t3.ROW_ID and

t2.FK_ID2=t4.ROW_ID

Order by t1.CREATED;


Basically, I have 4 tables (t1,t2,t3,t4) I want to display values from each based on the other tables. I want to display (Quote, field1, field2, date2, date2) from table 1.

However I need to go to table 2, to get a value and then to table 3 to find the value, then to table 4 as well..

Can anyone clear this up for me?

ijmar86
Starting Member

India
8 Posts

Posted - 11/30/2013 :  01:04:07  Show Profile  Reply with Quote
SELECT T1.Col1, T2.Col1, T3.Col1 (any Column from t1,T2,T3,T4) FROM
BaseTable T1
JOIN 1stTable T2 ON T1.MappingClumn1 == T2.MappingColumn1 AND T1.MappingClumn2 == T2.MappingColumn2 (Can Add n number cond between T1 & T2)
JOIN 2ndTable T3 ON T1.MappingClumn1 == T3.MappingColumn1 AND T2.MappingClumn2 == T3.MappingColumn2 (Can Add n number cond between T1 & T2 & T3 (T3 must in conditions))
JOIN 2ndTable T4 ON T1.MappingClumn1 == T4.MappingColumn1 AND T2.MappingClumn2 == T4.MappingColumn2 (Can Add n number cond between T1 & T2 & T3 & T4 (T4 must in conditions))
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 11/30/2013 :  01:54:39  Show Profile  Reply with Quote
First question is whether there's a one - to - one relationship between all tables. Are they any cases where one of table may not have matches. If thats true, then you need to use OUTER JOIN (LEFT/RIGHT/FULL depending on your requirement) rather than INNER JOIN to get this working

see
http://www.w3schools.com/sql/sql_join.asp

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000