Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OR doesn't work logically (simple but urgent)

Status
Not open for further replies.

rastkocvetkovic

Programmer
Aug 11, 2002
63
SI
This is my SQL query:

SELECT * FROM News WHERE (id IN (SELECT news_id FROM LinkingCategoriesNews WHERE news_id = 1) AND active= 1) OR (id = 82) ORDER BY date DESC

I suppose that it should firstly select all News that have ID's in LinkingCategoriesNews. That works, but it is problem that it just selects those. The News with id = 82 isn't included in output. Isn't that how OR is supposed to work? What seems to be the problem? Thanks in advance!

P.S.: I use MS Access over ODBC.
 
Without testing this out, I think you need to have the entire OR clause enclosed in parens. You have your AND statement enclosed but not the OR.
 
Hmm, after your replay I also tried this:

SELECT * FROM News WHERE ((id IN (SELECT news_id FROM LinkingCategoriesNews WHERE category_id = 1)) AND (active= 1)) OR (id = 51) ORDER BY date DESC

But the result is the same. Did you mean this or something else?
 
the syntax is fine, it should return the row from news with id=82, and if it doesn't, then do this:

based on how it's currently coded with the OR, break the query apart into two queries:

Code:
SELECT 'query 1'
     , News.* 
  FROM News
 WHERE (id IN 
         (SELECT news_id 
            FROM LinkingCategoriesNews 
           WHERE news_id = 1) 
   AND active= 1)

and

Code:
SELECT 'query 2'
     , News.* 
  FROM News
 WHERE (id = 82)

this should tell you all you need to know   ;-)

rudy
[URL unfurl="true"]http://rudy.ca/[/URL]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top