C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » ASP.NET »

Query for joining two tables


Posted Date: 04 Jul 2009      Posted By: Dni      Member Level: Gold     Points: 1   Responses: 11



I have two table in Sql db.in the first table i have name,phoneno.in the second table i have mobile no.i want to write a query to display the name,phone,mobile from the above two tables




Responses

Author: gopal.net    04 Jul 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

hi,

In both tables u need to put one common column.
Bcoz to put relation but 2 tables.

then use inner join by taking alias name



gopal



Author: Dni    04 Jul 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

The two tables doesnt have relation.how can i write the query?


Author: gopal.net    04 Jul 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

hi,

U have to take common column other wise u can't.


this is sample query for u question.

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
INNER JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName



Author: gopal.net    04 Jul 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

hi,

wht r two table names n columns in tht



Author: Dni    04 Jul 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

table names are table1(name,phone,id),table2(mobile,address)


Author: gopal.net    04 Jul 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

hi,

ok take id as common field in both tables.


then use


SELECT a.name, a.phone, b.mobile,b.address
FROM table1 a
INNER JOIN table2 b
ON a.ID=b.ID


where a and b are alias names for 2 tables



Author: gopal.net    04 Jul 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

hi,


is it helpful


gopal.net



Author: Miss Meetu Choudhary    04 Jul 2009Member Level: DiamondRating: 2 out of 52 out of 5     Points: 2

Please Use proper title

Thanks and Regards
Miss Meetu Choudhary (Site Coordinator)
Go Green Save Green
My Profile on Google



Author: Devendra    04 Jul 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

Dear,

You have to keep Primary and Foreign key Concept.
If you have then you can use following query

Select table1.name,table1.phone,table2.mobile FROM table1, table2
Where table1.PrimaryColumnName = table2.ForeignColmunName

If You Don have Then You can use Inner Join but it will not give you proper result

Thanks
dkmisra



Author: pradeep    05 Jul 2009Member Level: SilverRating: 2 out of 52 out of 5     Points: 2

use inner join


Author: Williams    05 Jul 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

Hi

If u not have minimum one common field to both the tables, its impossible to join two tables.



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : sample examples for the asp.net appications in c#
Previous : Auto generate
Return to Discussion Forum
Post New Message
Category: ASP.NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use