Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi!
i want to take join on two tables with foreign keys,
1st table is Comments having fields CommentID,userID(foreign key),AgentID(foreign Key),Description(DateTime)
and
another table Ratings with fields RatingID,userID(foreign key),AgentID(foreign Key),Rating(DateTime)

User Table is the table having UserID as a primary key which is used as foreign keys in both comment and rating table.
and AgentID is the Primary key in Agent Table used as foreign key in bothe comment and ratings table
now wot i want is to see the comments and rating given by the same user on a same Agent i-e AgentID .

i am taking join on Comment and Ratings table based on User ID for e.g i want to see rating and comment of the given same user
here is the query i am using

SQL
SELECT c.CommentID, c.UserID, c.PropertyID, c.PropertyType, c.DateTIme, c.Description, c.AgentID, c.visibility, r.RatingID, r.UserID AS Expr1, r.PropertyID AS Expr2, 
r.DateTime AS Expr3, r.AgentID AS Expr4, r.Rating
FROM Comments AS c INNER JOIN
Ratings AS r ON c.UserID = r.UserID AND c.AgentID = r.AgentID AND c.AgentID = 4


but the problem is it is taking cross join.i have two comments on a agent and two rating on the same agentID=4 given by the same userID..it displays record like this

CommentID UserID AgentID RAtingID Description Rating<br />
62      1 4 68 best 4<br />
71 1 4 68 hello 4<br />
62 1 4 8 best 2<br />
71 1 4 8 hello 2
Posted
Updated 16-May-11 11:22am
v2

1 solution

If you want to join by User Id then

SQL
FROM Comments AS c INNER JOIN
Ratings AS r ON c.UserID = r.UserID  WHERE c.AgentID = 4 AND r.AgentID=4


This one c.AgentID = r.AgentID AND c.AgentID = 4 A=B and A=5, means A=5 and B=5, Isn't it. :)
 
Share this answer
 
v2
Comments
Sandeep Mewara 17-May-11 2:55am    
My 5!
Albin Abel 17-May-11 5:06am    
Thanks Sandeep Mewara

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900