如果按值分组大于 1,Mysql 查询决定类型,则视为类型 2,否则为 1

问题描述

我想在按通道值分组时在mySQL查询中写一个case条件,如果计数大于1,它被认为是type_id 3,如果通道值没有重复,那么type_id应该是2 否则 0

select b.ChannelValue
case
when count(*),ChannelValue from tableb group by ChannelValue having count(*)=1 then 2
when count(*),ChannelValue from tableb group by ChannelValue having count(*)>1 then 3
else 0 END  AS type_id
from tablea a inner join tableb b
on a.ChannelValue = b.ChannelValue;

帮帮我!

解决方法

您可以使用以下内容:

let item = doc.data()
Promise.all([
  PostRawData(doc.data().RawData),checkBoundaries(doc.data().RawData),]).then(function (result) {
  item.decryptedData = result[0].data;
  item.colorIcon = result[1]
  items.push(item)
})

如果你有这个测试数据:

select ChannelValue,case
when (
  select count(*) from tableb 
  group by tableb.ChannelValue 
  having tableb.ChannelValue = tablea.ChannelValue) = 1 then 2
 when(
  select count(*) from tableb 
  group by tableb.ChannelValue 
  having tableb.ChannelValue = tablea.ChannelValue) > 1 then 3
 else 0
END as type_id
from tablea;

查询将返回:

ChannelValue type_id
100 3
200 0
300 2