如何检查表中的每个字段是否是另一个表中字段的子串 [英] How to check every field in a table is nt substring of a field in another table

查看:48
本文介绍了如何检查表中的每个字段是否是另一个表中字段的子串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的方案如下:我有两个表:table1table2table1 有一个字段:字符串类型的 table1-name.table2 有一个字段:table2-name 字符串类型.

My scheme looks like: I have two tables: table1, table2 table1 has one field: table1-name of type string. table2 has one field: table2-name of type string.

我想返回 table1-name 的所有行,这些行不是任何 table2-name 记录的子字符串.我做到了:

I want to return all rows of table1-name that are NOT substring of any table2-name records. I did:

SELECT DISTINCT `table2-name`.`table2`
FROM `table2`, `table1`
WHERE `table2-name`.`table2` NOT IN (SELECT `table1-name` FROM `table1`)
LIMIT 100;

但这会返回所有不等于 table1-nametable2-name.我需要的是所有 table2-name 不是 table2-name 的子字符串.

But this returns all table2-name that are not equal to table1-name. What I need is all table2-namethat are not sub-string of table2-name.

示例:

table1-name:aa.abc.com, bb.com, xyz.com

table1-name:aa.abc.com, bb.com, xyz.com

table2-name:abc.com、aaa.com、xyz.com

table2-name: abc.com, aaa.com, xyz.com

上面的查询将返回:

abc.com
aaa.com

我想返回的是:
aaa.com

我不希望返回 abc.com,因为它是 aa.abc.com 的子字符串.

I do not want abc.com to be returned because it is a sub-string of aa.abc.com.

你能更正我的查询吗?

推荐答案

对于这种情况使用 NOT EXISTS:

select *
from table1 t1
where not exists
(
  select *
  from table2 t2
  where t2.`table2-name` like concat('%', t1.`table1-name`, '%')
);

顺便说一句:您应该避免使用诸如 table2-name 之类的名称,在这些名称中您需要引号才能使用它们.改用 table2_name 之类的东西.

BTW: You should avoid names like table2-name where you need quotes in order to use them. Use something like table2_name instead.

这篇关于如何检查表中的每个字段是否是另一个表中字段的子串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆