gpt4 book ai didi

mysql - 使用 MySQL,如何在同一个表中不存在该值的情况下插入表中?

转载 作者:可可西里 更新时间:2023-11-01 08:00:17 24 4
gpt4 key购买 nike

insert into student (FirstName,AGE,CITYID) values('guna','26','1')

select * from student WHERE FirstName!='guna';

此查询显示错误。我无法将 FirstName 列设为唯一。请给出除此之外的想法。

谢谢

最佳答案

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

关于mysql - 使用 MySQL,如何在同一个表中不存在该值的情况下插入表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6014072/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com