PostgreSQL SELECT 减去数组(NOT IN)

发布于 2024-10-09 11:55:04 字数 1746 浏览 6 评论 0原文

  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 的结果?!?

  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 ?!?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帝王念 2024-10-16 11:55:04

因为AND的优先级高于OR

OR 子句括在括号中:

AND ((u.firstname ~* 'dir') OR (u.preposition ~* 'dir') OR (u.lastname ~* 'dir') OR (e.email ~* 'dir') OR (m.mobilenumber ~* 'dir'))

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'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文