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

为什么我仍然得到 user_uuid = 5b1f5ae5-b033-4b02-8bd0-3a9f7f2ccf19 的结果?!?

4

1 回答 1

2

因为AND比 具有更高的优先级OR

将您的OR子句括在括号中:

AND ((u.firstname ~* 'dir') OR (u.preposition ~* 'dir') OR (u.lastname ~* 'dir') OR (e.email ~* 'dir') OR (m.mobilenumber ~* 'dir'))
于 2010-12-27T17:40:13.087 回答