Jump to content

[SOLVED] SELECT statement were x or y are met


Recommended Posts

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

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.