2013-05-10 67 views
1

當使用EXISTS函數時,我從mysql獲取錯誤。我會去ON DUPLICATE KEY路線,但'str'字段必須是TEXT類型。MySQL EXISTS()錯誤

這裏是我的表:

CREATE TABLE `locale_display_string` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, 
    `str` TEXT NOT NULL, 
    `date` INT(11) NOT NULL DEFAULT '0', 
    PRIMARY KEY (`id`) 
) 
TYPE=MyISAM; 

下面是該查詢我試圖運行:

INSERT INTO `locale_display_string` (str, date) VALUES ('test', '1368204427') 
WHERE NOT EXISTS (SELECT * FROM `locale_display_string` WHERE str='test'); 

我在做什麼錯?

+1

錯誤是什麼? – Sudz 2013-05-10 17:40:37

+1

我不認爲MySQL INSERT語法支持WHERE子句 – mask8 2013-05-10 17:41:16

+0

我什至不能做'SELECT EXISTS(SELECT * FROM locale_display_string);' – skeelsave 2013-05-10 17:42:26

回答

2

VALUES不兼容...使用SELECT

INSERT INTO `locale_display_string` (str, date) 
SELECT 'test', '1368204427' 
FROM `locale_display_string` 
WHERE NOT EXISTS(SELECT TRUE FROM `locale_display_string` WHERE str='test') 
LIMIT 1