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

前端 未结 5 1654
梦毁少年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: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?

提交回复
热议问题