sibrows Posted February 8, 2009 Share Posted February 8, 2009 Hi All In simplified form I am trying to select records from two tables when x or y conditions are met. To explain I am building a very simple CMS with a table of what are effectively sub panes some are specific to the page that is viewed and the rest will be viewed on every page throughout the site. Current Tables ContentSub Content_ID (INT) Plugin_ID (INT) Default (VAR(1)) Plugins PluginID (INT) PluginName (VAR(50)) PluginPath (VAR(255)) Current Select Statement SELECT * FROM ContentSub, Plugins WHERE ContentSub.Content_ID = 'PreDefinedID' AND ContentSub.Plugin_ID = Plugins.PluginID OR ContentSub.Default = 'Y' AND ContentSub.Plugin_ID = Plugins.PluginID I'm sure this is just me misunderstanding the use of the OR statement but hey. Thanks in advance for your help. Regards Simon Quote Link to comment https://forums.phpfreaks.com/topic/144397-solved-select-statement-were-x-or-y-are-met/ Share on other sites More sharing options...
peranha Posted February 8, 2009 Share Posted February 8, 2009 Are you getting any errors? Not sure, but I believe you need to join the tables somehow. SELECT * FROM ContentSub CS INNER JOIN Plugins P ON CS.tablefield = P.tablefield WHERE CS.Content_ID = 'PreDefinedID' AND CS.Plugin_ID = P.PluginID OR CS.Default = 'Y' AND CS.Plugin_ID = P.PluginID Quote Link to comment https://forums.phpfreaks.com/topic/144397-solved-select-statement-were-x-or-y-are-met/#findComment-757740 Share on other sites More sharing options...
corbin Posted February 8, 2009 Share Posted February 8, 2009 Looks to me like you just needed parentheses in your original query. SELECT * FROM ContentSub, Plugins WHERE (ContentSub.Content_ID = 'PreDefinedID' AND ContentSub.Plugin_ID = Plugins.PluginID) OR (ContentSub.Default = 'Y' AND ContentSub.Plugin_ID = Plugins.PluginID) A join would work perfectly fine too though. Quote Link to comment https://forums.phpfreaks.com/topic/144397-solved-select-statement-were-x-or-y-are-met/#findComment-757746 Share on other sites More sharing options...
Mchl Posted February 9, 2009 Share Posted February 9, 2009 or SELECT * FROM ContentSub, Plugins WHERE ContentSub.Plugin_ID = Plugins.PluginID AND (ContentSub.Content_ID = 'PreDefinedID' OR ContentSub.Default = 'Y') Quote Link to comment https://forums.phpfreaks.com/topic/144397-solved-select-statement-were-x-or-y-are-met/#findComment-757803 Share on other sites More sharing options...
sibrows Posted February 11, 2009 Author Share Posted February 11, 2009 Many Thanks for all of your replies. Mchl, I ended up using your solution, now working like a dream. Regards Simon Quote Link to comment https://forums.phpfreaks.com/topic/144397-solved-select-statement-were-x-or-y-are-met/#findComment-759956 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.