寻找组合2个SQL查询的最佳方法

时间:2019-07-12 23:50:10

标签: mysql bash

我正在尝试结合2条SQL查询1:SELECT和2:DELETE。关于如何实现该目标的任何投入?

SELECT的输出在DELETE的WHERE子句中给出。请参考代码段。

select id from table1 where id_hash IN( select id_hash from table1 where name='stack' group by id_hash having count(*)>1

上述SELECT的输出(id)传递给DELETE

delete from table1 where id in (<id from SELECT>)

谢谢。

1 个答案:

答案 0 :(得分:-1)

这里是指向提出类似问题的人的链接:

How to write a SQL DELETE statement with a SELECT statement in the WHERE clause?

这是该线程的一个示例。归功于用户@AlexW

DELETE FROM tableA
WHERE ROWID IN 
  ( SELECT q.ROWID
    FROM tableA q
    INNER JOIN tableB u on (u.qlabel = q.entityrole AND u.fieldnum = q.fieldnum) 
    WHERE (LENGTH(q.memotext) NOT IN (8,9,10) OR q.memotext NOT LIKE '%/%/%')
    AND (u.FldFormat = 'Date'));