我正在尝试执行以下查询:
INSERT INTO table_listnames (name, address, tele) VALUES ('Rupert', 'Somewhere', '022') WHERE NOT EXISTS ( SELECT name FROM table_listnames WHERE name='value' );
但这会返回错误.基本上我不想插入记录,如果记录的'name'字段已存在于另一条记录中 - 如何检查新名称是否唯一?
我实际上并没有建议你这样做,因为UNIQUE
Piskvor和其他人建议的索引是一个更好的方法,但你实际上可以做你正在尝试的事情:
CREATE TABLE `table_listnames` ( `id` int(11) NOT NULL auto_increment, `name` varchar(255) NOT NULL, `address` varchar(255) NOT NULL, `tele` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB;
插入记录:
INSERT INTO table_listnames (name, address, tele) SELECT * FROM (SELECT 'Rupert', 'Somewhere', '022') AS tmp WHERE NOT EXISTS ( SELECT name FROM table_listnames WHERE name = 'Rupert' ) LIMIT 1; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 SELECT * FROM `table_listnames`; +----+--------+-----------+------+ | id | name | address | tele | +----+--------+-----------+------+ | 1 | Rupert | Somewhere | 022 | +----+--------+-----------+------+
尝试再次插入相同的记录:
INSERT INTO table_listnames (name, address, tele) SELECT * FROM (SELECT 'Rupert', 'Somewhere', '022') AS tmp WHERE NOT EXISTS ( SELECT name FROM table_listnames WHERE name = 'Rupert' ) LIMIT 1; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 +----+--------+-----------+------+ | id | name | address | tele | +----+--------+-----------+------+ | 1 | Rupert | Somewhere | 022 | +----+--------+-----------+------+
插入不同的记录:
INSERT INTO table_listnames (name, address, tele) SELECT * FROM (SELECT 'John', 'Doe', '022') AS tmp WHERE NOT EXISTS ( SELECT name FROM table_listnames WHERE name = 'John' ) LIMIT 1; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 SELECT * FROM `table_listnames`; +----+--------+-----------+------+ | id | name | address | tele | +----+--------+-----------+------+ | 1 | Rupert | Somewhere | 022 | | 2 | John | Doe | 022 | +----+--------+-----------+------+
等等...
INSERT WHERE
在语法中不允许.
你可以做什么:UNIQUE INDEX
在字段上创建一个唯一的(name
),然后使用:
正常INSERT
(如果名称已存在,则处理错误)
INSERT IGNORE
(如果名称已经存在,它将无声地失败导致警告(而不是错误))
INSERT ... ON DUPLICATE KEY UPDATE
(UPDATE
如果名称已存在,将在最后执行,请参阅文档)
工作:
INSERT INTO users (full_name, login, password) SELECT 'Mahbub Tito','tito',SHA1('12345') FROM DUAL WHERE NOT EXISTS (SELECT login FROM users WHERE login='tito');
MySQL提供了非常可爱的解决方案:
REPLACE INTO `table` VALUES (5, 'John', 'Doe', SHA1('password'));
由于您已声明了唯一的主键(此处值为5),因此非常易于使用.
INSERT IGNORE INTO `mytable` SET `field0` = '2', `field1` = 12345, `field2` = 12678;
这里是mysql查询,即插入记录(如果不存在)并将忽略现有的类似记录.
----Untested----
您可以轻松使用以下方式:
INSERT INTO ... ON DUPLICATE KEY UPDATE ...
通过这种方式,您可以插入任何新原始数据,如果您有重复数据,则替换特定列(最佳列是时间戳).
例如 :
CREATE TABLE IF NOT EXISTS Devices ( id INT(6) NOT NULL AUTO_INCREMENT, unique_id VARCHAR(100) NOT NULL PRIMARY KEY, created_at VARCHAR(100) NOT NULL, UNIQUE KEY unique_id (unique_id), UNIQUE KEY id (id) ) CHARACTER SET utf8 COLLATE utf8_unicode_ci; INSERT INTO Devices(unique_id, time) VALUES('$device_id', '$current_time') ON DUPLICATE KEY UPDATE time = '$current_time';
如果你真的无法在桌面上获得唯一索引,你可以尝试......
INSERT INTO table_listnames (name, address, tele) SELECT 'Rupert', 'Somewhere', '022' FROM some_other_table WHERE NOT EXISTS (SELECT name FROM table_listnames WHERE name='Rupert') LIMIT 1;
为了克服类似的问题,我已经使我插入的表具有唯一的列.使用你的例子,在创作时我会有类似的东西:
name VARCHAR(20), UNIQUE (name)
然后在插入时使用以下查询:
INSERT IGNORE INTO train set table_listnames='Rupert'
此查询效果很好:
INSERT INTO `user` ( `username` , `password` ) SELECT * FROM (SELECT 'ersks', md5( 'Nepal' )) AS tmp WHERE NOT EXISTS (SELECT `username` FROM `user` WHERE `username` = 'ersks' AND `password` = md5( 'Nepal' )) LIMIT 1
您可以使用以下查询创建表:
CREATE TABLE IF NOT EXISTS `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(30) NOT NULL, `password` varchar(32) NOT NULL, `status` tinyint(1) DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
注意:在尝试使用第一个查询之前,使用第二个查询创建表.