php - SQL -- IF EXISTS 不起作用

标签 php mysql sql if-statement exists

我正在尝试执行一个不起作用的 SQL 查询,我只想将一个元素添加到表中,并且仅当该值不在表中时,类似这样:

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

最好的方法是什么?

PS:我尝试通过谷歌和论坛查找该信息,但我不明白这个想法

提前非常感谢

PS2:抱歉,这是一个MYSQL数据库

最佳答案

如何使用 IF NOT EXISTS...

类似于:

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

关于php - SQL -- IF EXISTS 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19008862/

相关文章:

php - 在 Web 服务器上生成进程/运行可执行文件

mysql - 为什么MySQL在没有ORDER BY的情况下使用filesort?

mysql - 查询返回特定值

mysql - 我无法检索大于或等于 Sql 中特定日期的日期。

sql - `?` SQL `IN` 条件的占位符,具有持久性 's ` rawSql`

php - 简单的 dom 解析器 double find() 不起作用?

使用 mysqli 的 PHP 更新表单

php - 将数组数据插入到每个表列

mysql - 在 MySQL 中存储颜色信息并在 SELECT 查询中解析红色、绿色和蓝色值

sql - 将 SQL Server 连接限制为特定 IP 地址