EchoFool Posted January 22, 2010 Share Posted January 22, 2010 Hey I have an inner join that keeps returning 0 rows when it should not - im sure its just my approach to this is incorrect so was hopeing some one could correct what i got wrong firslty heres my query: SELECT buildings.Disabled,buildings.BuildingName,buildings.Cost,buildings.Bills,buildings.Size,buildings.RecordID FROM buildings INNER JOIN research ON buildings.ResearchID=research.ResearchID WHERE (buildings.ResearchID='0' OR (buildings.ResearchID>0 AND research.Completed='1' AND research.UserID='$UserID')) ORDER BY buildings.RecordID ASC What its suppose to do, is load buildings from the buildings table, which has a ResearchID field of either zero or an id above 0. If the research id is 0 then it doesn't need to check the research table related to the user, so those buildings should load regardless. And if it does have a ResearchID above 0 then check the research table to check the research is completed by the user and if so - load the building. But currently no rows return at all when it should, hope you can help see what i got wrong! Quote Link to comment https://forums.phpfreaks.com/topic/189390-inner-join-not-working/ Share on other sites More sharing options...
kickstart Posted January 22, 2010 Share Posted January 22, 2010 Hi Is here a ResearchID of '0' on the research table? If not then you will never get any records returned where ResearchID is 0 (you would need a LEFT OUTER JOIN for that). Can't see anything else obvious without looking at the data. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/189390-inner-join-not-working/#findComment-999808 Share on other sites More sharing options...
EchoFool Posted January 22, 2010 Author Share Posted January 22, 2010 Hi Is here a ResearchID of '0' on the research table? If not then you will never get any records returned where ResearchID is 0 (you would need a LEFT OUTER JOIN for that). Can't see anything else obvious without looking at the data. All the best Keith There are IDs defaulted to 0 but not in the research table. 0 means it doesnt require it to be linked to the research tabele. Quote Link to comment https://forums.phpfreaks.com/topic/189390-inner-join-not-working/#findComment-999992 Share on other sites More sharing options...
kickstart Posted January 22, 2010 Share Posted January 22, 2010 Hi In which case on you join no row will be brought back when buildings.ResearchID='0' (so no point in having it in the WHERE clause). If you do want them brought back then:- SELECT buildings.Disabled,buildings.BuildingName,buildings.Cost,buildings.Bills,buildings.Size,buildings.RecordID FROM buildings LEFT OUTER JOIN research ON buildings.ResearchID=research.ResearchID WHERE (buildings.ResearchID='0' OR (buildings.ResearchID>0 AND research.Completed='1' AND research.UserID='$UserID')) ORDER BY buildings.RecordID ASC All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/189390-inner-join-not-working/#findComment-999999 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.