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

请求编写SQL语句:找出对应多Product Key或共享Key的Product

查询满足指定条件的Product

需求

找出满足以下任一条件的Product:

  • 同一Product对应多个不同的Product Key
  • 该Product的Product Key被其他Product共享

表结构

ProductProduct Key
First10
First1
Second2
Second2
Third3
Four3
Test5
Test5

预期输出

Product
First
Third
Four

解决方案

可以通过子查询分别筛选两种情况,再合并结果:

SELECT DISTINCT Product
FROM your_table_name
WHERE 
    -- 条件1:同一Product对应多个不同的Product Key
    Product IN (
        SELECT Product
        FROM your_table_name
        GROUP BY Product
        HAVING COUNT(DISTINCT `Product Key`) > 1
    )
    OR
    -- 条件2:Product Key被其他Product共享
    `Product Key` IN (
        SELECT `Product Key`
        FROM your_table_name
        GROUP BY `Product Key`
        HAVING COUNT(DISTINCT Product) > 1
    );

说明

  1. 第一个子查询筛选出对应多个不同Product Key的Product;
  2. 第二个子查询筛选出被多个Product共享的Product Key,再关联到对应的Product;
  3. 使用DISTINCT避免重复结果,通过OR合并两种条件的匹配项。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 06:17:05