如果某个名称不在列中则 SQL 插入
我想在表中插入记录:
UserType | UserName | Valid |
---|---|---|
Type1 | Name1 | True |
目前我使用的是以下sql代码:
insert into table_name
select 'certain type', 'a user''s name', 1
但是如果表中已经记录了用户名
,则会导致<代码>用户名列。
换句话说,如果用户名
没有出现在UerName
列中,我想插入一行记录。我怎样才能做到这一点?
I want to insert records into a table:
UserType | UserName | Valid |
---|---|---|
Type1 | Name1 | True |
Currently I am using the following sql code:
insert into table_name
select 'certain type', 'a user''s name', 1
but if a user's name
has been recorded in the table, that will cause duplicated names in UserName
column.
In other words, I want to insert a row of record if a user's name
does not show up in the UerName
column. How can I achieve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确的话,您希望在表的列中强制执行唯一值。
在这种情况下,您可以对列使用 unique 约束,或者如果是 MSSQL ( T-SQL)你可以这样做:
If i understood you correctly you want to enforce unique values within column in a table.
In that case you can either use unique constraint on a column, or if that's MSSQL (T-SQL) you can do something like this:
您可以添加
不存在
条件:You can add a
not exists
condition: