dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersBibillis
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Forums » .NET » SQL Server »

Query for Two Table Data


Posted Date: 19 Jun 2012      Posted By:: Somnath     Member Level: Bronze    Member Rank: 4421     Points: 4   Responses: 3



Hi
I have 2 Tables
First Table is

Primary
--------------
PID | Name
1 Prime1
2 Prime2
3 Prime3
4 Prime4
5 Prime5

And Secound Table is

Mapping
---------------
StudentId PID
1 2
2 2
3 2
1 3
2 3

I want to write the Query for to get only those StudentId Which has PID 2 AND 3
Result Should Be


StudentID
------------
1
2

Please Help ASAP!
THANKS In Advance!





Responses

#676214    Author: Rajalingam      Member Level: Silver      Member Rank: 618     Date: 19/Jun/2012   Rating: 2 out of 52 out of 5     Points: 2

Hi Somnath,

use this query


select distinct s.studentid from firsttable f inner join secondtable s on s.pid=f.pid
where f.pid in(2,3)

hope it will help you



 
#676221    Author: Ultimaterengan        Member Level: Gold      Member Rank: 9     Date: 19/Jun/2012   Rating: 2 out of 52 out of 5     Points: 4

To Create tables:-

create table [Primary](PID int,name varchar(max))


create table Mapping(StudentId int,PID Int)


To insert values to tables

Insert into [Primary] values(1,'Prime1')
Insert into [Primary] values(2,'Prime2')
Insert into [Primary] values(3,'Prime3')
Insert into [Primary] values(4,'Prime4')
Insert into [Primary] values(5,'Prime5')


Insert into Mapping values(1,2)
Insert into Mapping values(2,2)
Insert into Mapping values(3,2)
Insert into Mapping values(1,3)
Insert into Mapping values(2,3)


To Get your result:-


SELECT
studentid ,
COUNT(studentid) AS NumberofTimes
FROM Mapping
GROUP BY studentid
HAVING ( COUNT(studentid) > 1 )






Thanks & Regards
G.Renganathan
Nothing is mine ,Everything is yours!!!


http://renganathan1984.blogspot.com/



 
#676401    Author: kirthiga      Member Level: Gold      Member Rank: 219     Date: 20/Jun/2012   Rating: 2 out of 52 out of 5     Points: 2

Hi Somnath,

Try this query


select StudentId from Mapping
where PID=3 and StudentId in (select StudentId from Mapping where PID =2)






 
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 : How will get table structure without using truncate
Previous : Single cell values count in sql server 2005
Return to Discussion Forum
Post New Message
Category:

Related Messages



Follow us on Twitter: https://twitter.com/dotnetspider

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.