Click here to Skip to main content
15,920,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display foreign key values in main table that should be display in grid


My table is like this

Id Name CountryID StateID 

11 aaa   23         2
34 rr4   33         5

so i want to display output like this

Id Name CountryName StateName 

11 aaa   India       AndhraPradesh
34 rr4   China       Chinkango  

This query will not display proper result

SQL
select  distinct (condet.ContactID),condet.ContactName,condet.EMailID,tc.CName,ts.Name,condet.ContactNumber   from tblContactDetails as condet
                             inner  join tblCountry as tc on condet.CountryID = tc.CountryID
                             inner  join tblState as ts on tc.CountryID = ts.CountryID
Posted
Updated 28-May-14 22:54pm
v3
Comments
syed shanu 29-May-14 4:33am    
In your query you have mention " tblState as ts on tc.CountryID = ts.CountryID" but i think tblstate need to check with stateid also ex like this
"tblState as ts on tc.CountryID = ts.CountryID AND tc.StateID=ts.StateID"
and in your query there is no "where condition".I hope you are missing the where conditions
CHill60 29-May-14 4:36am    
Worth posting as the solution! Don't think the OP needs a where condition to get the output they require though
CHill60 29-May-14 4:51am    
I've just noticed your original post 14 minutes prior to this one. Please do not repost the same question - especially as your own solution to your other post is the one suggested by syed shanu here

1 solution

Try this query :)

SQL
select condet.ContactID,condet.ContactName,condet.EMailID,tc.CName,ts.Name,condet.ContactNumber   from tblContactDetails as condet
                             inner  join tblCountry as tc on condet.CountryID = tc.CountryID
                             inner  join tblState as ts on tc.CountryID = ts.CountryID
 
Share this answer
 

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