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
 Help formatting my SQL..

Author  Topic 

Slipstream19
Starting Member

5 Posts

Posted - 2013-11-29 : 10:06:21
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

8 Posts

Posted - 2013-11-30 : 01:04:07
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

52326 Posts

Posted - 2013-11-30 : 01:54:39
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
   

- Advertisement -