如何在WHERE子句中使用SELECT语句编写SQL DELETE语句?

时间:2013-07-09 12:56:30

标签: sql select where-clause advantage-database-server sql-delete

数据库:Sybase Advantage 11

在我对数据进行规范化的过程中,我试图删除从SELECT语句中获得的结果:

SELECT tableA.entitynum
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')
;

这是我提出的DELETE陈述:

DELETE FROM tableA
WHERE (SELECT q.entitynum
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'))
;

当我尝试运行此语句时,我不断收到此错误:

ERROR IN SCRIPT: poQuery: Error 7200:  AQE Error:  State = S0000;   NativeError = 2124;
[iAnywhere Solutions][Advantage SQL Engine]Invalid operand for operator: = Boolean value
cannot be operated with non-Boolean value.

我也试过这句话:

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

结果是:

ERROR IN SCRIPT: poQuery: Error 7200:  AQE Error:  State = 42000;   NativeError = 2117;
[iAnywhere Solutions][Advantage SQL Engine] Unexpected token: INNER -- Expecting semicolon.
-- Location of error in the SQL statement is: 23 (line: 2 column: 1)

有人可以帮我正确构建一个DELETE查询,导致正确的数据被删除吗?

6 个答案:

答案 0 :(得分:104)

您需要在TableA中标识主键才能删除正确的记录。主键可以是单个列,也可以是多个列的组合,用于唯一标识表中的行。如果没有主键,则ROWID伪列可以用作主键。

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

答案 1 :(得分:19)

您的第二个DELETE查询几乎是正确的。请务必将表名(或别名)放在DELETEFROM 之间,以指定要删除的表。这比使用嵌套SELECT语句更简单。

更正的查询(选项1:使用完整的表名):

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

更正的查询(选项2:使用别名):

DELETE q
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')

此处有更多例子:
How to Delete using INNER JOIN with SQL Server?

答案 2 :(得分:13)

你不应该:

DELETE FROM tableA WHERE entitynum IN (...your select...)

现在你只有一个没有比较的WHERE:

DELETE FROM tableA WHERE (...your select...)

所以你的最终查询看起来像这样;

DELETE FROM tableA WHERE entitynum IN (
    SELECT tableA.entitynum 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')
)

答案 3 :(得分:4)

在这种情况下:

DELETE FROM tableA
WHERE (SELECT q.entitynum
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'));

你不想错过要比较的列吗?例如:

DELETE FROM tableA
WHERE entitynum in (SELECT q.entitynum
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'));    

我认为这是该列,因为在您的select语句中,您要从同一个表中选择要从该列中删除的列。

答案 4 :(得分:1)

曾经做过类似的事情:

CREATE TABLE exclusions(excl VARCHAR(250));
INSERT INTO exclusions(excl)
VALUES
       ('%timeline%'),
       ('%Placeholders%'),
       ('%Stages%'),
       ('%master_stage_1205x465%'),
       ('%Accessories%'),
       ('%chosen-sprite.png'),
('%WebResource.axd');
GO
CREATE VIEW ToBeDeleted AS 
SELECT * FROM chunks
       WHERE chunks.file_id IN
       (
       SELECT DISTINCT
             lf.file_id
       FROM LargeFiles lf
       WHERE lf.file_id NOT IN
             (
             SELECT DISTINCT
                    lf.file_id
             FROM LargeFiles lf
                LEFT JOIN exclusions e ON(lf.URL LIKE e.excl)
                WHERE e.excl IS NULL
             )
       );
GO
CHECKPOINT
GO
SET NOCOUNT ON;
DECLARE @r INT;
SET @r = 1;
WHILE @r>0

BEGIN
    DELETE TOP (10000) FROM ToBeDeleted;
    SET @r = @@ROWCOUNT  
END
GO

答案 5 :(得分:-1)

在我的项目中,我曾经删除重复项。 (В своем проекте использовал для удаления дублей)

DELETE 
FROM
    a_send 
WHERE
    send_id IN (
            SELECT
        PR.dd
    FROM
        ( SELECT MAX(send_id) as dd, COUNT ( 1 ) AS qty, phone_id FROM "a_send" GROUP BY phone_id ) AS PR 
    WHERE
        PR.qty > 1 
    ) 

注意为了不删除公国中的所有记录,使用MAX (send_id)
(обратите внимание для того чтобы в приниципе не удалилесь все записи пользовал MAX(send_id))