sql - 基于文本列的正则表达式模式匹配将表拆分为相关表
问题描述
假设我们有一个产品表,其中每个供应商对同一产品的名称略有不同。使用正则表达式模式匹配来识别我们的相似字符串来拆分表的最佳方法是什么。
请参见以下示例:
p_id p_name cat start_time vendor attrs
-------------------------------------------------------------------------------------------
1 'iphone' 'phones' some date 'google' some_jsonb
2 'apple iphone' 'phones' some date 'ebay' some_other_jsonb
想要的结果:
table A
p_id p_name
1 'iphone'
table B
e_id p_ref vendor attrs
1 1 'google' some_jsonb
2 1 'ebay' some_other_jsonb
我的问题不在于正则表达式规则或将表拆分为两个相关表,而是在何处以及如何实施我的正则表达式规则以查找列 p_name 中的重复项?
解决方案
如果对于 each p_name
,您想要表中已有的最短子名,那么您可以使用如下逻辑:
select e.p_name
from example e
where not exists (select 1
from example e2
where e.p_name like '%' || e2.p_name || '%' and
e.p_name <> e2.pname
);
推荐阅读
- docker - 为什么我的 RUN 对构建的 docker 镜像没有影响?
- react-native - 通过变量将数据导出到另一个屏幕
- javascript - 是否应该始终使用子资源完整性和交叉引用?
- python - txt文件中的字典使用json加载和转储
- single-sign-on - 如何使用 SAML 2.0 响应进行特定 DNN 用户角色组身份验证和授权?
- sql - SQL 查询成功执行,但未进行任何更改
- java - Android Studio 抛出“android.content.res.Resources$NotFoundException”
- c# - 从零开始按年龄范围分组的实体框架
- nexus - 以编程方式添加代理存储库并在 Nexus 中编辑存储库组(Helm Chart)
- ios - 从 Windows 计算机在反应本机 IOS 中调试 webview