Firebird SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesFirebird SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 8th, 2007, 05:03 AM
esupport4 esupport4 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Posts: 37 esupport4 User rank is Lance Corporal (50 - 100 Reputation Level)esupport4 User rank is Lance Corporal (50 - 100 Reputation Level)esupport4 User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 7 h 49 m 17 sec
Reputation Power: 2
Question Two foreign keys of a table referring a same primary key

i have a table1 having userid(primary key) and username. I have another table2 having senderid, receiverid fields referring to userid of table1. Now i have created a view wherein the condition i have used is:

where table2.senderid = table1.userid
and table2.receiverid = table1.userid.

the problem is this code will give me only those rows where both senderid and receiverid are same.
I have tried using joins also but didn't get proper result.

Can anyone tell me how to get through this?

Reply With Quote
  #2  
Old September 8th, 2007, 06:43 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 8,066 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 4 Weeks 1 h 56 m 57 sec
Reputation Power: 281
This has nothing to do with foreign keys, you need to join two times on the users table using an alias, something like
sql Code:
Original - sql Code
  1. SELECT
  2. t2.*
  3. FROM table2 t2 INNER JOIN table1 t1 ON t2.senderid = t1.userid
  4. INNER JOIN table1 t_1 ON t2.receiverid = t_1.userid;
I suggest also to use the inner join syntax instead of the where syntax for joins

Reply With Quote
  #3  
Old September 8th, 2007, 07:34 AM
esupport4 esupport4 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Posts: 37 esupport4 User rank is Lance Corporal (50 - 100 Reputation Level)esupport4 User rank is Lance Corporal (50 - 100 Reputation Level)esupport4 User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 7 h 49 m 17 sec
Reputation Power: 2
Thank You

Quote:
Originally Posted by pabloj
This has nothing to do with foreign keys, you need to join two times on the users table using an alias, something like
sql Code:
Original - sql Code
  1. SELECT
  2. t2.*
  3. FROM table2 t2 INNER JOIN table1 t1 ON t2.senderid = t1.userid
  4. INNER JOIN table1 t_1 ON t2.receiverid = t_1.userid;
I suggest also to use the inner join syntax instead of the where syntax for joins



THANKS A LOT. IT'S WORKING VERY WELL..........

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesFirebird SQL Development > Two foreign keys of a table referring a same primary key


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT