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
 SQL 3 Table Join

Author  Topic 

Dajka
Starting Member

5 Posts

Posted - 2011-10-20 : 18:06:59
Hi,

I'm new to sql so sorry in advance if I say something wrong.
My issue is the following: I'm trying to connect 3 tables where 2 is connected with a simple join, but the third tabel does not neccessarily have a record in it with the requested data.

I was trying to do something like this... I dont know if it helps you to understand what I'm trying to do... Maybe its totally useless...

SELECT n.Tipus, n.SubjectId, n.Nev, n.Timestamp, k.Tipus, t.Timestamp (FROM notifikaciok AS n, kovetes AS k, nezett_log AS t WHERE k.Nev = ? AND (n.SubjectId = k.SubjectId OR n.Nev=k.SubjectId) AND n.Timestamp>k.RegTime) LEFT JOIN t ON n.SubjectId=t.Nezett WHERE t.Nev=k.Nev ORDER BY n.Timestamp DESC

Thanks for your help in advance

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-10-20 : 18:09:19
Shoe us some sample data, please.

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

Subscribe to my blog
Go to Top of Page

Dajka
Starting Member

5 Posts

Posted - 2011-10-20 : 18:22:41
Sorry for the multi language.
I hope this is fine.

notifikaciok table:

(`NotId`, `Tipus`, `SubjectId`, `Nev`, `Timestamp`) VALUES
(82, 'HSZ', '1022', 'Dajka', 1319067674)

kovetes table:

(`KovId`, `Nev`, `SubjectId`, `Tipus`, `LastVisit`, `RegTime`) VALUES
(153, 'Ventura', '403', 'FAV', 1319067165, 1319067165)

nezett_log table:

(`Nev`, `Nezett`, `Timestamp`) VALUES
('Ventura', '907', 1316740495)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-10-20 : 18:28:07
Your sample data does not help us help you. You need to provide sample data that illustrates your problem. One row from each table is clearly not enough.

And if there's no relationship to the 3rd table, then how do you expect it to be joined? We need to see sample data that illustrates that.

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

Subscribe to my blog
Go to Top of Page

Dajka
Starting Member

5 Posts

Posted - 2011-10-20 : 18:45:09
Okay I’ll try to explain the problem... (Sorry for my English)

So the situation is the following... I'm trying to create a notification system, where all the events get into the 'notifikaciok' table. I want to order this table by time and then query it through to get the related events for each individual user. every user can register for events in the 'kovetes' table. So I want to return those rows from 'notifikaciok' where the (n.SubjectId = k.SubjectId OR n.Nev=k.SubjectId)

This is where my issue comes in.

Now I have a third table 'nezett_log' containing the information about when was the last time 'Timestamp' the user saw the appropriate event, but if the user never visited that page before, then there is going to be no record of this in this table. Even though there is no record, I still want to return all the other fields from the other two tables.

I hope this makes sense...
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-10-20 : 18:53:33
You just need a LEFT JOIN to the third table.

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

Subscribe to my blog
Go to Top of Page

Dajka
Starting Member

5 Posts

Posted - 2011-10-20 : 18:55:23
As you can see I was trying to do this in the SQL what I posted in my first comment, but it seems to have some syntax error what I cant figure out. Could you help me with that?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-10-20 : 19:30:51
Your syntax is confusing me. I will show you an example instead:

SELECT a.c1, b.zzz, b.hgh, c.asdf
FROM TableA a
JOIN TableB b
ON a.c1 = b.c1
LEFT JOIN TableC c
ON b.c2 = c.c2
WHERE ...

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

Subscribe to my blog
Go to Top of Page

Dajka
Starting Member

5 Posts

Posted - 2011-10-20 : 20:01:08
wonderful!

Thank you very much.
This is exactly what I needed.
work like a charm ;)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-10-20 : 20:07:22
You're welcome, glad to help!

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 -