遇到 EXISTS 和 GROUP BY 的问题
Having troubles with EXISTS and GROUP BY
我已经为此工作了一段时间,我找不到灵魂吗,所以也许你可以帮我。
我有一个包含 3 列的 table 'id' 'product' 'code',一个产品可以重复,但每个产品的代码必须是唯一的。这是 table:
的结构
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 的响应列中显示 'Types are : 1,2',我相信在子选择中搜索所有产品(而不是每个产品)所以总是如此代码 1 和代码 2 存在。
我们非常欢迎您提供任何帮助或指导。
感谢阅读。
Fiddle 示例:http://sqlfiddle.com/#!9/25eb55/3
您的子查询正在整个 table 中搜索您感兴趣的代码,而不仅仅是具有相同产品的行。
如果您希望仅针对具有相同产品的行评估子查询,则需要使用相关子查询。
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
我已经为此工作了一段时间,我找不到灵魂吗,所以也许你可以帮我。
我有一个包含 3 列的 table 'id' 'product' 'code',一个产品可以重复,但每个产品的代码必须是唯一的。这是 table:
的结构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 的响应列中显示 'Types are : 1,2',我相信在子选择中搜索所有产品(而不是每个产品)所以总是如此代码 1 和代码 2 存在。
我们非常欢迎您提供任何帮助或指导。
感谢阅读。
Fiddle 示例:http://sqlfiddle.com/#!9/25eb55/3
您的子查询正在整个 table 中搜索您感兴趣的代码,而不仅仅是具有相同产品的行。
如果您希望仅针对具有相同产品的行评估子查询,则需要使用相关子查询。
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