| Author |
Topic |
|
Kotti
Posting Yak Master
129 Posts |
Posted - 2009-05-22 : 10:41:46
|
| Hi FriendsI had three tablesUsers,Transactions ,TransactionContactUser Table HasUserid Company1 AAA2 BBB3 CCCTransaction Table hasTransactionId Transaction1 asdf2 zxcv3 qwerTransactioncontact table hasTcId TransactionId Userid1 2 12 3 33 1 2when i search the company AAA ,its Userid is 1 ,and i take the userid 1 and check in Transactioncontact table, i can get Transactionid as 2 ,so i need to display the transactionid 2 details from transaction table.Please help me how to get the result.Thanks in Advance |
|
|
Skorch
Constraint Violating Yak Guru
300 Posts |
Posted - 2009-05-22 : 11:17:57
|
| SELECT TransactionFROM Transaction tJOIN TransactionContact tc ON tc.TransactionID = t.TransactionIDJOIN User u on u.Userid = tc.UserIDWHERE u.Company = @searchedcompanySome days you're the dog, and some days you're the fire hydrant. |
 |
|
|
Kotti
Posting Yak Master
129 Posts |
Posted - 2009-05-22 : 11:26:35
|
| Hi SkorchThank you for your reply.I am getting Wrong Answer when i use your query.Please help me regarding this |
 |
|
|
Skorch
Constraint Violating Yak Guru
300 Posts |
Posted - 2009-05-22 : 11:29:28
|
| What answer are you getting? And what are you expected results?Some days you're the dog, and some days you're the fire hydrant. |
 |
|
|
Kotti
Posting Yak Master
129 Posts |
Posted - 2009-05-22 : 11:34:06
|
| I am getting No Results |
 |
|
|
Skorch
Constraint Violating Yak Guru
300 Posts |
Posted - 2009-05-22 : 11:38:50
|
| [code]DECLARE @Users TABLE (UserID int, Company varchar(30))INSERT INTO @UsersSELECT 1, 'AAA' UNION ALLSELECT 2, 'BBB' UNION ALLSELECT 3, 'CCC'DECLARE @Transaction TABLE (TransactionID int, [Transaction] varchar(30))INSERT INTO @TransactionSELECT 1, 'asdf' UNION ALLSELECT 2, 'zxcv' UNION ALLSELECT 3, 'qwer'DECLARE @TransactionContact TABLE (TCID int, TransactionID int, UserID int)INSERT INTO @TransactionContactSELECT 1, 2, 1 UNION ALLSELECT 2, 3, 3 UNION ALLSELECT 3, 1, 2SELECT [Transaction]FROM @Transaction tLEFT JOIN @TransactionContact tc ON tc.TransactionID = t.TransactionIDJOIN @Users u on u.Userid = tc.UserIDWHERE u.Company = 'AAA'[/code]See if this helps.Some days you're the dog, and some days you're the fire hydrant. |
 |
|
|
Kotti
Posting Yak Master
129 Posts |
Posted - 2009-05-22 : 11:56:13
|
| Now also , i am getting no results. |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2009-05-22 : 12:10:56
|
If you run the code given by Skorch you do not get any results? The code that is posted shoudl work:SELECT [Transaction]FROM @Transaction tINNER JOIN @TransactionContact tc ON tc.TransactionID = t.TransactionIDJOIN @Users u on u.Userid = tc.UserIDWHERE u.Company = 'AAA' Is there something else going on with your data or did you not present the whole issue?EDIT: I changed the LEFT JOIN to an INNER JOIN because the restriction turns it into an inner join. |
 |
|
|
Kotti
Posting Yak Master
129 Posts |
Posted - 2009-05-22 : 13:09:29
|
| Hi LampreyI used your query too ,but there is no result. |
 |
|
|
Skorch
Constraint Violating Yak Guru
300 Posts |
Posted - 2009-05-22 : 13:13:29
|
| You're obviously leaving out some data or part of your table structure. If you run the query I posted it runs just fine and returns 'zxcv'.Some days you're the dog, and some days you're the fire hydrant. |
 |
|
|
Kotti
Posting Yak Master
129 Posts |
Posted - 2009-05-23 : 00:42:26
|
| Sorry FriendsI just missed one thing,Now i am getting results.Thank you for all your help. |
 |
|
|
|