插入表中如果另一个现有表中存在相似的值(不是外键) [英] Insert Into table If similar value exists in another existing table (not a foreign key)

查看:77
本文介绍了插入表中如果另一个现有表中存在相似的值(不是外键)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在名为AsksFor的表中插入商品订单,并且我想确保商品和商品制造商存在于Sells表中。但是,使用IF时,我不断收到语法错误,如果是END_OF_INPUT或‘;’,这是意外的。有人知道其他为MySQL编写此方法的方法吗?

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?

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));


推荐答案

EXISTS子句不适用于MySQL。无论如何您都不需要它,WHERE子句中的AND条件执行检查部分,源表 Sells 中是否存在值。

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.

尝试一下

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

这篇关于插入表中如果另一个现有表中存在相似的值(不是外键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆