StuBlackett
StuBlackett

Reputation: 3855

AND clause after an OR clause

I am trying to running an MySQL Query with a "FIND_IN_SET" in it.

Running the query works, But I also need to find "user_optin" where it is equal to 1.

Running this query currently finds people who have user_optin as "0"

SELECT `user_id`, `user_firstname`, `user_surname`, `user_email`, `user_type`, `user_optin`
FROM (`users`)
WHERE `user_type` =  'Volunteer'
AND 1 <=FIND_IN_SET(1, user_interests)
OR 1 <=FIND_IN_SET(2, user_interests)
OR 1 <=FIND_IN_SET(5, user_interests)
OR 1 <=FIND_IN_SET(6, user_interests)
OR 1 <=FIND_IN_SET(7, user_interests)
OR 1 <=FIND_IN_SET(8, user_interests)
OR 1 <=FIND_IN_SET(10, user_interests)
AND 1 <=FIND_IN_SET(1, user_activities)
OR 1 <=FIND_IN_SET(2, user_activities)
OR 1 <=FIND_IN_SET(4, user_activities)
OR 1 <=FIND_IN_SET(5, user_activities)
OR 1 <=FIND_IN_SET(11, user_activities)
OR 1 <=FIND_IN_SET(12, user_activities)
OR 1 <=FIND_IN_SET(14, user_activities)
AND `user_optin` =  '1'

If I take the "OR's" out of the query the user_optin part of the query works.

Is there anyway at all I can make the user_optin explict to find a "1" entry, Not a "0" entry.

I didn't design this database and I realise that find_in_set is not the best method for MySQL database queries.

Cheers

Upvotes: 0

Views: 58

Answers (1)

ʰᵈˑ
ʰᵈˑ

Reputation: 11375

Add the other clauses within () to seperate them from the final AND.

SELECT
    `user_id`,
    `user_firstname`,
    `user_surname`,
    `user_email`,
    `user_type`,
    `user_optin`
FROM
    (`users`)
WHERE
    (
        `user_type` = 'Volunteer'
        AND 1 <= FIND_IN_SET(1, user_interests)
        OR 1 <= FIND_IN_SET(2, user_interests)
        OR 1 <= FIND_IN_SET(5, user_interests)
        OR 1 <= FIND_IN_SET(6, user_interests)
        OR 1 <= FIND_IN_SET(7, user_interests)
        OR 1 <= FIND_IN_SET(8, user_interests)
        OR 1 <= FIND_IN_SET(10, user_interests)
        AND 1 <= FIND_IN_SET(1, user_activities)
        OR 1 <= FIND_IN_SET(2, user_activities)
        OR 1 <= FIND_IN_SET(4, user_activities)
        OR 1 <= FIND_IN_SET(5, user_activities)
        OR 1 <= FIND_IN_SET(11, user_activities)
        OR 1 <= FIND_IN_SET(12, user_activities)
        OR 1 <= FIND_IN_SET(14, user_activities)
    )
AND `user_optin` = '1'

Upvotes: 1

Related Questions