当前位置:网站首页>MySQL delete from and subquery as conditions
MySQL delete from and subquery as conditions
2022-06-22 08:00:00 【xiaoke815】
I am trying such a query :
DELETE FROM term_hierarchy AS th
WHERE th.parent = 1015 AND th.tid IN (
SELECT DISTINCT(th1.tid)
FROM term_hierarchy AS th1
INNER JOIN term_hierarchy AS th2 ON (th1.tid = th2.tid AND th2.parent != 1015)
WHERE th1.parent = 1015
);
You can tell me , If the same tid Have other parents , I want to delete the parent relationship 1015. however , This will give me a grammatical error :
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS th WHERE th.parent = 1015 AND th.tid IN ( SELECT DISTINCT(th1.tid) FROM ter' at line 1
I've checked the document , And run the subquery by yourself , All this seems to have to be checked out . Can anyone figure out what's wrong here ?
to update : Answer as follows ,MySQL Tables to be deleted are not allowed in subqueries .
The best solution
You cannot specify the target table to delete .
resolvent
create table term_hierarchy_backup (tid int(10)); <- check data type
insert into term_hierarchy_backup
SELECT DISTINCT(th1.tid)
FROM term_hierarchy AS th1
INNER JOIN term_hierarchy AS th2 ON (th1.tid = th2.tid AND th2.parent != 1015)
WHERE th1.parent = 1015;
DELETE FROM term_hierarchy AS th
WHERE th.parent = 1015 AND th.tid IN (select tid from term_hierarchy_backup);
Second best solution
For those who want to remove this problem when using subqueries , I give you this example to illustrate MySQL The advantages of ( Even if some people seem to think it can't be done ):
DELETE e.*
FROM tableE e
WHERE id IN (SELECT id
FROM tableE
WHERE arg = 1 AND foo = 'bar');
Will give you a mistake :
ERROR 1093 (HY000): You can't specify target table 'e' for update in FROM clause
But this query :
DELETE e.*
FROM tableE e
WHERE id IN (SELECT id
FROM (SELECT id
FROM tableE
WHERE arg = 1 AND foo = 'bar') x);
Will work well :
Query OK, 1 row affected (3.91 sec)
Include subqueries in an additional subquery ( It's called x) in ,MySQL Will be happy to do what you ask .
The third solution
DELETE The keyword should be followed by an alias :
DELETE th
FROM term_hierarchy AS th
WHERE th.parent = 1015 AND th.tid IN
(
SELECT DISTINCT(th1.tid)
FROM term_hierarchy AS th1
INNER JOIN term_hierarchy AS th2 ON (th1.tid = th2.tid AND th2.parent != 1015)
WHERE th1.parent = 1015
);
The fourth option
You need to delete The alias is referenced again in the statement , Such as :
DELETE th FROM term_hierarchy AS th
....
As outlined here in MySQL docs.
The fifth option
I approach this in a slightly different way , It works for me ;
I need to quote from conditions Delete... From the table of the table secure_links, There are no longer any condition lines . A chore script basically gave me the wrong – You cannot specify the target table to delete .
So find inspiration here , I came up with the following query , It works properly . This is because it creates a file to be used as DELETE Temporary table for reference sl1.
DELETE FROM `secure_links` WHERE `secure_links`.`link_id` IN
(
SELECT
`sl1`.`link_id`
FROM
(
SELECT
`sl2`.`link_id`
FROM
`secure_links` AS `sl2`
LEFT JOIN `conditions` ON `conditions`.`job` = `sl2`.`job`
WHERE
`sl2`.`action` = 'something' AND
`conditions`.`ref` IS NULL
) AS `sl1`
)
Work for me
reference
边栏推荐
- MySQL query database capacity
- White paper on Web page quality of Baidu search engine guides website construction and optimization
- IP address planning
- 【 Oracle database】 Nursery Mother Tutorial day13 date Function
- Relative positioning, absolute positioning, fixed positioning
- AutoCAD 2020.3中文版 (旧版本)
- Charles uses
- AudioQueue
- enable_ irq_ Wake interrupt wakes up the kernel in low power mode
- Spritemanager load Atlas
猜你喜欢

模电实验——实验一 晶体管共射极单管放大器

面试突击59:一个表中可以有多个自增列吗?

Win openfeign from simple to deep

What is distributed transaction

(7)双向链表

Qualcomm platform msm8953 display subsystem learning

【Oracle 数据库】奶妈式教程 day11 数值函数

对于mysql中数据为NULL引发的一些问题和思考

【宋红康 MySQL数据库 】【高级篇】【06】MySQL的逻辑架构

Model electricity experiment -- Experiment 1 transistor common emitter single transistor amplifier
随机推荐
Target detection series -- detailed explanation of RCNN principle
MySQL transactions
AudioQueue
Expérience électrique en mode - - expérience 2 circuit d'amplification de source commune JFET
Applet /vant UI to upload files
Lookup encapsulation of unity monobehavior component
Do you want to modify the title of the website
MySQL query database capacity
丰田bZ4X取消上市发布会,就算低温充电问题不存在,产品力如何?
MySQL index
UI draw line
Excellent cases of data visualization
Symbolic processing of crash log
mysql查询group by 1055 问题完美解决,最简单最便捷的方法
The difference between get and post requests
Spritemanager load Atlas
【宋红康 MySQL数据库 】【高级篇】【07】MySQL的存储引擎
Modular import and export collation in JS
Cocoapods creates private libraries and publishes them
Multimedia architecture -- Introduction to display