Forum Moderators: open
below is my sql query but it seems there is some error with it.
SELECT FirstName, LastName, OID FROM Occupant WHERE OID = input OR FirstName = 'input' OR LastName = "input"
the datatype for my firstname & lastname is string. the oid is number. Im using ms access as my database.
this is the 'and' clause of my query. the and clause works fine but they will be ignored when i add in the or clause.
AND Address.AddID = PersonDetails.AddID AND PersonDetails.Status = 'active'
the database seems to ignore my 'and' clause at the back.Huh, what AND clause? What you posted only has ORs in it:
WHERE OID = input OR FirstName = 'input' OR LastName = "input"
they will be ignored when i add in the or clause.That is correct. The DB does not need to process every single clause in the where statement.
Consider this simplified example:
SELECT *
FROM MyTable
WHERE Field1=1 OR Field1=2 OR field1=3
If a row has a value of 2 for Field1 and 2 for Field2, the following will happen-
DB checks Field1=1 (false)
DB checks if Field1=2 (true)
DB doesn't need to check if Field1=3 because the statement is already true.
DB displays the row.
Now let's look at the following statement:
WHERE Field1=1 OR Field1=2 OR field1=3 AND Field2=1
DB checks Field1=1 (false)
DB checks if Field1=2 (true)
DB doesn't need to check if Field1=3 (or Field2=1) because the statement is already true.
DB displays the row.
What you need to do is add parenthesis. For the example, you probably want:
WHERE (Field1=1 OR Field1=2 OR field1=3) AND Field2=1
In this case:
DB checks Field1=1 (false)
DB checks if Field1=2 (true)
(DB doesn't need to check if Field1=3 because the parenthetical statement is already true.)
DB checks if Field2=1 (false)
DB does not display the row.
will the db check records field1=1 is true and proceed to check records field2=1 & check records field3=1
& these records will be compare with my AND Clause which compares the primary key of other table & display these records?
I know that this seems a little complicated but if this is possible, it would be great.