MySQL 如果存在,则删除行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12859884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete row if exists
提问by Kin
DELETE IF EXIST `#__menu`.*
FROM `#__menu`
LEFT JOIN `#__extensions` ON `#__extensions`.`name` = 'com_view'
WHERE `#__menu`.`component_id` = `#__xtensions`.`extension_id`
AND `#__menu`.`alias` = 'view-sites' AND `#__menu`.`path` = 'view-sites' AND `#__menu`.`title` = 'View sites';
What is wrong in my sql? I think the problem is in IF EXIST
, but i could not figure out how to use it on row.
我的sql有什么问题?我认为问题出在IF EXIST
,但我不知道如何在行上使用它。
回答by newfurniturey
When you're deleting rows from a table, you don't need to use IF EXISTS
- you're using a WHERE
clause, so if it exists - it will be deleted.
当您从表中删除行时,您不需要使用IF EXISTS
- 您正在使用WHERE
子句,因此如果它存在 - 它将被删除。
Try changing your query to:
尝试将您的查询更改为:
DELETE
FROM `#__menu`
LEFT JOIN `#__extensions` ON `#__extensions`.`name` = 'com_view'
WHERE `#__menu`.`component_id` = `#__xtensions`.`extension_id`
AND `#__menu`.`alias` = 'view-sites' AND `#__menu`.`path` = 'view-sites' AND `#__menu`.`title` = 'View sites';
Also, you don't need to specify ```#__menu.*`` (the columns) to be deleted - you'll just need
DELETE FROM...`. Check out herefor more info regarding the syntax.
此外,您不需要指定```#__menu .*`` (the columns) to be deleted - you'll just need
DELETE FROM...`。查看此处了解有关语法的更多信息。