简体   繁体   English

SQL - 如果EXISTS不起作用

[英]SQL — IF EXISTS doesnt work

Im trying to doing a sql query that is not working, I would like to add one element to a table only and only if that value is not in the table, something like that: 我试图做一个不起作用的SQL查询,我想只添加一个元素到一个表,只有当该值不在表中时,类似的东西:

IF EXISTS (Select `id` from `artists` Where `artist`='"$artist"') Do Nothing
ELSE      Insert into `artists` (`artist`) values('".$artist."')

What is the best way to do that? 最好的方法是什么?

PS: I have tried to look for that information by google and in the forum, but I dont understand the idea PS:我试图通过谷歌和论坛寻找这些信息,但我不明白这个想法

Thank you very much in advance 非常感谢你提前

PS2: Sorry, Its a MYSQL database PS2:抱歉,这是一个MYSQL数据库

What about using IF NOT EXISTS... 怎么样使用IF NOT EXISTS ...

Something like: 就像是:

IF NOT EXISTS (Select `id` from `artists` Where `artist`='"$artist"') Insert into `artists` (`artist`) values('".$artist."')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM