vBulletin Search Engine Optimization
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| I am struggling with a query which is stretching my sql (geddit?) knowledge to breaking point. I am storing data about railway accidents and accident reports. I have the following tables: tbllocations - stores locations of accidents tblcompanies - stores the train operating companies for the accident tblcauses - stores causes of accidents tblresults - stores results of accidents tblevents - stores the accidents themselves tbldocuments - stores accident reports Relationships are as follows: tbllocations 1-to-many tblevents tblcompanies many-to-many tblevents tblcauses many-to-many tblevents tblresults many-to-many tblevents tblevents 1-to-many tbldocuments therefore the many-to-many relationships have join tables, ie tblcauseventjoin - fields causeID, eventID tblresulteventjoin - fields resultID, eventID tblcompanyeventjoin - fields companyID, eventID I want to return only accidents which have a particular cause, a particular company, a particular result, and where a related document has been published (denoted by booPublished field in tbldocuments). I have the following sql, which at first glance appears to work, but which throws up the odd false negative: SELECT tbllocations.txtLocation, ALIAS1.eventID, ALIAS1.datDate FROM tbllocations INNER JOIN ( tblevents AS ALIAS1 INNER JOIN tblcompanyeventjoin ON ALIAS1.eventID = tblcompanyeventjoin.eventID ) ON tbllocations.locationID = ALIAS1.locationID INNER JOIN ( tblevents AS ALIAS2 INNER JOIN tblcauseeventjoin ON ALIAS2.eventID = tblcauseeventjoin.eventID ) ON tbllocations.locationID = ALIAS2.locationID INNER JOIN ( tblevents AS ALIAS3 INNER JOIN tblresulteventjoin ON ALIAS3.eventID = tblresulteventjoin.eventID ) ON tbllocations.locationID = ALIAS3.locationID WHERE tbllocations.locationID = '391' AND tblcompanyeventjoin.companyID = '22' AND tblcauseeventjoin.causeID = '1' AND tblresulteventjoin.resultID = '3' AND tbldocuments.booPublished = 'yes' GROUP BY ALIAS1.eventID Can anyone tell me a) where I am going wrong and, more importantly, b) why? GazK |