【问题标题】:Having troubles with EXISTS and GROUP BYEXISTS 和 GROUP BY 遇到问题
【发布时间】:2016-12-08 17:47:26
【问题描述】:

我一直在处理这个问题,我找不到灵魂,所以也许你可以帮助我。

我有一个包含 3 列“id”“产品”“代码”的表,一个产品可以重复,但每个产品的代码必须是唯一的。这是表的结构:

CREATE TABLE table1
    (`id` int, `product` varchar(10), `code` int)
;

INSERT INTO table1
    (`id`, `product`, `code`)
VALUES
    (1, 'product1', 1),
    (2, 'product1', 2),
    (3, 'product1', 3),
    (4, 'product2', 2),
    (5, 'product2', 3),
    (6, 'product3', 1),
    (7, 'product3', 3)
;

所以我要做的是一个案例列表,如果产品有代码 1 和代码 2,则在响应列中显示某个值,如果产品只有代码 1,则显示另一个值,如果产品有代码 2 然后是其他值,如果产品既没有代码 1 也没有代码 2,则显示另一个值(代码 3 在此示例中无关紧要)。

这就是我到目前为止所做的事情

select product, 
    case 
    when exists(select 1 from table1 where code=1) = 1
        and exists(select 1 from table1 where code=2) = 1
    then 'Types are  : 1,2'
    when exists(select 1 from table1 where code=1) = 1
        and exists(select 1 from table1 where code=2) = 0
    then 'Type is  : 1'
    when exists(select 1 from table1 where code=1) = 0
        and exists(select 1 from table1 where code=2) = 1
    then 'Type is  : 2'
    else
        'There are no types 1 or 2'
    end as response
from table1
group by product

问题是结果集仅在我的产品1、产品2和产品3的响应列中显示“类型是:1,2”,我相信在子选择中搜索所有产品(而不是每个产品)所以总是确实存在代码 1 和代码 2。

我们非常欢迎您提供任何帮助或指导。

感谢阅读。

小提琴示例:http://sqlfiddle.com/#!9/25eb55/3

【问题讨论】:

    标签: mysql exists


    【解决方案1】:

    您的子查询正在整个表中搜索您感兴趣的代码,而不仅仅是具有相同产品的行。

    如果您希望仅针对具有相同产品的行评估子查询,则需要使用相关子查询

    select p.product, 
        case 
        when exists(select 1 from table1 where code=1 and product=p.product) = 1
            and exists(select 1 from table1 where code=2 and product=p.product) = 1
        then 'Types are  : 1,2'
        when exists(select 1 from table1 where code=1 and product=p.product) = 1
            and exists(select 1 from table1 where code=2 and product=p.product) = 0
        then 'Type is  : 1'
        when exists(select 1 from table1 where code=1 and product=p.product) = 0
            and exists(select 1 from table1 where code=2 and product=p.product) = 1
        then 'Type is  : 2'
        else
            'There are no types 1 or 2'
        end as response
    from table1 as p
    group by product
    

    输出:

    +----------+------------------+
    | product  | response         |
    +----------+------------------+
    | product1 | Types are  : 1,2 |
    | product2 | Type is  : 2     |
    | product3 | Type is  : 1     |
    +----------+------------------+
    

    但是,我通常会避免使用相关子查询,因为它们的性能成本很高。 MySQL 必须为外部查询中的每一行重新执行子查询。

    这是一个不使用子查询但给出相同结果的替代查询:

    SELECT product,
      CASE GROUP_CONCAT(CASE WHEN code IN (1,2) THEN code ELSE NULL END ORDER BY code)
      WHEN '1' THEN 'Type is  : 1'
      WHEN '1,2' THEN 'Types are: 1,2'
      WHEN '2' THEN 'Type is  : 2'
      ELSE 'There are no types 1 or 2'
      END AS response
    FROM table1
    GROUP BY product
    

    【讨论】:

    • 肯定快多了,我担心性能,但现在不用了。
    猜你喜欢
    • 1970-01-01
    • 2012-10-02
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多