Delete row if exists

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 in a string.

+6
source share
1 answer

When you delete rows from a table, you do not need to use IF EXISTS - you use the WHERE , so if it exists, it will be deleted.

Try changing the query:

 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'; 

In addition, you do not need to specify `` #__ menu .*`` (the columns) to be deleted - you'll just need DELETE FROM ... `. Read more about syntax here .

+12
source

Source: https://habr.com/ru/post/927572/


All Articles