Jump to content

[SOLVED] SELECT statement were x or y are met


sibrows

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.