Q:

Mysql "order of operations" Question

Here's my situation: I want to SELECT all entries from a database WHERE id = $id.  But I want the results to be listed in a certain priority.  If, criteria = $criteria, then I want those results displayed first.  Otherwise, I just want to keep displaying the rest of the rows.  
My question is this: will this solve my problem?
SELECT field1 WHERE (criteria=$criteria AND id = $id) OR id=$id  LIMIT 5 

Will the query look at the () first? If not, is there another way to do this without splitting this into two separate queries?
Thanks,
Michael

A:

SELECT  field1
FROM    mytable
WHERE   id = $id
ORDER BY
        criteria = $criteria DESC, id
LIMIT 5

, or this:
SELECT  *
FROM    (
        SELECT  field1
        FROM    mytable
        WHERE   id = $id
                AND criteria = $criteria
        ORDER BY
                id
        LIMIT 5
        ) q
UNION
FROM    (
        SELECT  field1
        FROM    mytable
        WHERE   id = $id
        ORDER BY
                id
        LIMIT 5
        ) q2
ORDER BY
        criteria = $criteria DESC, id
LIMIT 5

The latter is more efficient if you have an index on (id, criteria) (in this order).

