我試圖向MySQL表中插入新行,但只有當我插入的值之一不在已經在表中的行中。插入新行,除非其中一個值與另一行匹配?
舉例來說,如果我做的:
insert into `mytable` (`id`, `name`) values (10, `Fred`)
我希望能夠進行檢查,看是否有其他行的表已name = 'Fred'
。如何才能做到這一點?
謝謝!
編輯
我想什麼(不能發佈確切的說法,但這裏有一個代表):
INSERT IGNORE INTO mytable (`domain`, `id`)
VALUES ('i.imgur.com', '12gfa')
WHERE '12gfa' not in (
select id from mytable
)
會拋出錯誤:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE '12gfa' not in (select id from mytable)' at line 3
你嘗試任何方法?你看過「if」關鍵字嗎? – Oded
我試着用'不在'然後做一個選擇,但它似乎沒有工作。 – Jake
我對'if'關鍵字不熟悉 – Jake