You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何查询无对应普通商户的管理员商户数据

查找无对应普通商户的管理员商户

场景说明

商户表包含两类商户:

  • 管理员商户:名称末尾带有“Admin”
  • 普通商户:名称不含“Admin”
    两类商户的merchant_group_oid值一致。

需求

找出所有没有对应普通商户的管理员商户,比如示例中仅需列出Merchant 2 Admin:

Rows (column: name):
Merchant 1 Admin
Merchant 1
Merchant 2 Admin

现有查询局限

当前SQL仅能筛选出存在对应普通商户的管理员商户,无法满足需求:

select t1.oid,t1.name, t2.oid, t2.name From 
(select * from merchants where upper(name) like upper('%Admin%')) T1 ,
(select * from merchants where upper(name) not like upper('%Admin%')) T2
where t1.merchant_group_oid = t2.merchant_group_oid;

解决方案

方法1:左连接+空值筛选

将原内连接改为左连接,筛选出普通商户端无匹配数据的管理员商户:

SELECT t1.oid, t1.name
FROM (SELECT * FROM merchants WHERE UPPER(name) LIKE UPPER('%Admin%')) T1
LEFT JOIN (SELECT * FROM merchants WHERE UPPER(name) NOT LIKE UPPER('%Admin%')) T2
ON t1.merchant_group_oid = t2.merchant_group_oid
WHERE t2.oid IS NULL;

方法2:NOT EXISTS子查询

直接查询管理员商户,同时校验同merchant_group_oid下不存在对应的普通商户:

SELECT oid, name
FROM merchants t1
WHERE UPPER(t1.name) LIKE UPPER('%Admin%')
AND NOT EXISTS (
    SELECT 1 
    FROM merchants t2
    WHERE t2.merchant_group_oid = t1.merchant_group_oid
    AND UPPER(t2.name) NOT LIKE UPPER('%Admin%')
);

补充说明

  • 两种方法均可实现需求,NOT EXISTS在数据量较大时通常性能更优
  • 保留UPPER()是为了避免大小写导致的匹配遗漏

内容的提问来源于stack exchange,提问作者Sanguinary

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 06:39:15