简体   繁体   English

SQL从表中排除LIKE项

[英]SQL Exclude LIKE items from table

I'm trying to figure out how to exclude items from a select statement from table A using an exclusion list from table B. The catch is that I'm excluding based on the prefix of a field. 我正试图弄清楚如何使用表B中的排除列表从表A中的select语句中排除项目。问题是我根据字段的前缀排除。

So a field value maybe "FORD Muffler" and to exclude it from a basic query I would do: 所以一个字段值可能是“FORD Muffler”并将其排除在基本查询之外:

SELECT FieldName 
FROM TableName 
WHERE UPPER(ColumnName) NOT LIKE 'FORD%'

But to use a list of values to exclude from a different tabel I would use a Subquery like: 但是要使用值列表从不同的表格中排除我会使用子查询,如:

SELECT FieldName 
FROM TableName 
WHERE UPPER(ColumnName) NOT IN (Select FieldName2 FROM TableName2)

The problem is that it only excludes exact matches and not LIKE or Wildcards (%). 问题是它只排除完全匹配而不是LIKE或通配符(%)。

How can I accomplish this task? 我怎样才能完成这项任务? Redesigning the table isn't an option as it is an existing table in use. 重新设计表不是一个选项,因为它是一个正在使用的现有表。

EDIT: Sorry I am using SQL Server (2005). 编辑:对不起,我正在使用SQL Server(2005)。

I think this will do it: 我想这会做到:

SELECT FieldName
FROM TableName
LEFT JOIN TableName2 ON UPPER(ColumnName) LIKE TableName2.FieldName2 + '%'
WHERE TableName2.FieldName2 IS NULL

Dunno how efficient this would be, but it should work: Dunno这将是多么有效,但它应该工作:

SELECT FieldName 
FROM TableName t1
WHERE NOT EXISTS (
    SELECT *
    FROM TableName2 t2
    WHERE t1.FieldName LIKE t2.FieldName2 + '%'
)
SELECT * FROM table_A 
LEFT OUTER JOIN table_B 
    ON (locate(table_b.column, UPPER(table_a.column)) == 1)
WHERE table_b.column IS NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM