需要检查where
情况,如果没有找到where type='P'
则记录type='C'
这是表格
paper_id | product_id | type
1 1 P
2 1 P
3 1 C
4 1 C
5 2 C
6 2 C
有product_id
1
和2
,需要获取那些有type='P'
的记录但那些没有type='P'
记录的产品则从记录type='C'
查询后需要这个结果
paper_id | product_id | type
1 1 P
2 1 P
5 2 C
6 2 C
我试试
从表中选择 *,其中 CASE WHEN type !='P' THEN type='C' ELSE type='P'END
但不工作
最佳答案
select paper_id, product_id, type from your_tab
where type = 'P'
union all
select t1.paper_id, t1.product_id, t1.type from your_tab t1
where t1.type = 'C'
and not exists (select 1 from your_tab t2
where t2.product_id = t1.product_id and t2.type = 'P');
关于mysql - 如果在记录中找不到,则在 mysql 中获取其他 where case,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27456997/