如何在MySQL中创建触发器以仅在不存在时插入新行?

发布于 2025-01-14 00:53:50 字数 1276 浏览 0 评论 0原文

我想要的是仅当该日期不存在时才插入新行,则应将其忽略。该表没有主键和唯一键,我在此表中使用它bar_opening_details。 我无法在此处添加这样的主要和唯一约束。INSERT IGNORE。 因为每天都会明智地输入具有相同 item_id 的项目。

CREATE definer=`root`@`localhost` TRIGGER `store`.`bar_recvd_details_after_insert` beforeINSERT
on `bar_recvd_details` FOR each row BEGIN
DECLARE mydate  date;DECLARE mydate1 DATE;DECLARE myid    INT;SELECT Max(close_date)
INTO   mydate
FROM   bar_opening_details;SELECT item_id
INTO   myid
FROM   bar_opening_details
WHERE  op_date=mydate
AND    item_id=new.item_id;SELECT op_date
INTO   mydate1
FROM   bar_opening_details
WHERE  op_date=mydate
AND    item_id=new.item_id;IF(myid != new.item_id
AND
mydate1 != mydate) then
INSERT INTO `store`.`bar_opening_details`
            (
                        `item_cid`,
                        `item_id`,
                        `op_date`,
                        `op_value`,
                        `close_date`,
                        `close_val`
            )
            VALUES
            (
                        new.item_cid,
                        new.item_id,
                        mydate,
                        '0',
                        mydate,
                        '0'
            );ENDIF;END 

what I want is to insert a new row only if it does not exist on that date then it should be ignored. this table does not have the primary and unique key I am using it in this table bar_opening_details.
I can't add such primary and unique constraints here.to INSERT IGNORE.
since this daily wise entry of items with the same item_id.

CREATE definer=`root`@`localhost` TRIGGER `store`.`bar_recvd_details_after_insert` beforeINSERT
on `bar_recvd_details` FOR each row BEGIN
DECLARE mydate  date;DECLARE mydate1 DATE;DECLARE myid    INT;SELECT Max(close_date)
INTO   mydate
FROM   bar_opening_details;SELECT item_id
INTO   myid
FROM   bar_opening_details
WHERE  op_date=mydate
AND    item_id=new.item_id;SELECT op_date
INTO   mydate1
FROM   bar_opening_details
WHERE  op_date=mydate
AND    item_id=new.item_id;IF(myid != new.item_id
AND
mydate1 != mydate) then
INSERT INTO `store`.`bar_opening_details`
            (
                        `item_cid`,
                        `item_id`,
                        `op_date`,
                        `op_value`,
                        `close_date`,
                        `close_val`
            )
            VALUES
            (
                        new.item_cid,
                        new.item_id,
                        mydate,
                        '0',
                        mydate,
                        '0'
            );ENDIF;END 

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

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

发布评论

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

评论(1

深空失忆 2025-01-21 00:53:50

就这样解决了

CREATE DEFINER=`root`@`localhost` TRIGGER `store`.`bar_recvd_details_BEFORE_INSERT` BEFORE INSERT ON `bar_recvd_details` FOR EACH ROW
BEGIN
declare mydate date;
SELECT max(close_date) into mydate FROM bar_opening_details;
 
 IF NOT EXISTS (SELECT 1 FROM bar_opening_details WHERE item_id = NEW.item_id and op_date=mydate) THEN
    INSERT INTO bar_opening_details (item_cid,
item_id,
op_date,
op_value,
close_date,
close_val)
    VALUES (NEW.item_cid,NEW.item_id,mydate,'0',mydate,'0');
END IF;
END

Solved it by this

CREATE DEFINER=`root`@`localhost` TRIGGER `store`.`bar_recvd_details_BEFORE_INSERT` BEFORE INSERT ON `bar_recvd_details` FOR EACH ROW
BEGIN
declare mydate date;
SELECT max(close_date) into mydate FROM bar_opening_details;
 
 IF NOT EXISTS (SELECT 1 FROM bar_opening_details WHERE item_id = NEW.item_id and op_date=mydate) THEN
    INSERT INTO bar_opening_details (item_cid,
item_id,
op_date,
op_value,
close_date,
close_val)
    VALUES (NEW.item_cid,NEW.item_id,mydate,'0',mydate,'0');
END IF;
END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文