检查记录是否存在使用mysql删除它 [英] Check if record exists delete it using mysql

查看:106
本文介绍了检查记录是否存在使用mysql删除它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MySQL,我想检查记录是否存在,如果存在则删除此记录.

i'm using MySQL and i want to check if a record exists and if it exists delete this record.

我尝试了这个,但它对我不起作用:

i try this but it 's not working for me:

SELECT 'Barcelone' AS City, EXISTS(SELECT 1 FROM mytable WHERE City = 'Barcelone') AS 'exists';

THEN

DELETE FROM mytable
WHERE City = 'Barcelone';

感谢您的帮助.

推荐答案

if 语句只允许在存储过程、存储函数和触发器(在 MySQL 中)中使用.

The if statement is only allowed in stored procedures, stored functions, and triggers (in MySQL).

如果我明白你想要什么,就这样做:

If I understand what you want, just do:

DELETE FROM mytable
WHERE City = 'Barcelone';

没有理由事先检查是否存在.只需删除该行.如果不存在,则没有问题.没有错误.

There is no reason to check for the existence beforehand. Just delete the row. If none exist, no problem. No errors.

出于性能原因,我建议在 mytable(city) 上建立索引.如果您想先检查该行是否存在,那很好,但是对于delete 来说是不必要的.

I would recommend an index on mytable(city) for performance reasons. If you want to check if the row exists first, that is fine, but it is unnecessary for the delete.

这篇关于检查记录是否存在使用mysql删除它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆