Click to See Complete Forum and Search --> : SQL query: do not return row if ALL fields match, but OK if some match


harlock
11-20-2005, 06:28 AM
I have four values.

I have a select query where I want to ask the database to give me rows only where ALL four of these values don't match up, but if any subset is matching to still let it through.

So if value1, value2, and value3 all match their counterparts in the database entry but value4 is different, it will still return that row as a search result.

If all four values are identical, it will not return that row.

I'm running into a snag where if I just use "and value1 != $value1 and value2 != $value2 and value3 != $value3 and value4 != $value4" then of course it will not return any entry with any ONE Of those values matching. If I put that section in parenthesis, it does not help.

So I'm looking for some syntax where if ALL those parameters match, the row isn't returned in the search results. But if any three or less happen to match up, include the row in the search results. So some kind of encapsulation for that part of the query. Does it need to be a sub-query? Haven't messed with nested queries too much yet...

NogDog
11-20-2005, 02:41 PM
If I'm understanding correctly:

...WHERE (value1 != $v1 OR value2 != $v2 OR value3 != $v3 OR value4 != $v4) AND
(value1 = $v1 OR value2 = $v2 OR value3 = $v3 OR value4 = $v4)...

This should return each row where 1, 2, or 3 of the values match, but not if all 4 match or none of the 4 match.

harlock
11-20-2005, 11:08 PM
Actually I do want it to return the row if none of the four match as well. What would that look like? Someone else suggested to me simply:

not (value1 = $value1 and value2 = $value2 and value3 = $value3 and value4 = $value4)

but that didn't seem to work, it still returned values where all four matched.

(it's placed in as 'and not (value1 = $value1...)' after some other ands.

NogDog
11-21-2005, 12:10 AM
...WHERE (value1 != $v1 OR value2 != $v2 OR value3 != $v3 OR value4 != $v4)...

The parens are needed (but can be left in with no problems) only if you have other conditions you want to include in your WHERE clause.

harlock
11-21-2005, 12:29 AM
Yes there are other conditions, so I have left the parentheses in.

I tried your new settings, which seem to make perfect sense, but it still returns rows where all four are equal.

These pieces of data are floats. The values appear to be identical to what's in the DB, no extra trailing or leading zeros. Still, should I use some kind of function within the query to make sure it's comparing apples and apples?

NogDog
11-21-2005, 01:09 AM
Make sure you AND the entire condition set above (within the parens) with your other conditions, i.e.:

WHERE (value1 != $v1 OR ... value4 != $v4) AND (...other conditions here...)

harlock
11-21-2005, 01:15 AM
OK I found the problem. Float inaccuracy. Changed it to Decimal for my purposes, should be fine. If I was more of a math geek I'd have realized that before now.