使用来自另一个表的数据使用sql'like'命令删除行

时间:2011-01-05 15:41:51

标签: sql database oracle database-design

我试图从表中删除行(“lovalarm”),其中字段(“pointid”)就像许多字符串中的任何一个。

目前我手动输入它们但是我需要能够拥有超过100,000个选项的列表。

我的想法是拥有一个包含所有可能字符串的表(“lovdata”)并运行查询以删除其中字段“喜欢”其他表中任何字符串的行。

任何人都可以指出我是否能够以这种方式使用这种方式的正确方向吗?

非常感谢, 帽

3 个答案:

答案 0 :(得分:7)

确定您可以加入LIKE运算符:

DELETE FROM lovalarm a 
 WHERE EXISTS (SELECT NULL 
                 FROM lovdata d 
                WHERE a.pointid LIKE d.pointid)

lovdata.pointid列可能包含通配符,请考虑:

SQL> CREATE TABLE lovalarm AS
  2  SELECT 'AA' pointid FROM dual
  3  UNION ALL SELECT 'AB' FROM dual
  4  UNION ALL SELECT 'AC' FROM dual
  5  UNION ALL SELECT 'BA' FROM dual
  6  UNION ALL SELECT 'BB' FROM dual
  7  UNION ALL SELECT 'BC' FROM dual;    

Table created

SQL> --# suppose that you want to remove all entries that start
  2  --# with A or end with C
  3  CREATE TABLE lovdata AS
  4  SELECT 'A%' pointid FROM dual
  5  UNION ALL SELECT '%C' FROM dual;

Table created

SQL> DELETE FROM lovalarm a
  2   WHERE EXISTS (SELECT NULL
  3                   FROM lovdata d
  4                  WHERE a.pointid LIKE d.pointid);

4 rows deleted

SQL> select * from lovalarm;

POINTID
-------
BA
BB

答案 1 :(得分:2)

只需使用子查询,如下所示:

DELETE 
  FROM lovalarm
 WHERE pointid IN (SELECT mystring FROM lovdata)

编辑:刚刚注意到LIKE是必需的,因此IN无法正常工作,因为这将完全匹配。我会留在这里作为参考,因为文森特已经添加了正确的查询。

答案 2 :(得分:1)

这是%concatenations

的示例
CREATE TABLE LOVALARM(POINTID VARCHAR2(50) ,TXT VARCHAR2(50));
create table deleteThese(deleteStringList varchar2(50));
/
INSERT INTO LOVALARM(POINTID, TXT) VALUES('abc def','1');
INSERT INTO LOVALARM(POINTID, TXT) VALUES('def','2');
INSERT INTO LOVALARM(POINTID, TXT) VALUES('abc','3');
INSERT INTO LOVALARM(POINTID, TXT) VALUES('efd','4');
INSERT INTO LOVALARM(POINTID, TXT) VALUES('abb','5');
INSERT INTO LOVALARM(POINTID, TXT) VALUES('efd bbb','6');
INSERT INTO LOVALARM(POINTID, TXT) VALUES('abb ccc','7');
INSERT INTO LOVALARM(POINTID, TXT) VALUES('abbbccc','8');


INSERT INTO DELETETHESE(DELETESTRINGLIST) VALUES('abc');
INSERT INTO DELETETHESE(DELETESTRINGLIST) VALUES('bbb');

COMMIT;
/

DELETE LOVALARM 
 where exists(
            SELECT NULL
              FROM DELETETHESE
             where LOVALARM.pointid like '%' || dELETETHESE.DELETESTRINGLIST  || '%'
        );

select * from LOVALARM;

POINTID                                            TXT                                                
-------------------------------------------------- -----
def                                                2                                                  
efd                                                4                                                  
ABB                                                5                                                  
abb ccc                                            7