我有两张桌子:
purchase_mis(id, user_id, total_purchased, date)
daily_purchase(id, user_id, product_id, paid_amount, purchase_date)
我有一个每天晚上运行的 CRON 文件,它计算"daily_purchase"
表中的每日购买量并运行insert into "purchase_mis"
.
例如:
SELECT
COUNT(*) AS purchase_count,
purchase_date
FROM daily_purchase
GROUP BY user_id;
这purchase_count
将为每个用户返回 ,然后将其插入到"purchase_mis"
表中。
INSERT INTO
purchase_mis(user_id, total_purchased, date)
VALUES
('2', 'purchase_count', 'purchase_date');
但是在插入之前,需要检查user_id = 2
某个日期“ purchase_date
”的购买信息是否已经插入,所以不要再次插入。
我想要类似下面的查询:
INSERT INTO
purchase_mis(user_id, total_purchased, date)
VALUES
('2', 'purchase_count', 'purchase_date')
WHERE date NOT EXISTS (SELECT date FROM purchase_mis WHERE user_id = '2');