如果类似的值存在于另一个现有表中(而不是外键),则插入到表中

时间:2021-04-26 12:28:57

I'm trying to insert an "Item order" in a table called AsksFor and I want to make sure the Item and ItemManufacturer exists in the table Sells. However I keep getting "syntax error, unexpected if, expecting END_OF_INPUT or ';'" for using the IF. Anyone know any other ways to write this for MySQL?

我试图在一个名为AsksFor的表中插入一个“Item order”,我想确保这个项目和ItemManufacturer存在于表中。然而,我总是得到“语法错误,如果,期望END_OF_INPUT或';”,“如果使用if”。有人知道用其他方法来写MySQL吗?

INSERT INTO AsksFor (Username, ItemName, ItemManufacturer)
VALUES ('Harish', 'zkoxtlv93', 'tbzrt93')
IF EXISTS(SELECT ItemName, ItemManufacturer
        FROM Sells
        WHERE Sells.ItemName = VALUES(ItemName)
        AND Sells.ItemManufacturer = VALUES(ItemManufacturer));

1 个解决方案

#1


2  

EXISTS clause is not availaible for MySQL . Anyways you don't need it , the AND condition in WHERE clause performs the checking part whether values exists in source table Sells.

对于MySQL来说,现有的条款是无效的。无论您不需要它,在WHERE子句中执行检查部分的条件和条件是否在源表中存在。

Try this

试试这个

INSERT INTO AsksFor (Username, ItemName, ItemManufacturer)
SELECT DISTINCT 'Harish',ItemName, ItemManufacturer
        FROM Sells
        WHERE ItemName='zkoxtlv93' AND ItemManufacturer='tbzrt93'

#1


2  

EXISTS clause is not availaible for MySQL . Anyways you don't need it , the AND condition in WHERE clause performs the checking part whether values exists in source table Sells.

对于MySQL来说,现有的条款是无效的。无论您不需要它,在WHERE子句中执行检查部分的条件和条件是否在源表中存在。

Try this

试试这个

INSERT INTO AsksFor (Username, ItemName, ItemManufacturer)
SELECT DISTINCT 'Harish',ItemName, ItemManufacturer
        FROM Sells
        WHERE ItemName='zkoxtlv93' AND ItemManufacturer='tbzrt93'