如何用WHERE子句中的SELECT语句编写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)
有人可以帮助我正确构build一个DELETE查询,这将导致正确的数据被删除?
您需要确定TableA中的主键以删除正确的logging。 主键可以是单个列,也可以是多个列的唯一标识表中的行的组合。 如果没有主键,则可以使用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'));
你不应该有:
DELETE FROM tableA WHERE entitynum IN (...your select...)
现在你只是有一个没有比较的地方:
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') )
在这种情况下:
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的语句中,你要从同一个表中select你想要从那个列中删除的表。
曾经有过这样的事情:
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
我调整了你的第二个查询,使其工作。 我认为这比在其他答案中使用嵌套的SELECT
语句更直接。
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')
更多细节在这里:
如何使用INNER JOIN与SQL Server删除?