Not retrieving correct results with mysql query using parentheses

MultiDev

I am trying to query all valid discounts from a mysql table.

It is setup like:

ID    ItemID    ProviderID    ConditionID    GradeID    DiscLimit    DiscRedeemed    DiscExp

1     0         0             3              0          10           10              0000-00-00 00:00:00

Where DiscExp is a datetime.

Only one of ItemID, ProviderID, ConditionID or GradeID would ever have a value other than 0 at any given time.

I am trying to query all valid discounts, where the "limit" hasn't been reached DiscRedeemed < DiscLimit and it hasn't expired DiscExp < NOW()

Here is the query:

$query = $db->query("SELECT * FROM `store_item_discounts` WHERE 
        (ItemID > 0 AND ItemID = '$item[ID]')
        OR (ProviderID > 0 AND ProviderID = '$item[Provider]')
        OR (ConditionID > 0 AND ConditionID = '$item[ItemCondition]')
        OR (GradeID > 0 AND GradeID = '$item[ItemGrade]')
        AND (DiscLimit = 0 OR DiscRedeemed < DiscLimit) 
        AND (DiscExp = '0000-00-00 00:00:00' OR DiscExp > NOW())        
        ORDER BY DiscType ASC");

Of the $item array, all values are 0 except for $item['ItemCondition'] which equals 3.

For some reason, this is returning the result as shown above, where the limit has been reached. Also it doesn't filter out the dates or other variables correctly.

I obviously have something wrong with my query, but I can't figure it out. Any suggestions?

pamelus

You're facing a problem with operator precedence. Since AND operator has higher precedence than OR operator, you WHERE clause really looks like:

WHERE 
    (
        (ItemID > 0 AND ItemID = '$item[ID]')
        OR (ProviderID > 0 AND ProviderID = '$item[Provider]')
        OR (ConditionID > 0 AND ConditionID = '$item[ItemCondition]')
    ) OR (
        (GradeID > 0 AND GradeID = '$item[ItemGrade]')
        AND (DiscLimit = 0 OR DiscRedeemed < DiscLimit) 
        AND (DiscExp = '0000-00-00 00:00:00' OR DiscExp > NOW())
    )

So ConditionID = 3 is a sufficient condition to return a row. As Uueerdo pointed out, you need to embrace all your OR conditions in another pair of parenthesis. Your query should then look like:

$query = $db->query("SELECT * FROM `store_item_discounts` WHERE 
    (
        (ItemID > 0 AND ItemID = '$item[ID]')
        OR (ProviderID > 0 AND ProviderID = '$item[Provider]')
        OR (ConditionID > 0 AND ConditionID = '$item[ItemCondition]')
        OR (GradeID > 0 AND GradeID = '$item[ItemGrade]')
    ) AND (DiscLimit = 0 OR DiscRedeemed < DiscLimit) 
    AND (DiscExp = '0000-00-00 00:00:00' OR DiscExp > NOW())        
    ORDER BY DiscType ASC");

See MySQL documentation to learn more about operator precedence.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Not retrieving correct results with mysql query using parentheses

From Dev

Geodjango distance query not retrieving correct results

From Dev

Retrieving results based on timeslots using MySQL

From Dev

timestamp query not retrieving results?

From Dev

results.length is not give the correct output for MySQL query?

From Dev

Ordering results of MySQL query (using join)

From Dev

MySQL Query: Using group by and getting unreal results

From Dev

Issue Retrieving results from SQL query in PHP

From Dev

MySQL - Using results from one query in another query

From Dev

Using results in one query to use in another query - Orace MySql

From Dev

Multiple AND/OR Query Not Yielding Correct Results

From Dev

sql query not returning correct results

From Dev

Why is this MySQL query correct?

From Dev

MySQL Query not correct

From Dev

Correct result for MySQL query using LEFT JOIN and History table

From Dev

Versioning for MySQL Query Results

From Dev

MySQL fallback query if no results

From Dev

Inaccurate results with MySql query

From Dev

MySQL query fliltering results

From Dev

MySQL query filtering results

From Dev

Utilizing MySQL query results

From Dev

Array with MySQL query results

From Dev

Transpose the results of a MySQL query

From Dev

Inaccurate results with MySql query

From Dev

Mysql query returns no results

From Dev

mysql query to fetch these results

From Dev

Using PHP SESSION Variables to store MySQL query results

From Dev

Parameterized Query Returns no Results using C++ .Net MySQL Connector

From Dev

Overlapping results on mysql query when using limit/offset