1
  SELECT DISTINCT ON
      (u.user_uuid, u.firstname)
      u.user_uuid,
      u.firstname,
      u.preposition,
      u.lastname,
      array_to_string_ex(ARRAY(SELECT email FROM emails WHERE user_uuid =
 u.user_uuid)) as emails,
      array_to_string_ex(ARRAY(SELECT mobilenumber FROM mobilenumbers WHERE
 user_uuid = u.user_uuid)) as
 mobilenumbers,
      array_to_string_ex(ARRAY(SELECT c.name FROM targetgroupusers AS tgu
 LEFT JOIN membercategories as mc ON
 mc.targetgroup_uuid =
 tgu.targetgroup_uuid LEFT JOIN
 categories AS c ON mc.category_uuid =
 c.category_uuid WHERE tgu.user_uuid =
 u.user_uuid)) as categories,
      array_to_string_ex(ARRAY(SELECT color FROM membercategories WHERE
 targetgroup_uuid IN(SELECT
 targetgroup_uuid FROM targetgroupusers
 WHERE user_uuid = u.user_uuid))) as
 colors  FROM
      membercategories AS mc  LEFT JOIN
      targetgroups AS tg  ON
      tg.targetgroup_uuid = mc.targetgroup_uuid  LEFT JOIN
      targetgroupusers AS tgu  ON
      tgu.targetgroup_uuid = tg.targetgroup_uuid  LEFT JOIN
      users AS u  ON
      u.user_uuid = tgu.user_uuid  LEFT JOIN
      emails AS e  ON
      e.user_uuid = u.user_uuid  LEFT JOIN
      mobilenumbers AS m  ON
      m.user_uuid = u.user_uuid  WHERE   mc.targetgroup_uuid IS NOT NULL  AND
 u.user_uuid !=
 ANY('{5b1f5ae5-b033-4b02-8bd0-3a9f7f2ccf19}'::UUID[])
 AND   isdeleted = false  AND
      mc.company_uuid = '5edcecd0-0534-11e0-81e0-0800200c9a66'
      AND (u.firstname ~* 'dir') OR (u.preposition ~* 'dir') OR
 (u.lastname ~* 'dir') OR (e.email ~*
 'dir') OR (m.mobilenumber ~* 'dir')
        ORDER BY
     u.firstname ASC
 OFFSET      0
 LIMIT      10

How comes i still get the result with user_uuid = 5b1f5ae5-b033-4b02-8bd0-3a9f7f2ccf19 ?!?

1 Answer 1

2

Because AND has higher precedence than OR.

Enclose your OR clauses into parentheses:

AND ((u.firstname ~* 'dir') OR (u.preposition ~* 'dir') OR (u.lastname ~* 'dir') OR (e.email ~* 'dir') OR (m.mobilenumber ~* 'dir'))
Sign up to request clarification or add additional context in comments.

2 Comments

Oops, i am sorry, i already changed that, but still doesnt work! =(
@Koen: Could you please post the query which "still doesn't work"? Also it would help much if you posted the records which you think shouldn't match but they do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.