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

前端 未结 5 1626
梦毁少年i
梦毁少年i 2020-11-30 02:10

Database: Sybase Advantage 11

On my quest to normalize data, I am trying to delete the results I get from this SELECT statement:

SELECT          


        
相关标签:
5条回答
  • 2020-11-30 02:42

    You need to identify the primary key in TableA in order to delete the correct record. The primary key may be a single column or a combination of several columns that uniquely identifies a row in the table. If there is no primary key, then the ROWID pseudo column may be used as the primary key.

    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'));
    
    0 讨论(0)
  • 2020-11-30 02:48

    Shouldn't you have:

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

    Now you just have a WHERE with no comparison:

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

    So your final query would look like this;

    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')
    )
    
    0 讨论(0)
  • 2020-11-30 02:50

    in this scenario:

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

    aren't you missing the column you want to compare to? example:

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

    I assume it's that column since in your select statement you're selecting from the same table you're wanting to delete from with that column.

    0 讨论(0)
  • 2020-11-30 02:55

    Your second DELETE query was nearly correct. Just be sure to put the table name (or an alias) between DELETE and FROM to specify which table you are deleting from. This is simpler than using a nested SELECT statement like in the other answers.

    Corrected Query (option 1: using full table name):

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

    Corrected Query (option 2: using an alias):

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

    More examples here:
    How to Delete using INNER JOIN with SQL Server?

    0 讨论(0)
  • 2020-11-30 03:00

    Did something like that once:

    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
    
    0 讨论(0)
提交回复
热议问题