insert into student (FirstName,AGE,CITYID) values('guna','26','1')
select * from student WHERE FirstName!='guna';
此查询显示错误。我无法将FirstName列设为唯一。请提出除此之外的想法。
由于
答案 0 :(得分:3)
INSERT INTO student ( ....)
WHERE FirstName NOT IN (SELECT FirstName FROM student)
经过修订和测试:
INSERT INTO student
(FirstName, age, cityid)
SELECT
'guna','26','1'
FROM student -- any table name will do
WHERE 'guna' NOT IN
(
SELECT FirstName
FROM student
)
LIMIT 1 -- required because all rows will qualify if
-- WHERE clause is satisfied
答案 1 :(得分:2)
您可以在该表上添加一个唯一的索引,它将为您执行相同的操作
ALTER TABLE student ADD UNIQUE <name_of_index>[optional] (FirstName);
编辑:
如果你不能使用唯一索引..
我能想到的一个解决方案是使用复合语句 - http://dev.mysql.com/doc/refman/5.0/en/if-statement.html