SQL - 如何在Idempotent插入示例中使用连接而不是EXISTS和NOT EXISTS

时间:2013-06-11 20:14:46

标签: sql postgresql plpgsql

我遇到了这个用于进行幂等插入的github SQL代码示例。完全符合我的要求。我不想使用EXISTS因为我觉得它有点令人困惑。可以使用连接编码相同的操作吗?

以下是我在github上找到的相同代码:https://github.com/imtiazahmad007/SQL-Tips-Techniques/blob/master/Idempotent_Postgres_Inserts.sql

INSERT INTO users_articles (date, user_id, hit_count)
SELECT a.date, a.user_id, sum(a.article_id as hits)  
from (
select t.* from users_article_details AS t
WHERE NOT EXISTS (
    SELECT 1
    FROM do_not_email_users_tbl
    WHERE email = t.email
    LIMIT 1
)) a 
WHERE NOT EXISTS
(SELECT 1 from users_articles b
WHERE b.date = a.date)
GROUP BY date, user_id

2 个答案:

答案 0 :(得分:2)

我赞成使用外连接来做这种事情。尝试:

INSERT INTO USERS_ARTICLES (DATE, USER_ID, HIT_COUNT)
  SELECT a.DATE,
         a.USER_ID,
         SUM(a.ARTICLE_ID) AS HITS
    FROM USERS_ARTICLE_DETAILS a
    LEFT OUTER JOIN DO_NOT_EMAIL_USERS_TBL n
      ON (n.EMAIL = a.EMAIL)
    LEFT OUTER JOIN USERS_ARTICLES b
      ON (b.DATE = a.DATE)
    WHERE n.EMAIL IS NULL AND
          b.DATE IS NULL
    GROUP BY DATE, USER_ID

使用保留字DATE作为列名可能会导致问题 - 如果是这样,请用双引号括起DATE(例如“DATE”)。

分享并享受。

答案 1 :(得分:1)

INSERT INTO
  users_articles
  (date, user_id, hit_count)
SELECT
  uad.date,
  uad.user_id,
  SUM(uad.article_id) AS hits
FROM
  users_article_details uad
WHERE
  uad.email NOT IN (select email from do_not_email_user_table where email=uad.email) AND
  uad.date NOT IN (select `date` from users_articles where `date`=uad.date and user_id=uad.user_id)
GROUP BY
  uad.date,
  uad.user_id;