Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
JBRTaylor
Starting Member
5 Posts |
Posted - 2013-06-15 : 04:10:24
|
I am enjoying the steep learning curve that is sql after using it in access but have not got stuck again on how to make a many to many query work for me. I have been told a stored procedure may be the way forward.For the example i am trying to get working i have a persons table and a transport table with a link table in between. If i search for more than one method of transport i get multiple occurrences of each person (one for each method of transport they use) however i would like to only recive one result for each person because as long as i know a person uses that particular transport then it is just the person record i am interested in..In other words using this example below from access if i do not want to display the criteria i am searching, only the results from the people table how would i do this.SELECT tblPerson.PersonID, tblPerson.PersonFROM tblPerson INNER JOIN tblLink ON tblPerson.PersonID = tblLink.PersonFIDWHERE (((tblLink.TransportFID)=3)) OR (((tblLink.TransportFID)=1)); Many Thanks in advanceJon |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-06-15 : 09:22:10
|
Use of the EXISTS clause might be the most straightforward option:SELECT tblPerson.PersonID, tblPerson.PersonFROM tblPerson WHERE EXISTS ( SELECT * FROM tblLink WHERE tblLink.TransportFID in (1,3) AND tblPerson.PersonID = tblLink.PersonFID ); |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-06-15 : 09:25:42
|
[code]SELECT p.PersonID, p.PersonFROM dbo.tblPerson AS pINNER JOIN ( SELECT DISTINCT PersonFID FROM dbo.tblLink WHERE TransportFID IN (1, 3) ) AS w ON w.PersonFID = p.PersonID[/code] N 56°04'39.26"E 12°55'05.63" |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-15 : 13:05:40
|
[code]SELECT p.PersonID, p.PersonFROM dbo.tblPerson AS pINNER JOIN dbo.tblLink AS lON l.PersonFID = p.PersonIDGROUP BY p.PersonID, p.PersonHAVING SUM(CASE WHEN l.TransportFID IN (1, 3) THEN 1 ELSE 0 END) >0[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
JBRTaylor
Starting Member
5 Posts |
Posted - 2013-06-16 : 03:28:32
|
Hi GuysThanks for your help that is great. I have tried using the code in PHPRunner which i am using to build my front end and it didn't work for me but within SQL studio it worked great.I think i need to go away and learn how to write PHP, wow that is gonna be a challenge. Why do i do it to myself, lol. Thanks for your help and no doubt i'll be back for more advice.ThanksJon |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-16 : 14:21:25
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|