簡體   English   中英

不存在代碼的錯誤 - mariaDB

[英]Error with not exists code - mariaDB

我不確定為什么:

INSERT INTO $db.further_assess (taskid) VALUES ('id')
            WHERE NOT EXISTS (SELECT * FROM $db.further_assess where taskid='$id')

給我這個錯誤:

You have an error in your SQL syntax; check the manual that corresponds to 
your MariaDB server version for the right syntax to use near 'WHERE NOT 
EXISTS (SELECT 1 FROM risk_assessment.further_assess where taskid='222' at line 2

遵循這一點: 如果行不存在,則 Sql 插入

更新:

我的,現在正確,查詢:

        INSERT INTO $db.further_assess (taskid, reportid)
         SELECT '$id', '$report_id'
         FROM (SELECT 1) as dummytable
         WHERE NOT EXISTS (SELECT * FROM $db.further_assess where taskid='$id');

INSERT 語句沒有 WHERE 子句。 如果要運行條件 INSERT 語句,可以使用帶有虛擬表的 INSERT-SELECT 語句:

INSERT INTO $db.further_assess (taskid) 
   SELECT 'id'
   FROM (SELECT 1) as dummytable
   WHERE NOT EXISTS (SELECT * FROM $db.further_assess where taskid='$id')

做起來更簡單(也更快)

INSERT IGNORE  INTO $db.further_assess
    (taskid)
    VALUES
    ('$id')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM